Skip to content

Commit 270b56a

Browse files
Tweak Maven build gen tests (#4079)
I had to update the test data of `mill.main.maven.BuildGenTests` for #4067. The output of those tests didn't really help. This PR changes the helper method checking if generated files match test data in resources, so that it prints more helpful output (removed / added files, diff of modified files), and allows to update the test data on disk by setting `BuildGenTests.updateSnapshots` to true.
1 parent dfa8e7d commit 270b56a

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

Diff for: main/maven/test/src/mill/main/maven/BuildGenTests.scala

+38-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ package mill.main.maven
22

33
import mill.T
44
import mill.api.PathRef
5+
import mill.main.client.OutFiles
56
import mill.scalalib.scalafmt.ScalafmtModule
67
import mill.testkit.{TestBaseModule, UnitTester}
78
import utest.*
89
import utest.framework.TestPath
910

11+
import java.nio.file.FileSystems
12+
1013
object BuildGenTests extends TestSuite {
1114

15+
// Change this to true to update test data on disk
16+
def updateSnapshots = false
17+
1218
def tests: Tests = Tests {
1319
val resources = os.Path(sys.env("MILL_TEST_RESOURCE_DIR"))
1420
val scalafmtConfigFile = PathRef(resources / ".scalafmt.conf")
@@ -33,7 +39,7 @@ object BuildGenTests extends TestSuite {
3339
eval(module.reformat())
3440

3541
// test
36-
checkFiles(files, resources / expectedRel)
42+
checkFiles(files.map(_.path.relativeTo(dest).asSubPath), dest, resources / expectedRel)
3743
}
3844

3945
// multi level nested modules
@@ -125,15 +131,38 @@ object BuildGenTests extends TestSuite {
125131
.map(PathRef(_))
126132
.toSeq
127133

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
130156

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)
137164
}
165+
166+
diffExitCode == 0 || updateSnapshots
138167
}
139168
}

0 commit comments

Comments
 (0)