summaryrefslogtreecommitdiff
path: root/core/src/test/scala/mill/discover/DiscoveredTests.scala
blob: e917096747dff74e59587a5191ed524df3cdb41a (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package mill.discover

import java.io.InputStreamReader

import mill.discover.Router.{ArgSig, EntryPoint}
import utest._
import mill.{Module, T}
import mill.util.TestUtil.test
object DiscoveredTests extends TestSuite{

  val tests = Tests{

    'targets - {
      class CanNest extends Module{
        val single = test()
        val invisible: Any = test()
        val invisible2: mill.define.Task[Int] = test()
        val invisible3: mill.define.Task[_] = test()
      }
      object outer {
        val single = test()
        val invisible: Any = test()
        object nested extends Module{
          val single = test()
          val invisible: Any = test()

        }
        val classInstance = new CanNest

      }

      val discovered = Discovered[outer.type]


      def flatten(h: Mirror[outer.type, _]): Seq[Any] = {
        h.node(outer) :: h.children.flatMap{case (label, c) => flatten(c)}
      }
      val flattenedHierarchy = flatten(discovered.mirror)

      val expectedHierarchy = Seq(
        outer,
        outer.classInstance,
        outer.nested,
      )
      assert(flattenedHierarchy == expectedHierarchy)

      val mapped = discovered.targets(outer).map(x => x.segments -> x.target)

      val expected = Seq(
        (List("classInstance", "single"), outer.classInstance.single),
        (List("nested", "single"), outer.nested.single),
        (List("single"), outer.single)
      )
      assert(mapped.toSet == expected.toSet)
    }

    'commands - {
      object outer {
        def hello() = T.command{
          println("Hello")
        }
        def echoPair(prefix: String, suffix: String) = T.command{
          println(prefix + " " + suffix)
        }
        object nested extends Module{
          def inner(x: Int) = T.command{
            println(x)
          }
        }

      }

      val discovered = Discovered[outer.type]
      val outerCommands = discovered.mirror.commands

      assertMatch(outerCommands){case Seq(
        EntryPoint("echoPair",
          List(ArgSig("prefix", "String", None, None), ArgSig("suffix", "String", None, None)),
          None,
          false,
          _
        ),
        EntryPoint("hello", Nil, None, false, _)
      ) =>}

      val innerCommands = discovered.mirror
        .children
        .flatMap(_._2.commands.asInstanceOf[Seq[EntryPoint[_]]])

      assertMatch(innerCommands){case Seq(
        EntryPoint("inner", _, None, false, _),
      ) =>}
    }

    'compileError - {
      'unserializableTarget - {

        object outer extends Module {
          def single = mill.T{ new InputStreamReader(System.in) }
        }

        val error = compileError("Discovered[outer.type]")
        assert(
          error.msg.contains("could not find implicit value"),
          error.pos.contains("def single = mill.T{ new InputStreamReader(System.in) }")
        )
      }

      'unreadableCommand - {
        object outer extends Module {
          def single(in: InputStreamReader) = mill.T.command{ println(123) }
        }

        val error = compileError("Discovered[outer.type]")

        assert(
          error.msg.contains("could not find implicit value"),
          error.pos.contains("def single(in: InputStreamReader) = mill.T.command{ println(123) }")
        )
      }
    }
  }
}