From be9b77ee1319d3597d7d83cd858367803845c46e Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Sun, 29 Oct 2017 15:39:13 -0700 Subject: First pass at making `javac` example an automated test. Still doesn't pass... --- src/test/examples/javac/resources/hello.txt | 1 + src/test/examples/javac/src/Bar.java | 4 + src/test/examples/javac/src/Foo.java | 7 ++ src/test/resources/example/resources/hello.txt | 1 - src/test/resources/example/src/Bar.java | 4 - src/test/resources/example/src/Foo.java | 7 -- src/test/scala/forge/IntegrationTests.scala | 110 +++++++++++++++++++++++++ src/test/scala/forge/Main.scala | 44 +--------- 8 files changed, 123 insertions(+), 55 deletions(-) create mode 100644 src/test/examples/javac/resources/hello.txt create mode 100644 src/test/examples/javac/src/Bar.java create mode 100644 src/test/examples/javac/src/Foo.java delete mode 100644 src/test/resources/example/resources/hello.txt delete mode 100644 src/test/resources/example/src/Bar.java delete mode 100644 src/test/resources/example/src/Foo.java create mode 100644 src/test/scala/forge/IntegrationTests.scala (limited to 'src/test') diff --git a/src/test/examples/javac/resources/hello.txt b/src/test/examples/javac/resources/hello.txt new file mode 100644 index 00000000..5e1c309d --- /dev/null +++ b/src/test/examples/javac/resources/hello.txt @@ -0,0 +1 @@ +Hello World \ No newline at end of file diff --git a/src/test/examples/javac/src/Bar.java b/src/test/examples/javac/src/Bar.java new file mode 100644 index 00000000..4e30c89b --- /dev/null +++ b/src/test/examples/javac/src/Bar.java @@ -0,0 +1,4 @@ +package test; +public class Bar{ + static int value = 271828; +} \ No newline at end of file diff --git a/src/test/examples/javac/src/Foo.java b/src/test/examples/javac/src/Foo.java new file mode 100644 index 00000000..a5ba9e2a --- /dev/null +++ b/src/test/examples/javac/src/Foo.java @@ -0,0 +1,7 @@ +package test; +public class Foo{ + static int value = 31337; + public static void main(String[] args){ + System.out.println(value); + } +} \ No newline at end of file diff --git a/src/test/resources/example/resources/hello.txt b/src/test/resources/example/resources/hello.txt deleted file mode 100644 index 5e1c309d..00000000 --- a/src/test/resources/example/resources/hello.txt +++ /dev/null @@ -1 +0,0 @@ -Hello World \ No newline at end of file diff --git a/src/test/resources/example/src/Bar.java b/src/test/resources/example/src/Bar.java deleted file mode 100644 index 4e30c89b..00000000 --- a/src/test/resources/example/src/Bar.java +++ /dev/null @@ -1,4 +0,0 @@ -package test; -public class Bar{ - static int value = 271828; -} \ No newline at end of file diff --git a/src/test/resources/example/src/Foo.java b/src/test/resources/example/src/Foo.java deleted file mode 100644 index a5ba9e2a..00000000 --- a/src/test/resources/example/src/Foo.java +++ /dev/null @@ -1,7 +0,0 @@ -package test; -public class Foo{ - static int value = 31337; - public static void main(String[] args){ - System.out.println(value); - } -} \ No newline at end of file diff --git a/src/test/scala/forge/IntegrationTests.scala b/src/test/scala/forge/IntegrationTests.scala new file mode 100644 index 00000000..5bf8a032 --- /dev/null +++ b/src/test/scala/forge/IntegrationTests.scala @@ -0,0 +1,110 @@ +package forge + +import java.io.FileOutputStream +import java.nio.file.attribute.FileTime +import java.time.Instant +import java.util.jar.JarEntry + +import ammonite.ops.{Path, ls, pwd, read} +import forge.util.{Args, OSet, PathRef} +import utest._ + +object IntegrationTests extends TestSuite{ + def compileAll(sources: Target[Seq[PathRef]]) = { + new Target.Subprocess( + Seq(sources), + args => + Seq("javac") ++ + args[Seq[PathRef]](0).map(_.path.toString) ++ + Seq("-d", args.dest.toString) + ).map(_.dest) + } + + def list(root: Target[PathRef]): Target[Seq[PathRef]] = { + root.map(x => ls.rec(x.path).map(PathRef(_))) + } + case class jarUp(roots: Target[PathRef]*) extends Target[PathRef]{ + + val inputs = roots + def evaluate(args: Args): PathRef = { + + val output = new java.util.jar.JarOutputStream(new FileOutputStream(args.dest.toIO)) + for{ + root0 <- args.args + root = root0.asInstanceOf[PathRef] + + path <- ls.rec(root.path) + if path.isFile + }{ + val relative = path.relativeTo(root.path) + output.putNextEntry(new JarEntry(relative.toString)) + output.write(read.bytes(path)) + } + output.close() + PathRef(args.dest) + } + + + } + val tests = Tests{ + 'javac{ + object Build{ + val sourceRootPath = pwd / 'src / 'test / 'examples / 'javac / 'src + val resourceRootPath = pwd / 'src / 'test / 'examples / 'javac / 'resources + val sourceRoot = Target.path(sourceRootPath) + val resourceRoot = Target.path(resourceRootPath) + val allSources = list(sourceRoot) + val classFiles = compileAll(allSources) + val jar = jarUp(resourceRoot, classFiles) + } + import Build._ + val mapping = Discovered.mapping(Build) + + def check(targets: OSet[Target[_]], expected: OSet[Target[_]]) = { + val evaluator = new Evaluator(pwd / 'target / 'workspace / 'javac, mapping) + val evaluated = evaluator.evaluate(targets).evaluated.filter(mapping.contains) + pprint.log(evaluated.map(mapping)) + assert(evaluated == expected) + } + + def touch(path: Path) = { + java.nio.file.Files.setLastModifiedTime(path.toNIO, FileTime.from(Instant.now())) + } + + check( + targets = OSet(jar), + expected = OSet(resourceRoot, sourceRoot, allSources, classFiles, jar) + ) + + check(targets = OSet(jar), expected = OSet()) + + touch(Build.resourceRootPath / "hello.txt") + + check( + targets = OSet(jar), + expected = OSet(resourceRoot, jar) + ) + + check(targets = OSet(jar), expected = OSet()) + + touch(Build.sourceRootPath / "Foo.java") + + check( + targets = OSet(jar), + expected = OSet(sourceRoot, allSources, classFiles) + ) + + touch(Build.sourceRootPath / "Bar.java") + touch(Build.resourceRootPath / "hello.txt") + + check( + targets = OSet(classFiles), + expected = OSet(sourceRoot, allSources, classFiles) + ) + check( + targets = OSet(jar), + expected = OSet(resourceRoot, jar) + ) + } + } +} diff --git a/src/test/scala/forge/Main.scala b/src/test/scala/forge/Main.scala index 3f5732c9..51aaa3bd 100644 --- a/src/test/scala/forge/Main.scala +++ b/src/test/scala/forge/Main.scala @@ -6,53 +6,11 @@ import collection.JavaConverters._ import ammonite.ops._ import forge.util.{Args, OSet, PathRef} object Main{ - val sourceRoot = Target.path(pwd / 'src / 'test / 'resources / 'example / 'src) - val resourceRoot = Target.path(pwd / 'src / 'test / 'resources / 'example / 'resources) - val allSources = list(sourceRoot) - val classFiles = compileAll(allSources) - val jar = jarUp(resourceRoot, classFiles) + def main(args: Array[String]): Unit = { - val mapping = Discovered.mapping(Main) - val evaluator = new Evaluator(pwd / 'target / 'workspace / 'main, mapping) - val res = evaluator.evaluate(OSet(jar)) - println(res.evaluated.collect(mapping)) - } - def compileAll(sources: Target[Seq[PathRef]]) = { - new Target.Subprocess( - Seq(sources), - args => - Seq("javac") ++ - args[Seq[PathRef]](0).map(_.path.toString) ++ - Seq("-d", args.dest.toString) - ).map(_.dest) - } - def list(root: Target[PathRef]): Target[Seq[PathRef]] = { - root.map(x => ls.rec(x.path).map(PathRef(_))) } - case class jarUp(roots: Target[PathRef]*) extends Target[PathRef]{ - - val inputs = roots - def evaluate(args: Args): PathRef = { - val output = new java.util.jar.JarOutputStream(new FileOutputStream(args.dest.toIO)) - for{ - root0 <- args.args - root = root0.asInstanceOf[PathRef] - - path <- ls.rec(root.path) - if path.isFile - }{ - val relative = path.relativeTo(root.path) - output.putNextEntry(new JarEntry(relative.toString)) - output.write(read.bytes(path)) - } - output.close() - PathRef(args.dest) - } - - - } } -- cgit v1.2.3