@@ -2,13 +2,19 @@ package mill.main.maven
2
2
3
3
import mill .T
4
4
import mill .api .PathRef
5
+ import mill .main .client .OutFiles
5
6
import mill .scalalib .scalafmt .ScalafmtModule
6
7
import mill .testkit .{TestBaseModule , UnitTester }
7
8
import utest .*
8
9
import utest .framework .TestPath
9
10
11
+ import java .nio .file .FileSystems
12
+
10
13
object BuildGenTests extends TestSuite {
11
14
15
+ // Change this to true to update test data on disk
16
+ def updateSnapshots = false
17
+
12
18
def tests : Tests = Tests {
13
19
val resources = os.Path (sys.env(" MILL_TEST_RESOURCE_DIR" ))
14
20
val scalafmtConfigFile = PathRef (resources / " .scalafmt.conf" )
@@ -33,7 +39,7 @@ object BuildGenTests extends TestSuite {
33
39
eval(module.reformat())
34
40
35
41
// test
36
- checkFiles(files, resources / expectedRel)
42
+ checkFiles(files.map(_.path.relativeTo(dest).asSubPath), dest , resources / expectedRel)
37
43
}
38
44
39
45
// multi level nested modules
@@ -125,15 +131,38 @@ object BuildGenTests extends TestSuite {
125
131
.map(PathRef (_))
126
132
.toSeq
127
133
128
- def checkFiles (actualFiles : Seq [PathRef ], expectedRoot : os.Path ): Boolean = {
129
- val expectedFiles = buildFiles(expectedRoot)
134
+ def checkFiles (actualFiles : Seq [os.SubPath ], root : os.Path , expectedRoot : os.Path ): Boolean = {
135
+ // Non *.mill files, that are not in test data, that we don't want
136
+ // to see in the diff
137
+ val toCleanUp = os.walk(root, skip = _.startsWith(root / OutFiles .defaultOut))
138
+ .filter(os.isFile)
139
+ .filter(! _.lastOpt.exists(_.endsWith(" .mill" )))
140
+ toCleanUp.foreach(os.remove)
141
+
142
+ // Try to normalize permissions while not touching those of committed test data
143
+ val supportsPerms = FileSystems .getDefault.supportedFileAttributeViews().contains(" posix" )
144
+ if (supportsPerms)
145
+ for {
146
+ testFile <- os.walk(expectedRoot)
147
+ if os.isFile(testFile)
148
+ targetFile = root / testFile.relativeTo(expectedRoot).asSubPath
149
+ if os.isFile(targetFile)
150
+ }
151
+ os.perms.set(targetFile, os.perms(testFile))
152
+
153
+ val diffExitCode = os.proc(" git" , " diff" , " --no-index" , expectedRoot, root)
154
+ .call(stdin = os.Inherit , stdout = os.Inherit , check = ! updateSnapshots)
155
+ .exitCode
130
156
131
- actualFiles.nonEmpty &&
132
- actualFiles.length == expectedFiles.length &&
133
- actualFiles.iterator.zip(expectedFiles.iterator).forall {
134
- case (actual, expected) =>
135
- actual.path.endsWith(expected.path.relativeTo(expectedRoot)) &&
136
- os.read.lines(actual.path) == os.read.lines(expected.path)
157
+ if (updateSnapshots && diffExitCode != 0 ) {
158
+ System .err.println(
159
+ s " Expected and actual files differ, updating expected files in resources under $expectedRoot"
160
+ )
161
+
162
+ os.remove.all(expectedRoot)
163
+ os.copy(root, expectedRoot)
137
164
}
165
+
166
+ diffExitCode == 0 || updateSnapshots
138
167
}
139
168
}
0 commit comments