Skip to content

Commit 193c70b

Browse files
committed
Make --update-check option work for partest, added "do not edit" header to generated.
1 parent 6cb8faa commit 193c70b

File tree

3 files changed

+66
-31
lines changed

3 files changed

+66
-31
lines changed

test/dotty/partest/DPConsoleRunner.scala

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ object DPConsoleRunner {
3939

4040
// console runner has a suite runner which creates a test runner for each test
4141
class DPConsoleRunner(args: String, extraJars: List[String]) extends ConsoleRunner(args) {
42-
println("ConsoleRunner options: " + args)
4342

4443
override val suiteRunner = new DPSuiteRunner (
4544
testSourcePath = optSourcePath getOrElse DPConfig.testRoot,
4645
fileManager = new DottyFileManager(extraJars),
4746
updateCheck = optUpdateCheck,
48-
failed = optFailed)
47+
failed = optFailed,
48+
consoleArgs = args)
4949

5050
override def run = {}
5151
def runPartest = super.run
@@ -62,6 +62,7 @@ class DPSuiteRunner(testSourcePath: String, // relative path, like "files", or "
6262
fileManager: DottyFileManager,
6363
updateCheck: Boolean,
6464
failed: Boolean,
65+
consoleArgs: String,
6566
javaCmdPath: String = PartestDefaults.javaCmd,
6667
javacCmdPath: String = PartestDefaults.javacCmd,
6768
scalacExtraArgs: Seq[String] = Seq.empty)
@@ -76,9 +77,11 @@ extends SuiteRunner(testSourcePath, fileManager, updateCheck, failed, javaCmdPat
7677
override def banner: String = {
7778
s"""|Welcome to Partest for Dotty! Partest version: ${Properties.versionNumberString}
7879
|Compiler under test: dotty.tools.dotc.Bench or dotty.tools.dotc.Main
79-
|Test root: ${PathSettings.srcDir}${File.separator}
80+
|Generated test sources: ${PathSettings.srcDir}${File.separator}
8081
|Test directories: ${DPConfig.testDirs.toList.mkString(", ")}
82+
|Debugging: failed tests have compiler output in test-kind.clog, run output in test-kind.log, class files in test-kind.obj
8183
|Parallel: ${DPConfig.runTestsInParallel}
84+
|Options: (use partest --help for usage information) ${consoleArgs}
8285
""".stripMargin
8386
}
8487

@@ -92,12 +95,13 @@ extends SuiteRunner(testSourcePath, fileManager, updateCheck, failed, javaCmdPat
9295
// Parts of test output might end up in the wrong file or get lost.
9396
Console.out.flush
9497
Console.err.flush
95-
val clog = SFile(runner.logFile).changeExtension("clog")
98+
val clog = runner.cLogFile
9699
val stream = new PrintStream(new FileOutputStream(clog.jfile), true)
97100
val result = Console.withOut(stream)({ Console.withErr(stream)({
98-
runner.run()
101+
val res = runner.run()
99102
Console.err.flush
100103
Console.out.flush
104+
res
101105
})})
102106
result match {
103107
// Append compiler output to transcript if compilation failed,
@@ -117,6 +121,8 @@ extends SuiteRunner(testSourcePath, fileManager, updateCheck, failed, javaCmdPat
117121
}
118122

119123
class DPTestRunner(testFile: File, suiteRunner: DPSuiteRunner) extends nest.Runner(testFile, suiteRunner) {
124+
val cLogFile = SFile(logFile).changeExtension("clog")
125+
120126
// override to provide DottyCompiler
121127
override def newCompiler = new dotty.partest.DPDirectCompiler(this)
122128

@@ -199,6 +205,38 @@ class DPTestRunner(testFile: File, suiteRunner: DPSuiteRunner) extends nest.Runn
199205
}
200206
}
201207

208+
// override to change check file updating to original file, not generated
209+
override def diffIsOk: Boolean = {
210+
// always normalize the log first
211+
normalizeLog()
212+
val diff = currentDiff
213+
// if diff is not empty, is update needed?
214+
val updating: Option[Boolean] = (
215+
if (diff == "") None
216+
else Some(suiteRunner.updateCheck)
217+
)
218+
pushTranscript(s"diff $logFile $checkFile")
219+
nextTestAction(updating) {
220+
case Some(true) =>
221+
val origCheck = SFile(checkFile.changeExtension("checksrc").fileLines(1))
222+
NestUI.echo("Updating original checkfile " + origCheck)
223+
origCheck writeAll file2String(logFile)
224+
genUpdated()
225+
case Some(false) =>
226+
// Get a word-highlighted diff from git if we can find it
227+
val bestDiff = if (updating.isEmpty) "" else {
228+
if (checkFile.canRead)
229+
gitDiff(logFile, checkFile) getOrElse {
230+
s"diff $logFile $checkFile\n$diff"
231+
}
232+
else diff
233+
}
234+
pushTranscript(bestDiff)
235+
genFail("output differs")
236+
case None => genPass() // redundant default case
237+
} getOrElse true
238+
}
239+
202240
// override because Dotty currently doesn't handle separate compilation well,
203241
// so we ignore groups (tests suffixed with _1 and _2)
204242
override def groupedFiles(sources: List[File]): List[List[File]] = {
@@ -223,4 +261,10 @@ class DPTestRunner(testFile: File, suiteRunner: DPSuiteRunner) extends nest.Runn
223261
// override to add dotty and scala jars to classpath
224262
override def extraClasspath = suiteRunner.fileManager.asInstanceOf[DottyFileManager].extraJarList ::: super.extraClasspath
225263

264+
// override to keep class files if failed and delete clog if ok
265+
override def cleanup = if (lastState.isOk) {
266+
logFile.delete
267+
cLogFile.delete
268+
Directory(outDir).deleteRecursively
269+
}
226270
}

test/partest

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
#!/usr/bin/env bash
2-
# partest error message references partest script to update check files, but
3-
# doesn't work for dotty because we don't know where tests came from.
2+
# partest error message references partest script to update check files
43

5-
if [ $1='--update-check' ];
6-
then
7-
echo """ERROR: Since dotty partest runs on generated files, please update the check
8-
files in the original location (run tests) or update the expected error count
9-
(neg tests) in the test file."
10-
else
11-
echo "This script doesn't launch partest, please use sbt partest instead."
12-
fi
4+
exec sbt "partest-only $*"

test/test/CompilerTest.scala

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ abstract class CompilerTest extends DottyTest {
101101
}
102102
}
103103
}
104-
def runFile(prefix: String, fileName: String, args: List[String] = Nil, xerrors: Int = 0,
104+
def runFile(prefix: String, fileName: String, args: List[String] = Nil, xerrors: Int = 0,
105105
extension: String = ".scala")(implicit defaultOptions: List[String]): Unit =
106106
compileFile(prefix, fileName, args, xerrors, extension, true)
107107

@@ -131,7 +131,7 @@ abstract class CompilerTest extends DottyTest {
131131
val firstDest = Directory(DPConfig.testRoot + JFile.separator + kind + JFile.separator + dirName)
132132
computeDestAndCopyFiles(sourceDir, firstDest, kind, flags, xerrors.toString)
133133
if (deep == "deep")
134-
deleteDir(sourceDir)
134+
Directory(sourceDir).deleteRecursively
135135
} else {
136136
throw new java.io.FileNotFoundException(s"Unable to locate test dir $prefix$dirName")
137137
}
@@ -172,7 +172,7 @@ abstract class CompilerTest extends DottyTest {
172172
recCopyFiles(jfile, destDir / jfile.getName)
173173
})
174174
compileDir(DPConfig.testRoot + JFile.separator, testName, args, xerrors)
175-
deleteDir(destDir.jfile)
175+
destDir.deleteRecursively
176176
}
177177
}
178178

@@ -186,14 +186,14 @@ abstract class CompilerTest extends DottyTest {
186186
}
187187

188188
// In particular, don't copy flags from scalac tests
189-
private val extensionsToCopy = scala.collection.immutable.HashSet("scala", "java", "check")
189+
private val extensionsToCopy = scala.collection.immutable.HashSet("scala", "java")
190190

191191
/** Determines what kind of test to run. */
192192
private def testKind(prefixDir: String, xerrors: Int, runTest: Boolean) = {
193193
if (runTest) "run"
194194
else if (xerrors > 0) "neg"
195195
else if (prefixDir.endsWith("run" + JFile.separator)) {
196-
log("WARNING: test is being run as pos test despite being in a run directory. " +
196+
log("WARNING: test is being run as pos test despite being in a run directory. " +
197197
"Use runFile/runDir instead of compileFile/compileDir to do a run test")
198198
"pos"
199199
} else "pos"
@@ -239,10 +239,12 @@ abstract class CompilerTest extends DottyTest {
239239
if (nerr != "0")
240240
dest.changeExtension("nerr").createFile(true).writeAll(nerr)
241241
sourceFile.changeExtension("check").ifFile({ check =>
242-
if (kind == "run")
242+
if (kind == "run") {
243243
FileManager.copyFile(check.jfile, dest.changeExtension("check").jfile)
244-
else
244+
dest.changeExtension("checksrc").createFile(true).writeAll("check file generated from source:\n" + check.toString)
245+
} else {
245246
log(s"WARNING: ignoring $check for test kind $kind")
247+
}
246248
})
247249

248250
}
@@ -253,7 +255,12 @@ abstract class CompilerTest extends DottyTest {
253255
processFileDir(sourceFile, { sf =>
254256
if (extensionsToCopy.contains(sf.extension)) {
255257
dest.parent.jfile.mkdirs
256-
FileManager.copyFile(sourceFile.jfile, dest.jfile)
258+
dest.toFile.writeAll("/* ==========================================\n",
259+
" * ========= AUTOMATICALLY GENERATED ========\n",
260+
" * ========= DO NOT EDIT THIS FILE ==========\n",
261+
" * ==========================================\n",
262+
" * Original: " + sf.toString + " */\n\n",
263+
sf.slurp)
257264
} else {
258265
log(s"WARNING: ignoring $sf")
259266
}
@@ -326,14 +333,6 @@ abstract class CompilerTest extends DottyTest {
326333
destDir.jfile
327334
}
328335

329-
/** Recursively deletes directories (and files). */
330-
private def deleteDir(dir: JFile): Unit = {
331-
val children = dir.listFiles
332-
if (children != null)
333-
children.foreach(deleteDir(_))
334-
dir.delete
335-
}
336-
337336
/** Write either to console (JUnit) or log file (partest). */
338337
private def log(msg: String) = logFile.map(_.appendAll(msg + "\n")).getOrElse(println(msg))
339338
}

0 commit comments

Comments
 (0)