Skip to content

Fix printed file path in PrintingTest.scala #8330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions compiler/test/dotty/tools/dotc/printing/PrintingTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ class PrintingTest {
val testsDir = "tests/printing"
val options = List("-Xprint:typer", "-color:never", "-classpath", TestConfiguration.basicClasspath)

private def fileContent(filePath: String): List[String] =
if (new File(filePath).exists)
Source.fromFile(filePath, "UTF-8").getLines().toList
else Nil


private def compileFile(path: JPath): Boolean = {
val baseFilePath = path.toString.stripSuffix(".scala")
val checkFilePath = baseFilePath + ".check"
Expand All @@ -42,8 +36,20 @@ class PrintingTest {
}

val actualLines = byteStream.toString("UTF-8").split("\\r?\\n")

FileDiff.checkAndDump(path.toString, actualLines.toIndexedSeq, checkFilePath)
// 'options' includes option '-Xprint:typer' so the first output line
// looks similar to "result of tests/printing/i620.scala after typer:";
// check files use slashes as file separators (Unix) but running tests
// on Windows produces backslashes.
// NB. option '-Xprint:<..>' can specify several phases.
val filteredLines =
if (config.Properties.isWin)
actualLines.map(line =>
if (line.startsWith("result of")) line.replaceAll("\\\\", "/") else line
)
else
actualLines

FileDiff.checkAndDump(path.toString, filteredLines.toIndexedSeq, checkFilePath)
}

@Test
Expand Down
10 changes: 8 additions & 2 deletions compiler/test/dotty/tools/vulpix/FileDiff.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dotty.tools.vulpix
import scala.io.Source
import java.io.File
import java.lang.System.{lineSeparator => EOL}
import java.nio.file.{Files, Paths}

object FileDiff {
def diffMessage(expectFile: String, actualFile: String): String =
Expand Down Expand Up @@ -34,15 +35,20 @@ object FileDiff {
outFile.writeAll(content.mkString("", EOL, EOL))
}

def checkAndDump(sourceTitle: String, actualLines: Seq[String], checkFilePath: String): Boolean =
def checkAndDump(sourceTitle: String, actualLines: Seq[String], checkFilePath: String): Boolean = {
val outFilePath = checkFilePath + ".out"
FileDiff.check(sourceTitle, actualLines, checkFilePath) match {
case Some(msg) =>
val outFilePath = checkFilePath + ".out"
FileDiff.dump(outFilePath, actualLines)
println(msg)
println(FileDiff.diffMessage(checkFilePath, outFilePath))
false
case _ =>
val jOutFilePath = Paths.get(outFilePath)
if (Files.exists(jOutFilePath))
try { Files.delete(jOutFilePath) } catch { case _: Exception => () }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen if we leave out the code here? If they are really needed, it seems the best place is inside the method dump? BTW, Files. deleteIfExists will make the code shorter (maybe remove the try/catch).

Copy link
Contributor Author

@michelou michelou Feb 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a scenario to explain why I would not leave out the highlighted code.

Given the test case from command sbt "testOnly dotty.tools.dotc.PrintingTest",

  1. I run the above command and it fails.
    I can compare the output file (suffx .check.out) with the check file (suffix .check).
  2. I fix the issue (and compile the modified source files).
  3. I rerun the above command and it succeeds.
    The output file (suffx .check.out) is still there (generated by step 1) despite a successful execution.

Yes, using Files.deleteIfExists is better.

true
}
}

}