From e086a4440b51a39f0f6d953b06f8efa1c063b571 Mon Sep 17 00:00:00 2001 From: Philipp Haller Date: Fri, 15 Feb 2008 16:10:50 +0000 Subject: Added support for 'neg' tests to new partest --- .../scala/tools/partest/nest/DiffPrint.java | 72 +++++++++++++---- .../scala/tools/partest/nest/FileManager.scala | 16 +--- .../scala/tools/partest/nest/NestRunner.scala | 1 + src/partest/scala/tools/partest/nest/Worker.scala | 89 ++++++++++++++++------ 4 files changed, 128 insertions(+), 50 deletions(-) (limited to 'src/partest') diff --git a/src/partest/scala/tools/partest/nest/DiffPrint.java b/src/partest/scala/tools/partest/nest/DiffPrint.java index 7fc8eacb0d..cb02210eb9 100644 --- a/src/partest/scala/tools/partest/nest/DiffPrint.java +++ b/src/partest/scala/tools/partest/nest/DiffPrint.java @@ -52,8 +52,8 @@ public class DiffPrint { are provided as well. */ public static abstract class Base { - protected Base(Object[] a,Object[] b) { - outfile = new PrintWriter(new OutputStreamWriter(System.out)); + protected Base(Object[] a,Object[] b, Writer w) { + outfile = new PrintWriter(w); file0 = a; file1 = b; } @@ -202,8 +202,8 @@ public class DiffPrint { */ public static class NormalPrint extends Base { - public NormalPrint(Object[] a,Object[] b) { - super(a,b); + public NormalPrint(Object[] a,Object[] b, Writer w) { + super(a,b,w); } /** Print a hunk of a normal diff. @@ -244,8 +244,8 @@ public class DiffPrint { */ public static class EdPrint extends Base { - public EdPrint(Object[] a,Object[] b) { - super(a,b); + public EdPrint(Object[] a,Object[] b, Writer w) { + super(a,b,w); } /** Print a hunk of an ed diff */ @@ -303,8 +303,8 @@ public class DiffPrint { protected int context = 3; - public ContextPrint(Object[] a,Object[] b) { - super(a,b); + public ContextPrint(Object[] a,Object[] b, Writer w) { + super(a,b,w); } protected void print_context_label (String mark, File inf, String label) { @@ -425,8 +425,8 @@ public class DiffPrint { */ public static class UnifiedPrint extends ContextPrint { - public UnifiedPrint(Object[] a,Object[] b) { - super(a,b); + public UnifiedPrint(Object[] a,Object[] b, Writer w) { + super(a,b,w); } public void print_header(String filea,String fileb) { @@ -560,15 +560,59 @@ public class DiffPrint { System.err.println("No differences"); else { Base p; + Writer w = new OutputStreamWriter(System.out); switch (style) { case 'e': - p = new EdPrint(a,b); break; + p = new EdPrint(a,b,w); break; case 'c': - p = new ContextPrint(a,b); break; + p = new ContextPrint(a,b,w); break; case 'u': - p = new UnifiedPrint(a,b); break; + p = new UnifiedPrint(a,b,w); break; default: - p = new NormalPrint(a,b); + p = new NormalPrint(a,b,w); + } + p.print_header(filea,fileb); + p.print_script(script); + } + } + + public static void doDiff(String[] argv, Writer w) throws IOException { + String filea = argv[argv.length - 2]; + String fileb = argv[argv.length - 1]; + String[] a = slurp(filea); + String[] b = slurp(fileb); + Diff d = new Diff(a,b); + char style = 'n'; + for (int i = 0; i < argv.length - 2; ++i) { + String f = argv[i]; + if (f.startsWith("-")) { + for (int j = 1; j < f.length(); ++j) { + switch (f.charAt(j)) { + case 'e': // Ed style + style = 'e'; break; + case 'c': // Context diff + style = 'c'; break; + case 'u': + style = 'u'; break; + } + } + } + } + boolean reverse = style == 'e'; + Diff.change script = d.diff_2(reverse); + if (script == null) + w.write("No differences\n"); + else { + Base p; + switch (style) { + case 'e': + p = new EdPrint(a,b,w); break; + case 'c': + p = new ContextPrint(a,b,w); break; + case 'u': + p = new UnifiedPrint(a,b,w); break; + default: + p = new NormalPrint(a,b,w); } p.print_header(filea,fileb); p.print_script(script); diff --git a/src/partest/scala/tools/partest/nest/FileManager.scala b/src/partest/scala/tools/partest/nest/FileManager.scala index ade5dd46b5..b55ebdcfea 100644 --- a/src/partest/scala/tools/partest/nest/FileManager.scala +++ b/src/partest/scala/tools/partest/nest/FileManager.scala @@ -4,8 +4,7 @@ package scala.tools.partest.nest -import java.io.{File, FilenameFilter, ByteArrayOutputStream, PrintStream, - IOException, BufferedOutputStream} +import java.io.{File, FilenameFilter, IOException, StringWriter} import java.net.URI object FileManager { @@ -171,17 +170,10 @@ elif [ -d "$PREFIX/bin" ]; then def compareFiles(f1: File, f2: File): String = { var res = "" try { - val diffStream = new ByteArrayOutputStream - val diffOutput = new PrintStream( - new BufferedOutputStream(diffStream), true) - System.setOut(diffOutput) - System.setErr(diffOutput) + val diffWriter = new StringWriter val args = Array(f1.getCanonicalPath(), f2.getCanonicalPath()) - DiffPrint.main(args) - System.setOut(System.out) - System.setErr(System.err) - - res = diffStream.toString + DiffPrint.doDiff(args, diffWriter) + res = diffWriter.toString if (res.startsWith("No")) res = "" } catch { diff --git a/src/partest/scala/tools/partest/nest/NestRunner.scala b/src/partest/scala/tools/partest/nest/NestRunner.scala index 09d0bf0af8..f9f2a44af4 100644 --- a/src/partest/scala/tools/partest/nest/NestRunner.scala +++ b/src/partest/scala/tools/partest/nest/NestRunner.scala @@ -177,6 +177,7 @@ object NestRunner { */ def testCheckAll(): (Int, Int) = { val results = List(runTests("pos", posCheck, "Testing compiler (on files whose compilation should succeed)"), + runTests("neg", negCheck, "Testing compiler (on files whose compilation should fail)"), runTests("run", runCheck, "Testing JVM backend"), runTests("jvm", jvmCheck, "Testing JVM backend"), runTests("jvm5", jvm5Check, "Testing JVM backend")) diff --git a/src/partest/scala/tools/partest/nest/Worker.scala b/src/partest/scala/tools/partest/nest/Worker.scala index 662a8708d2..bf02c1fb9b 100644 --- a/src/partest/scala/tools/partest/nest/Worker.scala +++ b/src/partest/scala/tools/partest/nest/Worker.scala @@ -62,15 +62,6 @@ class Worker extends Actor { NestUI.normal("]\n", printer) } - abstract class TestRun(file: File) { - def beforeRun(): Unit - def runTest(): Unit - def afterRun(): Unit - def doAll() { - beforeRun(); runTest(); afterRun() - } - } - var log = "" def execTest(outDir: File, logFile: File) { @@ -221,6 +212,15 @@ class Worker extends Actor { (files.length-errors, errors) } + abstract class TestRun(file: File) { + def beforeRun(): Unit + def runTest(): Unit + def afterRun(): Unit + def doAll() { + beforeRun(); runTest(); afterRun() + } + } + /** Runs a list of tests. * * @param kind The test kind (pos, neg, run, etc.) @@ -230,9 +230,16 @@ class Worker extends Actor { val compileMgr = new CompileManager var errors = 0 var succeeded = true + var diff = "" + var log = "" abstract class CompileTestRun(file: File, wr: PrintWriter) extends TestRun(file) { - def beforeRun() { succeeded = true; printInfoStart(file, wr) } + def beforeRun() { + succeeded = true; + diff = "" + log = "" + printInfoStart(file, wr) + } def afterRun() { printInfoEnd(succeeded, wr) } } @@ -260,24 +267,58 @@ class Worker extends Actor { (files.length-errors, errors) } case "neg" => { - def negTestRun(file: File, wr: PrintWriter, log: File) = new CompileTestRun(file, wr) { - def runTest() { - if (!compileMgr.shouldFailCompile(file, kind, log)) { + for (file <- files) { + // when option "--failed" is provided + // execute test only if log file is present + // (which means it failed before) + val logFile = getLogFile(file, kind) + if (!NestRunner.failed || (logFile.exists && logFile.canRead)) { + val swr = new StringWriter + val wr = new PrintWriter(swr) + succeeded = true; diff = ""; log = "" + printInfoStart(file, wr) + + if (!compileMgr.shouldFailCompile(file, kind, logFile)) { succeeded = false errors += 1 - } //TODO: else compare log file to check file + } else { //TODO: else compare log file to check file + val fileBase: String = basename(file.getName) + val dir = file.getParentFile + val outDir = new File(dir, fileBase + "-" + kind + ".obj") + + NestUI.verbose("comparing output with check file...") + diff = compareOutput(dir, fileBase, kind, logFile) + if (!diff.equals("")) { + NestUI.verbose("output differs from log file\n") + succeeded = false + errors += 1 + } + + // delete output dir + FileManager.deleteRecursive(outDir) + + // delete log file only if test was successful + if (succeeded) + FileManager.deleteRecursive(logFile) + } + printInfoEnd(succeeded, wr) + wr.flush() + swr.flush() + NestUI.normal(swr.toString) + + if (!succeeded && NestRunner.showDiff) NestUI.normal(diff) + if (!succeeded && NestRunner.showLog) { + // output log file + val logReader = new FileReader(logFile) + val logWriter = new StringWriter + val logAppender = new StreamAppender(logReader, logWriter) + logAppender.start() + logAppender.join() + val log = logWriter.toString + NestUI.normal(log) + } } } - for (file <- files) { - val logFile = getLogFile(file, kind) - val swr = new StringWriter - val wr = new PrintWriter(swr) - val testRun = negTestRun(file, wr, logFile) - testRun.doAll() - wr.flush() - swr.flush() - NestUI.normal(swr.toString) - } NestUI.verbose("finished testing "+kind+" with "+errors+" errors") NestUI.verbose("created "+compileMgr.numSeparateCompilers+" separate compilers") (files.length-errors, errors) -- cgit v1.2.3