diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2018-01-28 22:43:40 -0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2018-01-28 22:43:40 -0800 |
commit | bc777b3c4e83149f45df7edda245868e22495eb3 (patch) | |
tree | b2e58f641bc9db34d17193b495cf07e76a9e13a5 /core/test | |
parent | 42d24a0b59fa21bfa79040c2cd51a1f7384eee12 (diff) | |
download | mill-bc777b3c4e83149f45df7edda245868e22495eb3.tar.gz mill-bc777b3c4e83149f45df7edda245868e22495eb3.tar.bz2 mill-bc777b3c4e83149f45df7edda245868e22495eb3.zip |
Extract out `ScriptTestSuite` from `IntegrationTestSuite` and use it to create a version of `JavaCompilerJarTests` that runs through our main method & script runner.
This should let us catch a lot of bugs with `MainRunner` and friends quickly, without needing to run the slow integration tests
Diffstat (limited to 'core/test')
-rw-r--r-- | core/test/resources/examples/javac/build.sc | 23 | ||||
-rw-r--r-- | core/test/src/mill/main/JavaCompileJarTests.scala | 66 | ||||
-rw-r--r-- | core/test/src/mill/util/ScriptTestSuite.scala | 38 |
3 files changed, 127 insertions, 0 deletions
diff --git a/core/test/resources/examples/javac/build.sc b/core/test/resources/examples/javac/build.sc new file mode 100644 index 00000000..4a0882c6 --- /dev/null +++ b/core/test/resources/examples/javac/build.sc @@ -0,0 +1,23 @@ +import ammonite.ops._ +import mill.T +import mill.eval.JavaCompileJarTests.compileAll +import mill.eval.PathRef +import mill.modules.Jvm +import mill.util.Loose + +def sourceRootPath = basePath / 'src +def resourceRootPath = basePath / 'resources + +// sourceRoot -> allSources -> classFiles +// | +// v +// resourceRoot ----> jar +def sourceRoot = T.source{ sourceRootPath } +def resourceRoot = T.source{ resourceRootPath } +def allSources = T{ ls.rec(sourceRoot().path).map(PathRef(_)) } +def classFiles = T{ compileAll(allSources()) } +def jar = T{ Jvm.createJar(Loose.Agg(resourceRoot().path, classFiles().path)) } + +def run(mainClsName: String) = T.command{ + %%('java, "-cp", classFiles().path, mainClsName)(T.ctx().dest) +} diff --git a/core/test/src/mill/main/JavaCompileJarTests.scala b/core/test/src/mill/main/JavaCompileJarTests.scala new file mode 100644 index 00000000..a4c9fafe --- /dev/null +++ b/core/test/src/mill/main/JavaCompileJarTests.scala @@ -0,0 +1,66 @@ +package mill.main + +import ammonite.ops._ +import mill.util.ScriptTestSuite +import utest._ + +object JavaCompileJarTests extends ScriptTestSuite { + def workspaceSlug = "java-compile-jar" + def scriptSourcePath = pwd / 'core / 'test / 'resources / 'examples / 'javac + val tests = Tests{ + initWorkspace() + 'test - { + // Basic target evaluation works + assert(eval("classFiles")) + assert(eval("jar")) + + val classFiles1 = meta("classFiles") + val jar1 = meta("jar") + + assert(eval("classFiles")) + assert(eval("jar")) + + // Repeated evaluation has the same results + val classFiles2 = meta("classFiles") + val jar2 = meta("jar") + + assert( + jar1 == jar2, + classFiles1 == classFiles2 + ) + + // If we update resources, classFiles are unchanged but jar changes + for(scalaFile <- ls.rec(workspacePath).filter(_.ext == "txt")){ + write.append(scalaFile, "\n") + } + + assert(eval("classFiles")) + assert(eval("jar")) + + val classFiles3 = meta("classFiles") + val jar3 = meta("jar") + + assert( + jar2 != jar3, + classFiles2 == classFiles3 + ) + + // We can intentionally break the code, have the targets break, then + // fix the code and have them recover. + for(scalaFile <- ls.rec(workspacePath).filter(_.ext == "java")){ + write.append(scalaFile, "\n}") + } + + assert(!eval("classFiles")) + assert(!eval("jar")) + + for(scalaFile <- ls.rec(workspacePath).filter(_.ext == "java")){ + write.over(scalaFile, read(scalaFile).dropRight(2)) + } + + assert(eval("classFiles")) + assert(eval("jar")) + } + } +} + diff --git a/core/test/src/mill/util/ScriptTestSuite.scala b/core/test/src/mill/util/ScriptTestSuite.scala new file mode 100644 index 00000000..7a963ef9 --- /dev/null +++ b/core/test/src/mill/util/ScriptTestSuite.scala @@ -0,0 +1,38 @@ +package mill.util + +import java.io.{ByteArrayInputStream, ByteArrayOutputStream, PrintStream} + +import ammonite.ops._ +import mill.main.ParseArgs +import utest._ + +abstract class ScriptTestSuite extends TestSuite{ + def workspaceSlug: String + def scriptSourcePath: Path + + val workspacePath = pwd / 'target / 'workspace / workspaceSlug + val stdOutErr = new PrintStream(new ByteArrayOutputStream()) +// val stdOutErr = new PrintStream(System.out) + val stdIn = new ByteArrayInputStream(Array()) + val runner = new mill.main.MainRunner( + ammonite.main.Cli.Config(wd = workspacePath), false, + stdOutErr, stdOutErr, stdIn + ) + def eval(s: String*) = runner.runScript(workspacePath / "build.sc", s.toList) + def meta(s: String) = { + val (List(selector), args) = ParseArgs.apply(Seq(s)).right.get + + read(workspacePath / "out" / selector.flatMap(_.pathSegments) / "meta.json") + } + + + def initWorkspace() = { + rm(workspacePath) + mkdir(workspacePath / up) + // The unzipped git repo snapshots we get from github come with a + // wrapper-folder inside the zip file, so copy the wrapper folder to the + // destination instead of the folder containing the wrapper. + + cp(scriptSourcePath, workspacePath) + } +} |