summaryrefslogtreecommitdiff
path: root/main/test/src/mill/eval/TaskTests.scala
blob: 4ba65e17ba05eec1aa31cb5005911473167982d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package mill.eval

import utest._
import ammonite.ops._
import mill.T

import mill.util.TestEvaluator
object TaskTests extends TestSuite{
  val tests = Tests{
    object build extends mill.util.TestUtil.BaseModule{
      var count = 0
      // Explicitly instantiate `Function1` objects to make sure we get
      // different instances each time
      def staticWorker = T.worker{
        new Function1[Int, Int] {
          def apply(v1: Int) = v1 + 1
        }
      }
      def noisyWorker = T.worker{
        new Function1[Int, Int] {
          def apply(v1: Int) = input() + 1
        }
      }
      def input = T.input{
        count += 1
        count
      }
      def task = T.task{
        count += 1
        count
      }
      def taskInput = T{ input() }
      def taskNoInput = T{ task() }

      def persistent = T.persistent{
        input() // force re-computation
        mkdir(T.ctx().dest)
        write.append(T.ctx().dest/'count, "hello\n")
        read.lines(T.ctx().dest/'count).length
      }
      def nonPersistent = T{
        input() // force re-computation
        mkdir(T.ctx().dest)
        write.append(T.ctx().dest/'count, "hello\n")
        read.lines(T.ctx().dest/'count).length
      }

      def staticWorkerDownstream = T{
        staticWorker().apply(1)
      }
      def noisyWorkerDownstream = T{
        noisyWorker().apply(1)
      }
    }

    'inputs - {
      // Inputs always re-evaluate, including forcing downstream cached Targets
      // to re-evaluate, but normal Tasks behind a Target run once then are cached
      val check = new TestEvaluator(build)

      val Right((1, 1)) = check.apply(build.taskInput)
      val Right((2, 1)) = check.apply(build.taskInput)
      val Right((3, 1)) = check.apply(build.taskInput)

      val Right((4, 1)) = check.apply(build.taskNoInput)
      val Right((4, 0)) = check.apply(build.taskNoInput)
      val Right((4, 0)) = check.apply(build.taskNoInput)
    }

    'persistent - {
      // Persistent tasks keep the working dir around between runs
      val check = new TestEvaluator(build)
      val Right((1, 1)) = check.apply(build.persistent)
      val Right((2, 1)) = check.apply(build.persistent)
      val Right((3, 1)) = check.apply(build.persistent)

      val Right((1, 1)) = check.apply(build.nonPersistent)
      val Right((1, 1)) = check.apply(build.nonPersistent)
      val Right((1, 1)) = check.apply(build.nonPersistent)
    }

    'worker - {
      // Persistent task
      def check = new TestEvaluator(build)

      val Right((2, 1)) = check.apply(build.noisyWorkerDownstream)
      val Right((3, 1)) = check.apply(build.noisyWorkerDownstream)
      val Right((4, 1)) = check.apply(build.noisyWorkerDownstream)

      val Right((2, 1)) = check.apply(build.staticWorkerDownstream)
      val Right((2, 0)) = check.apply(build.staticWorkerDownstream)
      val Right((2, 0)) = check.apply(build.staticWorkerDownstream)
    }
  }
}