From ee080e63971399ceb22fd8f059a97e956d9f0dcb Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Tue, 7 Nov 2017 21:10:06 -0800 Subject: - Allow main methods to return `Target[T]`s, so they can then be evaled by an external `Evaluator` that has the `Discovered` mapping available - Basic integration tests for `T.command` entrypoint running in the `JavaCompilerJarTests` suite --- .../src/test/scala/forge/JavaCompileJarTests.scala | 51 +++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'core/src/test/scala') diff --git a/core/src/test/scala/forge/JavaCompileJarTests.scala b/core/src/test/scala/forge/JavaCompileJarTests.scala index 6ce02149..92e1d0e9 100644 --- a/core/src/test/scala/forge/JavaCompileJarTests.scala +++ b/core/src/test/scala/forge/JavaCompileJarTests.scala @@ -1,7 +1,7 @@ package forge -import ammonite.ops._ +import ammonite.ops._, ImplicitWd._ import forge.define.Target import forge.discover.Discovered import forge.eval.{Evaluator, PathRef} @@ -44,10 +44,20 @@ object JavaCompileJarTests extends TestSuite{ def allSources = T{ ls.rec(sourceRoot().path).map(PathRef(_)) } def classFiles = T{ compileAll(allSources) } def jar = T{ jarUp(resourceRoot, classFiles) } + + @forge.discover.Router.main + def run(mainClsName: String): Target[CommandResult] = T.command{ + %%('java, "-cp", classFiles().path, mainClsName) + } } import Build._ val mapping = Discovered.mapping(Build) + def eval[T](t: Target[T]): (T, Int) = { + val evaluator = new Evaluator(workspacePath, mapping) + val evaluated = evaluator.evaluate(OSet(t)) + (evaluated.values(0).asInstanceOf[T], evaluated.evaluated.size) + } def check(targets: OSet[Target[_]], expected: OSet[Target[_]]) = { val evaluator = new Evaluator(workspacePath, mapping) @@ -113,6 +123,45 @@ object JavaCompileJarTests extends TestSuite{ val executed = %%('java, "-cp", workspacePath/'jar, "test.Foo")(workspacePath).out.string assert(executed == (31337 + 271828) + "\n") + + println("="*20 + "Run Main" + "="*20) + for(i <- 0 until 3){ + // Build.run is not cached, so every time we eval it it has to + // re-evaluate + val (runOutput, evalCount) = eval(Build.run("test.Foo")) + assert( + runOutput.out.string == (31337 + 271828) + "\n", + evalCount == 1 + ) + } + + val ex = intercept[ammonite.ops.ShelloutException]{ + eval(Build.run("test.BarFour")) + } + assert(ex.getMessage.contains("Could not find or load main class")) + + append( + sourceRootPath / "Bar.java", + """ + class BarFour{ + public static void main(String[] args){ + System.out.println("New Cls!"); + } + } + """ + ) + val (runOutput2, evalCount2) = eval(Build.run("test.BarFour")) + assert( + runOutput2.out.string == "New Cls!\n", + evalCount2 == 5 + ) + val (runOutput3, evalCount3) = eval(Build.run("test.BarFour")) + assert( + runOutput3.out.string == "New Cls!\n", + evalCount3 == 1 + ) + + } } } -- cgit v1.2.3