diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2018-02-02 20:45:41 -0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2018-02-02 20:51:04 -0800 |
commit | 27273750636acbbff1b37cf53bba76684f0b96bb (patch) | |
tree | af71defa4ea57b2ef70998c1546aa34321f38013 /core/test | |
parent | 2e50c963eecadcf7bd6ecd6956584ab099d5273a (diff) | |
download | mill-27273750636acbbff1b37cf53bba76684f0b96bb.tar.gz mill-27273750636acbbff1b37cf53bba76684f0b96bb.tar.bz2 mill-27273750636acbbff1b37cf53bba76684f0b96bb.zip |
consolidate test initialization logic into TestEvaluator
Diffstat (limited to 'core/test')
-rw-r--r-- | core/test/src/mill/define/CacherTests.scala | 10 | ||||
-rw-r--r-- | core/test/src/mill/eval/EvaluationTests.scala | 14 | ||||
-rw-r--r-- | core/test/src/mill/eval/FailureTests.scala | 43 | ||||
-rw-r--r-- | core/test/src/mill/eval/JavaCompileJarTests.scala | 37 | ||||
-rw-r--r-- | core/test/src/mill/util/TestEvaluator.scala | 38 |
5 files changed, 69 insertions, 73 deletions
diff --git a/core/test/src/mill/define/CacherTests.scala b/core/test/src/mill/define/CacherTests.scala index 60216c2e..7619e770 100644 --- a/core/test/src/mill/define/CacherTests.scala +++ b/core/test/src/mill/define/CacherTests.scala @@ -1,7 +1,7 @@ package mill.define -import mill.eval.Evaluator -import mill.util.{DummyLogger, TestUtil} +import ammonite.ops.pwd +import mill.util.{DummyLogger, TestEvaluator, TestUtil} import mill.util.Strict.Agg import mill.T import mill.eval.Result.Success @@ -26,11 +26,11 @@ object CacherTests extends TestSuite{ } val tests = Tests{ - def eval[T <: mill.Module, V](mapping: T, v: Task[V]) + def eval[T <: TestUtil.TestBuild, V](mapping: T, v: Task[V]) (implicit discover: Discover[T], tp: TestPath) = { val workspace = ammonite.ops.pwd / 'target / 'workspace / tp.value - val evaluator = new Evaluator(workspace, ammonite.ops.pwd, mapping, discover, DummyLogger) - evaluator.evaluate(Agg(v)).values(0) + val evaluator = new TestEvaluator(mapping, workspace, pwd) + evaluator(v).right.get._1 } 'simpleDefIsCached - assert( diff --git a/core/test/src/mill/eval/EvaluationTests.scala b/core/test/src/mill/eval/EvaluationTests.scala index eb0c30c6..386d5340 100644 --- a/core/test/src/mill/eval/EvaluationTests.scala +++ b/core/test/src/mill/eval/EvaluationTests.scala @@ -4,18 +4,20 @@ package mill.eval import mill.util.TestUtil.{Test, test} import mill.define.{Discover, Graph, Target, Task} import mill.{Module, T} -import mill.util.{DummyLogger, TestGraphs, TestUtil} +import mill.util.{DummyLogger, TestEvaluator, TestGraphs, TestUtil} import mill.util.Strict.Agg import utest._ import utest.framework.TestPath import mill.util.TestEvaluator.implicitDisover - +import ammonite.ops._ object EvaluationTests extends TestSuite{ - class Checker[T <: mill.Module](module: T)(implicit tp: TestPath, discover: Discover[T]) { - val workspace = ammonite.ops.pwd / 'target / 'workspace / tp.value - ammonite.ops.rm(ammonite.ops.Path(workspace, ammonite.ops.pwd)) + class Checker[T <: TestUtil.TestBuild](module: T) + (implicit tp: TestPath, discover: Discover[T]) { + val workspace = pwd / 'target / 'workspace / tp.value + rm(Path(workspace, pwd)) // Make sure data is persisted even if we re-create the evaluator each time - def evaluator = new Evaluator(workspace, ammonite.ops.pwd, module, discover, DummyLogger) + + def evaluator = new TestEvaluator(module, workspace, pwd).evaluator def apply(target: Task[_], expValue: Any, expEvaled: Agg[Task[_]], diff --git a/core/test/src/mill/eval/FailureTests.scala b/core/test/src/mill/eval/FailureTests.scala index 6a9b684f..86eccb3f 100644 --- a/core/test/src/mill/eval/FailureTests.scala +++ b/core/test/src/mill/eval/FailureTests.scala @@ -1,8 +1,7 @@ package mill.eval -import mill.define.{Discover, Target} -import mill.util.DummyLogger -import mill.util.Strict.Agg +import mill.util.TestEvaluator +import ammonite.ops.pwd import utest._ import utest.framework.TestPath import mill.util.TestEvaluator.implicitDisover @@ -11,34 +10,16 @@ object FailureTests extends TestSuite{ def workspace(implicit tp: TestPath) = { ammonite.ops.pwd / 'target / 'workspace / 'failure / implicitly[TestPath].value } - class Checker[T <: mill.Module](module: T)(implicit tp: TestPath, discover: Discover[T]){ - val evaluator = new Evaluator(workspace, ammonite.ops.pwd, module, discover, DummyLogger) - def apply(target: Target[_], expectedFailCount: Int, expectedRawValues: Seq[Result[_]]) = { - - val res = evaluator.evaluate(Agg(target)) - - val cleaned = res.rawValues.map{ - case Result.Exception(ex, _) => Result.Exception(ex, Nil) - case x => x - } - - assert( - cleaned == expectedRawValues, - res.failing.keyCount == expectedFailCount - ) - - } - } val tests = Tests{ val graphs = new mill.util.TestGraphs() import graphs._ 'evaluateSingle - { ammonite.ops.rm(ammonite.ops.Path(workspace, ammonite.ops.pwd)) - val check = new Checker(singleton) - check( + val check = new TestEvaluator(singleton, workspace, pwd) + check.fail( target = singleton.single, expectedFailCount = 0, expectedRawValues = Seq(Result.Success(0)) @@ -46,7 +27,7 @@ object FailureTests extends TestSuite{ singleton.single.failure = Some("lols") - check( + check.fail( target = singleton.single, expectedFailCount = 1, expectedRawValues = Seq(Result.Failure("lols")) @@ -54,7 +35,7 @@ object FailureTests extends TestSuite{ singleton.single.failure = None - check( + check.fail( target = singleton.single, expectedFailCount = 0, expectedRawValues = Seq(Result.Success(0)) @@ -65,16 +46,16 @@ object FailureTests extends TestSuite{ singleton.single.exception = Some(ex) - check( + check.fail( target = singleton.single, expectedFailCount = 1, expectedRawValues = Seq(Result.Exception(ex, Nil)) ) } 'evaluatePair - { + val check = new TestEvaluator(pair, workspace, pwd) ammonite.ops.rm(ammonite.ops.Path(workspace, ammonite.ops.pwd)) - val check = new Checker(pair) - check( + check.fail( pair.down, expectedFailCount = 0, expectedRawValues = Seq(Result.Success(0)) @@ -82,7 +63,7 @@ object FailureTests extends TestSuite{ pair.up.failure = Some("lols") - check( + check.fail( pair.down, expectedFailCount = 1, expectedRawValues = Seq(Result.Skipped) @@ -90,7 +71,7 @@ object FailureTests extends TestSuite{ pair.up.failure = None - check( + check.fail( pair.down, expectedFailCount = 0, expectedRawValues = Seq(Result.Success(0)) @@ -98,7 +79,7 @@ object FailureTests extends TestSuite{ pair.up.exception = Some(new IndexOutOfBoundsException()) - check( + check.fail( pair.down, expectedFailCount = 1, expectedRawValues = Seq(Result.Skipped) diff --git a/core/test/src/mill/eval/JavaCompileJarTests.scala b/core/test/src/mill/eval/JavaCompileJarTests.scala index 36bbe5cd..0137f372 100644 --- a/core/test/src/mill/eval/JavaCompileJarTests.scala +++ b/core/test/src/mill/eval/JavaCompileJarTests.scala @@ -6,11 +6,11 @@ import mill.define.{Discover, Input, Target, Task} import mill.modules.Jvm import mill.util.Ctx.DestCtx import mill.{Module, T} -import mill.util.{DummyLogger, Loose, TestUtil} +import mill.util.{DummyLogger, Loose, TestEvaluator, TestUtil} import mill.util.Strict.Agg import utest._ import mill._ - +import TestEvaluator.implicitDisover object JavaCompileJarTests extends TestSuite{ def compileAll(sources: Seq[PathRef])(implicit ctx: DestCtx) = { mkdir(ctx.dest) @@ -50,31 +50,12 @@ object JavaCompileJarTests extends TestSuite{ import Build._ def eval[T](t: Task[T]) = { - val evaluator = new Evaluator(workspacePath, pwd, Build, Discover[Build.type], DummyLogger) - val evaluated = evaluator.evaluate(Agg(t)) - - if (evaluated.failing.keyCount == 0){ - Right(Tuple2( - evaluated.rawValues(0).asInstanceOf[Result.Success[T]].value, - evaluated.evaluated.collect{ - case t: Target[_] if Build.millInternal.targets.contains(t) => t - case t: mill.define.Command[_] => t - }.size - )) - }else{ - Left(evaluated.failing.lookupKey(evaluated.failing.keys().next).items.next()) - } - + val evaluator = new TestEvaluator(Build, workspacePath, pwd) + evaluator.apply(t) } def check(targets: Agg[Task[_]], expected: Agg[Task[_]]) = { - val evaluator = new Evaluator(workspacePath, pwd, Build, Discover[Build.type], DummyLogger) - - val evaluated = evaluator.evaluate(targets) - .evaluated - .flatMap(_.asTarget) - .filter(Build.millInternal.targets.contains) - .filter(!_.isInstanceOf[Input[_]]) - assert(evaluated == expected) + val evaluator = new TestEvaluator(Build, workspacePath, pwd) + evaluator.check(targets, expected) } def append(path: Path, txt: String) = ammonite.ops.write.append(path, txt) @@ -144,7 +125,7 @@ object JavaCompileJarTests extends TestSuite{ val Right((runOutput, evalCount)) = eval(Build.run("test.Foo")) assert( runOutput.out.string == (31337 + 271828) + "\n", - evalCount == 2 + evalCount == 1 ) } @@ -165,12 +146,12 @@ object JavaCompileJarTests extends TestSuite{ val Right((runOutput2, evalCount2)) = eval(Build.run("test.BarFour")) assert( runOutput2.out.string == "New Cls!\n", - evalCount2 == 4 + evalCount2 == 3 ) val Right((runOutput3, evalCount3)) = eval(Build.run("test.BarFour")) assert( runOutput3.out.string == "New Cls!\n", - evalCount3 == 2 + evalCount3 == 1 ) } } diff --git a/core/test/src/mill/util/TestEvaluator.scala b/core/test/src/mill/util/TestEvaluator.scala index 41830d48..3b34e27d 100644 --- a/core/test/src/mill/util/TestEvaluator.scala +++ b/core/test/src/mill/util/TestEvaluator.scala @@ -1,19 +1,24 @@ package mill.util -import ammonite.ops.Path +import ammonite.ops.{Path, pwd} import mill.define.Discover.applyImpl import mill.define.{Discover, Input, Target, Task} import mill.eval.{Evaluator, Result} import mill.util.Strict.Agg +import utest.assert + import language.experimental.macros object TestEvaluator{ implicit def implicitDisover[T]: Discover[T] = macro applyImpl[T] + val externalBasePath = pwd / 'target / 'external } class TestEvaluator[T <: TestUtil.TestBuild](module: T, workspacePath: Path, basePath: Path) (implicit discover: Discover[T]){ - val evaluator = new Evaluator(workspacePath, basePath, module, discover, DummyLogger) + val evaluator = new Evaluator( + workspacePath, basePath, TestEvaluator.externalBasePath, module, discover, DummyLogger + ) // val evaluator = new Evaluator(workspacePath, basePath, module, discover, new PrintLogger(true, ammonite.util.Colors.Default, System.out, System.out, System.err)) def apply[T](t: Task[T]): Either[Result.Failing, (T, Int)] = { val evaluated = evaluator.evaluate(Agg(t)) @@ -23,7 +28,9 @@ class TestEvaluator[T <: TestUtil.TestBuild](module: T, Tuple2( evaluated.rawValues.head.asInstanceOf[Result.Success[T]].value, evaluated.evaluated.collect { - case t: Target[_] if module.millInternal.targets.contains(t) && !t.isInstanceOf[Input[_]] => t + case t: Target[_] + if module.millInternal.targets.contains(t) + && !t.isInstanceOf[Input[_]] => t case t: mill.define.Command[_] => t }.size )) @@ -33,4 +40,29 @@ class TestEvaluator[T <: TestUtil.TestBuild](module: T, } } + def fail(target: Target[_], expectedFailCount: Int, expectedRawValues: Seq[Result[_]]) = { + + val res = evaluator.evaluate(Agg(target)) + + val cleaned = res.rawValues.map{ + case Result.Exception(ex, _) => Result.Exception(ex, Nil) + case x => x + } + + assert( + cleaned == expectedRawValues, + res.failing.keyCount == expectedFailCount + ) + + } + + def check(targets: Agg[Task[_]], expected: Agg[Task[_]]) = { + val evaluated = evaluator.evaluate(targets) + .evaluated + .flatMap(_.asTarget) + .filter(module.millInternal.targets.contains) + .filter(!_.isInstanceOf[Input[_]]) + assert(evaluated == expected) + } + } |