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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
package mill.define
import mill.discover.Discovered
import Discovered.mapping
import mill.eval.Evaluator
import mill.util.{TestGraphs, TestUtil}
import utest._
import mill.util.Strict.Agg
object GraphTests extends TestSuite{
val tests = Tests{
val graphs = new TestGraphs()
import graphs._
import TestGraphs._
'topoSortedTransitiveTargets - {
def check(targets: Agg[Task[_]], expected: Agg[Task[_]]) = {
val result = Graph.topoSorted(Graph.transitiveTargets(targets)).values
TestUtil.checkTopological(result)
assert(result == expected)
}
'singleton - check(
targets = Agg(singleton.single),
expected = Agg(singleton.single)
)
'pair - check(
targets = Agg(pair.down),
expected = Agg(pair.up, pair.down)
)
'anonTriple - check(
targets = Agg(anonTriple.down),
expected = Agg(anonTriple.up, anonTriple.down.inputs(0), anonTriple.down)
)
'diamond - check(
targets = Agg(diamond.down),
expected = Agg(diamond.up, diamond.left, diamond.right, diamond.down)
)
'anonDiamond - check(
targets = Agg(diamond.down),
expected = Agg(
diamond.up,
diamond.down.inputs(0),
diamond.down.inputs(1),
diamond.down
)
)
'defCachedDiamond - check(
targets = Agg(defCachedDiamond.down),
expected = Agg(
defCachedDiamond.up.inputs(0),
defCachedDiamond.up,
defCachedDiamond.down.inputs(0).inputs(0).inputs(0),
defCachedDiamond.down.inputs(0).inputs(0),
defCachedDiamond.down.inputs(0).inputs(1).inputs(0),
defCachedDiamond.down.inputs(0).inputs(1),
defCachedDiamond.down.inputs(0),
defCachedDiamond.down
)
)
'bigSingleTerminal - {
val result = Graph.topoSorted(Graph.transitiveTargets(Agg(bigSingleTerminal.j))).values
TestUtil.checkTopological(result)
assert(result.size == 28)
}
}
'groupAroundNamedTargets - {
def check[T, R <: Target[Int]](base: T)
(target: T => R,
important0: Agg[T => Target[_]],
expected: Agg[(R, Int)]) = {
val topoSorted = Graph.topoSorted(Graph.transitiveTargets(Agg(target(base))))
val important = important0.map(_ (base))
val grouped = Graph.groupAroundImportantTargets(topoSorted) {
case t: Target[_] if important.contains(t) => t
}
val flattened = Agg.from(grouped.values().flatMap(_.items))
TestUtil.checkTopological(flattened)
for ((terminal, expectedSize) <- expected) {
val grouping = grouped.lookupKey(terminal)
assert(
grouping.size == expectedSize,
grouping.flatMap(_.asTarget: Option[Target[_]]).filter(important.contains) == Agg(terminal)
)
}
}
'singleton - check(singleton)(
_.single,
Agg(_.single),
Agg(singleton.single -> 1)
)
'pair - check(pair)(
_.down,
Agg(_.up, _.down),
Agg(pair.up -> 1, pair.down -> 1)
)
'anonTriple - check(anonTriple)(
_.down,
Agg(_.up, _.down),
Agg(anonTriple.up -> 1, anonTriple.down -> 2)
)
'diamond - check(diamond)(
_.down,
Agg(_.up, _.left, _.right, _.down),
Agg(
diamond.up -> 1,
diamond.left -> 1,
diamond.right -> 1,
diamond.down -> 1
)
)
'defCachedDiamond - check(defCachedDiamond)(
_.down,
Agg(_.up, _.left, _.right, _.down),
Agg(
defCachedDiamond.up -> 2,
defCachedDiamond.left -> 2,
defCachedDiamond.right -> 2,
defCachedDiamond.down -> 2
)
)
'anonDiamond - check(anonDiamond)(
_.down,
Agg(_.down, _.up),
Agg(
anonDiamond.up -> 1,
anonDiamond.down -> 3
)
)
'bigSingleTerminal - check(bigSingleTerminal)(
_.j,
Agg(_.a, _.b, _.e, _.f, _.i, _.j),
Agg(
bigSingleTerminal.a -> 3,
bigSingleTerminal.b -> 2,
bigSingleTerminal.e -> 9,
bigSingleTerminal.i -> 6,
bigSingleTerminal.f -> 4,
bigSingleTerminal.j -> 4
)
)
}
'multiTerminalGroupCounts - {
def countGroups(mapping: Discovered.Mapping[_], goals: Task[_]*) = {
val topoSorted = Graph.topoSorted(
Graph.transitiveTargets(Agg.from(goals))
)
val grouped = Graph.groupAroundImportantTargets(topoSorted) {
case t: NamedTask[Any] => t
case t if goals.contains(t) => t
}
grouped.keyCount
}
'separateGroups - {
import separateGroups._
val groupCount = countGroups(mapping(separateGroups), right, left)
assert(groupCount == 3)
}
'triangleTask - {
// Make sure the following graph ends up as a single group, since although
// `right` depends on `left`, both of them depend on the un-cached `task`
// which would force them both to re-compute every time `task` changes
import triangleTask._
val groupCount = countGroups(mapping(triangleTask), right, left)
assert(groupCount == 2)
}
'multiTerminalGroup - {
// Make sure the following graph ends up as two groups
import multiTerminalGroup._
val groupCount = countGroups(mapping(multiTerminalGroup), right, left)
assert(groupCount == 2)
}
'multiTerminalBoundary - {
// Make sure the following graph ends up as a three groups: one for
// each cached target, and one for the downstream task we are running
import multiTerminalBoundary._
val groupCount = countGroups(mapping(multiTerminalBoundary), task2)
assert(groupCount == 3)
}
}
}
}
|