Skip to content

Commit 6465524

Browse files
committed
Factor out check file logic to FileDiff
1 parent b544090 commit 6465524

File tree

4 files changed

+88
-87
lines changed

4 files changed

+88
-87
lines changed

compiler/test/dotty/tools/dotc/printing/PrintingTest.scala

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dotty
22
package tools
33
package dotc
44

5+
import vulpix.FileDiff
56
import vulpix.TestConfiguration
67
import vulpix.TestConfiguration
78
import reporting.TestReporter
@@ -27,11 +28,10 @@ class PrintingTest {
2728

2829

2930
private def compileFile(path: JPath): Boolean = {
30-
val baseFilePath = path.toAbsolutePath.toString.stripSuffix(".scala")
31-
val outFilePath = baseFilePath + ".out"
31+
val baseFilePath = path.toString.stripSuffix(".scala")
3232
val checkFilePath = baseFilePath + ".check"
33-
val ps = new PrintStream(new File(outFilePath))
34-
val reporter = TestReporter.reporter(ps, INFO)
33+
val byteStream = new ByteArrayOutputStream()
34+
val reporter = TestReporter.reporter(new PrintStream(byteStream), INFO)
3535

3636
try {
3737
Main.process((path.toString::options).toArray, reporter, null)
@@ -40,31 +40,10 @@ class PrintingTest {
4040
println(s"Compile $path exception:")
4141
e.printStackTrace()
4242
}
43-
finally ps.close()
44-
45-
val actual = fileContent(outFilePath)
46-
val expected = fileContent(checkFilePath)
47-
48-
val success =
49-
actual.length == expected.length &&
50-
(actual, expected).zipped.forall(_ == _)
51-
52-
success || {
53-
println(
54-
s"""|Output from '$outFilePath' did not match check file. Actual output:
55-
|${actual.mkString(EOL)}
56-
|""".stripMargin + "\n")
57-
58-
println(
59-
s"""Test output dumped in: ${outFilePath}
60-
| See diff of the checkfile
61-
| > diff $checkFilePath $outFilePath
62-
| Replace checkfile with current output output
63-
| > mv $outFilePath $checkFilePath
64-
""".stripMargin)
65-
66-
false
67-
}
43+
44+
val actualLines = byteStream.toString("UTF-8").split("\\r?\\n")
45+
46+
FileDiff.checkAndDump(path.toString, actualLines, checkFilePath)
6847
}
6948

7049
@Test

compiler/test/dotty/tools/dotc/transform/PatmatExhaustivityTest.scala

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@ package tools
33
package dotc
44
package transform
55

6+
import vulpix.FileDiff
67
import vulpix.TestConfiguration
8+
import reporting.TestReporter
9+
10+
import dotty.tools.io.Directory
711

812
import java.io._
913
import java.nio.file.{Path => JPath}
1014

1115
import scala.io.Source._
12-
import dotty.tools.io.Directory
1316
import org.junit.Test
14-
import reporting.TestReporter
15-
import vulpix.TestConfiguration
1617

1718
class PatmatExhaustivityTest {
1819
val testsDir = "tests/patmat"
1920
// stop-after: patmatexhaust-huge.scala crash compiler
2021
val options = List("-color:never", "-Ystop-after:crossCast", "-Ycheck-all-patmat", "-classpath", TestConfiguration.basicClasspath)
2122

22-
private def compileFile(path: JPath) = {
23+
private def compileFile(path: JPath): Boolean = {
2324
val stringBuffer = new StringWriter()
2425
val reporter = TestReporter.simplifiedReporter(new PrintWriter(stringBuffer))
2526

@@ -31,18 +32,18 @@ class PatmatExhaustivityTest {
3132
e.printStackTrace()
3233
}
3334

34-
val actual = stringBuffer.toString.trim.replaceAll("\\s+\n", "\n")
35-
val checkFilePath = path.toAbsolutePath.toString.stripSuffix(".scala") + ".check"
36-
val checkContent =
37-
if (new File(checkFilePath).exists)
38-
fromFile(checkFilePath).getLines().map(_.replaceAll("\\s+$", "")).mkString("\n").trim
39-
else ""
35+
val actualLines: Seq[String] = stringBuffer.toString.trim.replaceAll("\\s+\n", "\n") match {
36+
case "" => Nil
37+
case s => s.split("\\r?\\n")
38+
}
39+
val baseFilePath = path.toString.stripSuffix(".scala")
40+
val checkFilePath = baseFilePath + ".check"
4041

41-
(path, checkContent, actual)
42+
FileDiff.checkAndDump(path.toString, actualLines, checkFilePath)
4243
}
4344

4445
/** A single test with multiple files grouped in a folder */
45-
private def compileDir(path: JPath) = {
46+
private def compileDir(path: JPath): Boolean = {
4647
val stringBuffer = new StringWriter()
4748
val reporter = TestReporter.simplifiedReporter(new PrintWriter(stringBuffer))
4849

@@ -58,14 +59,14 @@ class PatmatExhaustivityTest {
5859
e.printStackTrace()
5960
}
6061

61-
val actual = stringBuffer.toString.trim.replaceAll("\\s+\n", "\n")
62+
val actualLines: Seq[String] = stringBuffer.toString.trim.replaceAll("\\s+\n", "\n") match {
63+
case "" => Nil
64+
case s => s.split("\\r?\\n")
65+
}
66+
6267
val checkFilePath = path + File.separator + "expected.check"
63-
val checkContent =
64-
if (new File(checkFilePath).exists)
65-
fromFile(checkFilePath).getLines().map(_.replaceAll("\\s+$", "")).mkString("\n").trim
66-
else ""
6768

68-
(path, checkContent, actual)
69+
FileDiff.checkAndDump(path.toString, actualLines, checkFilePath)
6970
}
7071

7172
@Test
@@ -79,15 +80,9 @@ class PatmatExhaustivityTest {
7980
compileFile(f.jpath)
8081
}
8182

82-
val failed = res.filter { case (_, expected, actual) => expected != actual }
83+
val failed = res.filter(!_)
8384
val ignored = Directory(testsDir).list.toList.filter(_.extension == "ignore")
8485

85-
failed.foreach { case (file, expected, actual) =>
86-
println(s"\n----------------- incorrect output for $file --------------\n" +
87-
s"Expected:\n---------\n$expected\n\nActual:\n-------\n$actual\n"
88-
)
89-
}
90-
9186
val msg = s"Total: ${res.length + ignored.length}, Failed: ${failed.length}, Ignored: ${ignored.length}"
9287

9388
assert(failed.length == 0, msg)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package dotty.tools.vulpix
2+
3+
import scala.io.Source
4+
import java.io.File
5+
import java.lang.System.{lineSeparator => EOL}
6+
7+
object FileDiff {
8+
def diffMessage(expectFile: String, actualFile: String): String =
9+
s"""Test output dumped in: $actualFile
10+
| See diff of the checkfile (`brew install icdiff` for colored diff)
11+
| > diff $expectFile $actualFile
12+
| Replace checkfile with current output output
13+
| > mv $actualFile $expectFile
14+
""".stripMargin
15+
16+
def check(sourceTitle: String, outputLines: Seq[String], checkFile: String): Option[String] = {
17+
val checkLines =
18+
if (!(new File(checkFile)).exists) Nil
19+
else Source.fromFile(checkFile, "UTF-8").getLines().toList
20+
21+
def linesMatch =
22+
outputLines.length == checkLines.length &&
23+
(outputLines, checkLines).zipped.forall(_ == _)
24+
25+
if (!linesMatch) Some(
26+
s"""|Output from '$sourceTitle' did not match check file. Actual output:
27+
|${outputLines.mkString(EOL)}
28+
|""".stripMargin + "\n")
29+
else None
30+
}
31+
32+
def dump(path: String, content: Seq[String]): Unit = {
33+
val outFile = dotty.tools.io.File(path)
34+
outFile.writeAll(content.mkString("", EOL, EOL))
35+
}
36+
37+
def checkAndDump(sourceTitle: String, actualLines: Seq[String], checkFilePath: String): Boolean =
38+
FileDiff.check(sourceTitle, actualLines, checkFilePath) match {
39+
case Some(msg) =>
40+
val outFilePath = checkFilePath + ".out"
41+
FileDiff.dump(outFilePath, actualLines)
42+
println(msg)
43+
println(FileDiff.diffMessage(checkFilePath, outFilePath))
44+
false
45+
case _ =>
46+
true
47+
}
48+
}

compiler/test/dotty/tools/vulpix/ParallelTesting.scala

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,17 @@ trait ParallelTesting extends RunnerOrchestration { self =>
228228
* If not, fails the test.
229229
*/
230230
final def diffTest(testSource: TestSource, checkFile: JFile, actual: List[String], reporters: Seq[TestReporter], logger: LoggedRunnable) = {
231-
val expected = Source.fromFile(checkFile, "UTF-8").getLines().toList
232-
for (msg <- diffMessage(testSource.title, actual, expected)) {
231+
for (msg <- FileDiff.check(testSource.title, actual, checkFile.getPath)) {
233232
onFailure(testSource, reporters, logger, Some(msg))
234-
dumpOutputToFile(checkFile, actual)
233+
234+
if (updateCheckFiles) {
235+
FileDiff.dump(checkFile.toPath.toString, actual)
236+
echo("Updated checkfile: " + checkFile.getPath)
237+
} else {
238+
val outFile = checkFile.toPath.resolveSibling(checkFile.toPath.getFileName + ".out").toString
239+
FileDiff.dump(outFile, actual)
240+
echo(FileDiff.diffMessage(checkFile.getPath, outFile))
241+
}
235242
}
236243
}
237244

@@ -568,24 +575,6 @@ trait ParallelTesting extends RunnerOrchestration { self =>
568575
this
569576
}
570577

571-
protected def dumpOutputToFile(checkFile: JFile, lines: Seq[String]): Unit = {
572-
if (updateCheckFiles) {
573-
val outFile = dotty.tools.io.File(checkFile.toPath)
574-
outFile.writeAll(lines.mkString("", EOL, EOL))
575-
echo("Updated checkfile: " + checkFile.getPath)
576-
} else {
577-
val outFile = dotty.tools.io.File(checkFile.toPath.resolveSibling(checkFile.toPath.getFileName + ".out"))
578-
outFile.writeAll(lines.mkString("", EOL, EOL))
579-
echo(
580-
s"""Test output dumped in: ${outFile.path}
581-
| See diff of the checkfile
582-
| > diff $checkFile $outFile
583-
| Replace checkfile with current output output
584-
| > mv $outFile $checkFile
585-
""".stripMargin)
586-
}
587-
}
588-
589578
/** Returns all files in directory or the file if not a directory */
590579
private def flattenFiles(f: JFile): Array[JFile] =
591580
if (f.isDirectory) f.listFiles.flatMap(flattenFiles)
@@ -611,12 +600,12 @@ trait ParallelTesting extends RunnerOrchestration { self =>
611600

612601
private def verifyOutput(checkFile: Option[JFile], dir: JFile, testSource: TestSource, warnings: Int, reporters: Seq[TestReporter], logger: LoggedRunnable) = {
613602
if (Properties.testsNoRun) addNoRunWarning()
614-
else runMain(testSource.runClassPath) match {
603+
else runMain(testSource.runClassPath) match {
615604
case Success(output) => checkFile match {
616605
case Some(file) if file.exists => diffTest(testSource, file, output.linesIterator.toList, reporters, logger)
617606
case _ =>
618607
}
619-
case Failure(output) =>
608+
case Failure(output) =>
620609
echo(s"Test '${testSource.title}' failed with output:")
621610
echo(output)
622611
failTestSource(testSource)
@@ -712,16 +701,6 @@ trait ParallelTesting extends RunnerOrchestration { self =>
712701
override def maybeFailureMessage(testSource: TestSource, reporters: Seq[TestReporter]): Option[String] = None
713702
}
714703

715-
def diffMessage(sourceTitle: String, outputLines: Seq[String], checkLines: Seq[String]): Option[String] = {
716-
def linesMatch =
717-
(outputLines, checkLines).zipped.forall(_ == _)
718-
719-
if (outputLines.length != checkLines.length || !linesMatch) Some(
720-
s"""|Output from '$sourceTitle' did not match check file. Actual output:
721-
|${outputLines.mkString(EOL)}
722-
|""".stripMargin + "\n")
723-
else None
724-
}
725704

726705
/** The `CompilationTest` is the main interface to `ParallelTesting`, it
727706
* can be instantiated via one of the following methods:

0 commit comments

Comments
 (0)