Skip to content

Commit 66ad0cd

Browse files
committed
Support compiling multiple files in coverage tests
1 parent 817d72f commit 66ad0cd

File tree

16 files changed

+236
-22
lines changed

16 files changed

+236
-22
lines changed

compiler/test/dotty/tools/dotc/coverage/CoverageTests.scala

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,27 @@ class CoverageTests:
5454
lines
5555
end fixWindowsPaths
5656

57-
def runOnFile(p: Path): Boolean =
58-
scalaFile.matches(p) &&
59-
(Properties.testsFilter.isEmpty || Properties.testsFilter.exists(p.toString.contains))
57+
def runOnFileOrDir(p: Path): Boolean =
58+
(scalaFile.matches(p) || Files.isDirectory(p))
59+
&& (p != dir)
60+
&& (Properties.testsFilter.isEmpty || Properties.testsFilter.exists(p.toString.contains))
61+
62+
Files.walk(dir, 1).filter(runOnFileOrDir).forEach(path => {
63+
// measurement files only exist in the "run" category
64+
// as these are generated at runtime by the scala.runtime.coverage.Invoker
65+
val (targetDir, expectFile, expectMeasurementFile) =
66+
if Files.isDirectory(path) then
67+
val dirName = path.getFileName().toString
68+
assert(!Files.walk(path).filter(scalaFile.matches(_)).toList.isEmpty, s"No scala files found in test directory: ${path}")
69+
val targetDir = computeCoverageInTmp(path, isDirectory = true, dir, run)
70+
(targetDir, path.resolve(s"test.scoverage.check"), path.resolve(s"test.measurement.check"))
71+
else
72+
val fileName = path.getFileName.toString.stripSuffix(".scala")
73+
val targetDir = computeCoverageInTmp(path, isDirectory = false, dir, run)
74+
(targetDir, path.resolveSibling(s"${fileName}.scoverage.check"), path.resolveSibling(s"${fileName}.measurement.check"))
6075

61-
Files.walk(dir).filter(runOnFile).forEach(path => {
62-
val fileName = path.getFileName.toString.stripSuffix(".scala")
63-
val targetDir = computeCoverageInTmp(path, dir, run)
6476
val targetFile = targetDir.resolve(s"scoverage.coverage")
65-
val expectFile = path.resolveSibling(s"$fileName.scoverage.check")
77+
6678
if updateCheckFiles then
6779
Files.copy(targetFile, expectFile, StandardCopyOption.REPLACE_EXISTING)
6880
else
@@ -72,9 +84,6 @@ class CoverageTests:
7284
val instructions = FileDiff.diffMessage(expectFile.toString, targetFile.toString)
7385
fail(s"Coverage report differs from expected data.\n$instructions")
7486

75-
// measurement files only exist in the "run" category
76-
// as these are generated at runtime by the scala.runtime.coverage.Invoker
77-
val expectMeasurementFile = path.resolveSibling(s"$fileName.measurement.check")
7887
if run && Files.exists(expectMeasurementFile) then
7988

8089
// Note that this assumes that the test invoked was single threaded,
@@ -95,14 +104,20 @@ class CoverageTests:
95104
})
96105

97106
/** Generates the coverage report for the given input file, in a temporary directory. */
98-
def computeCoverageInTmp(inputFile: Path, sourceRoot: Path, run: Boolean)(using TestGroup): Path =
107+
def computeCoverageInTmp(inputFile: Path, isDirectory: Boolean, sourceRoot: Path, run: Boolean)(using TestGroup): Path =
99108
val target = Files.createTempDirectory("coverage")
100109
val options = defaultOptions.and("-Ycheck:instrumentCoverage", "-coverage-out", target.toString, "-sourceroot", sourceRoot.toString)
101110
if run then
102-
val test = compileDir(inputFile.getParent.toString, options)
111+
val path = if isDirectory then inputFile.toString else inputFile.getParent.toString
112+
val test = compileDir(path, options)
113+
test.checkFilePaths.foreach { checkFilePath =>
114+
assert(checkFilePath.exists, s"Expected checkfile for $path $checkFilePath does not exist.")
115+
}
103116
test.checkRuns()
104117
else
105-
val test = compileFile(inputFile.toString, options)
118+
val test =
119+
if isDirectory then compileDir(inputFile.toString, options)
120+
else compileFile(inputFile.toString, options)
106121
test.checkCompile()
107122
target
108123

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
258258
* For a given test source, returns a check file against which the result of the test run
259259
* should be compared. Is used by implementations of this trait.
260260
*/
261-
final def checkFile(testSource: TestSource): Option[JFile] = (testSource match {
262-
case ts: JointCompilationSource =>
263-
ts.files.collectFirst {
264-
case f if !f.isDirectory =>
265-
new JFile(f.getPath.replaceFirst("\\.(scala|java)$", ".check"))
266-
}
267-
case ts: SeparateCompilationSource =>
268-
Option(new JFile(ts.dir.getPath + ".check"))
269-
}).filter(_.exists)
261+
final def checkFile(testSource: TestSource): Option[JFile] = (CompilationLogic.checkFilePath(testSource)).filter(_.exists)
270262

271263
/**
272264
* Checks if the given actual lines are the same as the ones in the check file.
@@ -343,6 +335,18 @@ trait ParallelTesting extends RunnerOrchestration { self =>
343335
}
344336
}
345337

338+
object CompilationLogic {
339+
private[ParallelTesting] def checkFilePath(testSource: TestSource) = testSource match {
340+
case ts: JointCompilationSource =>
341+
ts.files.collectFirst {
342+
case f if !f.isDirectory =>
343+
new JFile(f.getPath.replaceFirst("\\.(scala|java)$", ".check"))
344+
}
345+
case ts: SeparateCompilationSource =>
346+
Option(new JFile(ts.dir.getPath + ".check"))
347+
}
348+
}
349+
346350
/** Each `Test` takes the `testSources` and performs the compilation and assertions
347351
* according to the implementing class "neg", "run" or "pos".
348352
*/
@@ -1157,6 +1161,8 @@ trait ParallelTesting extends RunnerOrchestration { self =>
11571161
def this(targets: List[TestSource]) =
11581162
this(targets, 1, true, None, false, false)
11591163

1164+
def checkFilePaths: List[JFile] = targets.map(CompilationLogic.checkFilePath).flatten
1165+
11601166
def copy(targets: List[TestSource],
11611167
times: Int = times,
11621168
shouldDelete: Boolean = shouldDelete,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
List()
2+
List(abc, def)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Baz
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
>>> hello <<<

0 commit comments

Comments
 (0)