Skip to content

Commit a8a6439

Browse files
TheElectronWillsmarter
authored andcommitted
Standardize coverage tests
Now use scala3-compiler-bootstrapped/testCoverage [--update-checkfiles]
1 parent bad8b86 commit a8a6439

16 files changed

+224
-197
lines changed
Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,74 @@
11
package dotty.tools.dotc.coverage
22

33
import org.junit.Test
4+
import org.junit.AfterClass
45
import org.junit.Assert.*
56
import org.junit.experimental.categories.Category
67

7-
import dotty.BootstrappedOnlyTests
8-
import dotty.tools.dotc.Main
9-
10-
import java.nio.file.Files
11-
import java.nio.file.Path
12-
import java.nio.file.FileSystems
13-
import java.nio.file.Paths
14-
import java.nio.charset.StandardCharsets.UTF_8
15-
import java.nio.file.StandardCopyOption
8+
import dotty.{BootstrappedOnlyTests, Properties}
9+
import dotty.tools.vulpix.TestConfiguration.*
10+
import dotty.tools.vulpix.*
1611

17-
@main def updateExpect =
18-
CoverageTests().runExpectTest(updateCheckfiles = true)
12+
import java.nio.file.{Files, FileSystems, Path, Paths, StandardCopyOption}
13+
import scala.jdk.CollectionConverters.*
14+
import scala.language.unsafeNulls
15+
import dotty.tools.dotc.Main
1916

2017
@Category(Array(classOf[BootstrappedOnlyTests]))
2118
class CoverageTests:
19+
import CoverageTests.*
20+
import CoverageTests.given
2221

23-
private val scalaFile = FileSystems.getDefault.nn.getPathMatcher("glob:**.scala").nn
24-
private val rootSrc = Paths.get(System.getProperty("dotty.tools.dotc.coverage.test")).nn
25-
private val expectDir = rootSrc.resolve("expect").nn
26-
27-
@Category(Array(classOf[dotty.SlowTests]))
28-
@Test def expectTests: Unit =
29-
runExpectTest(dotty.Properties.testsUpdateCheckfile)
22+
private val scalaFile = FileSystems.getDefault.getPathMatcher("glob:**.scala")
23+
private val rootSrc = Paths.get(System.getProperty("dotty.tools.dotc.coverage.test"))
24+
private val expectDir = rootSrc.resolve("expect")
3025

31-
/** Runs the tests */
32-
def runExpectTest(updateCheckfiles: Boolean): Unit =
33-
val sourceRoot = if updateCheckfiles then "../" else "."
26+
@Test
27+
def checkInstrumentedCode(): Unit =
28+
given TestGroup = TestGroup("instrumentCoverage")
29+
val updateCheckfiles = dotty.Properties.testsUpdateCheckfile
30+
val sourceRoot = rootSrc.toString
3431

35-
Files.walk(expectDir).nn.filter(scalaFile.matches).nn.forEach(p => {
36-
val path = p.nn
37-
val fileName = path.getFileName.nn.toString.nn.stripSuffix(".scala")
38-
val targetDir = computeCoverageInTmp(Seq(path), sourceRoot).nn
39-
val targetFile = targetDir.resolve(s"scoverage.coverage").nn
40-
val expectFile = expectDir.resolve(s"$fileName.scoverage.check").nn
32+
Files.walk(expectDir).filter(scalaFile.matches).forEach(p => {
33+
val path = p
34+
val fileName = path.getFileName.toString.stripSuffix(".scala")
35+
val targetDir = computeCoverageInTmp(path, sourceRoot)
36+
val targetFile = targetDir.resolve(s"scoverage.coverage")
37+
val expectFile = expectDir.resolve(s"$fileName.scoverage.check")
4138

4239
if updateCheckfiles then
4340
Files.copy(targetFile, expectFile, StandardCopyOption.REPLACE_EXISTING)
4441
else
45-
val expected = new String(Files.readAllBytes(expectFile), UTF_8)
46-
val obtained = new String(Files.readAllBytes(targetFile), UTF_8)
47-
assertEquals(expected, obtained)
42+
val expected = Files.readAllLines(expectFile).asScala
43+
val obtained = Files.readAllLines(targetFile).asScala
44+
if expected != obtained then
45+
for ((exp, actual),i) <- expected.zip(obtained).filter(_ != _).zipWithIndex do
46+
Console.err.println(s"wrong line ${i+1}:")
47+
Console.err.println(s" expected: $exp")
48+
Console.err.println(s" actual : $actual")
49+
fail(s"$targetFile differs from expected $expectFile")
4850

4951
})
5052

5153
/** Generates the coverage report for the given input file, in a temporary directory. */
52-
def computeCoverageInTmp(inputFiles: Seq[Path], sourceRoot: String): Path =
54+
def computeCoverageInTmp(inputFile: Path, sourceRoot: String)(using TestGroup): Path =
5355
val target = Files.createTempDirectory("coverage")
54-
val args = Array(
55-
"-Ycheck:instrumentCoverage",
56-
"-coverage-out",
57-
target.toString,
58-
"-coverage-sourceroot",
59-
sourceRoot,
60-
"-usejavacp"
61-
) ++ inputFiles.map(_.toString)
62-
val exit = Main.process(args)
63-
assertFalse(s"Compilation failed, ${exit.errorCount} errors", exit.hasErrors)
64-
target.nn
56+
val options = defaultOptions.and("-Ycheck:instrumentCoverage", "-coverage-out", target.toString, "-coverage-sourceroot", sourceRoot)
57+
compileFile(inputFile.toString, options).checkCompile()
58+
target
59+
60+
object CoverageTests extends ParallelTesting:
61+
import scala.concurrent.duration.*
62+
63+
def maxDuration = 30.seconds
64+
def numberOfSlaves = 1
65+
66+
def safeMode = Properties.testsSafeMode
67+
def testFilter = Properties.testsFilter
68+
def isInteractive = SummaryReport.isInteractive
69+
def updateCheckFiles = Properties.testsUpdateCheckfile
70+
71+
given summaryReport: SummaryReporting = SummaryReport()
72+
@AfterClass def tearDown(): Unit =
73+
super.cleanup()
74+
summaryReport.echoSummary()

project/Build.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ object Build {
138138
// Run tests with filter through vulpix test suite
139139
val testCompilation = inputKey[Unit]("runs integration test with the supplied filter")
140140

141+
// Run code coverage instrumentation tests
142+
val testCoverage = inputKey[Unit]("runs code coverage instrumentation test")
143+
141144
// Used to compile files similar to ./bin/scalac script
142145
val scalac = inputKey[Unit]("run the compiler using the correct classpath, or the user supplied classpath")
143146

@@ -607,6 +610,20 @@ object Build {
607610
}
608611
}.evaluated,
609612

613+
testCoverage := Def.inputTaskDyn {
614+
val args = spaceDelimited("<arg>").parsed
615+
if (args.contains("--help")) {
616+
println("usage: testCoverage [--update-checkfiles]")
617+
(Test / testOnly).toTask(" not.a.test")
618+
} else {
619+
val updateCheckfile = args.contains("--update-checkfiles")
620+
val test = "dotty.tools.dotc.coverage.CoverageTests"
621+
val argUpdateCheckfile = if (updateCheckfile) "-Ddotty.tests.updateCheckfiles=TRUE" else ""
622+
val cmd = s" $test -- $argUpdateCheckfile"
623+
(Test/testOnly).toTask(cmd)
624+
}
625+
}.evaluated,
626+
610627
Compile / mainClass := Some("dotty.tools.dotc.Main"),
611628

612629
scala := {

tests/coverage/expect/Constructor.scoverage.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# ' ' sign
2020
# ------------------------------------------
2121
1
22-
tests/coverage/expect/Constructor.scala
22+
expect/Constructor.scala
2323
covtest
2424
C
2525
Class
@@ -36,7 +36,7 @@ false
3636
1
3737

3838
2
39-
tests/coverage/expect/Constructor.scala
39+
expect/Constructor.scala
4040
covtest
4141
C
4242
Class
@@ -53,7 +53,7 @@ false
5353
f(x)
5454

5555
3
56-
tests/coverage/expect/Constructor.scala
56+
expect/Constructor.scala
5757
covtest
5858
O$
5959
Object
@@ -70,7 +70,7 @@ false
7070
1
7171

7272
4
73-
tests/coverage/expect/Constructor.scala
73+
expect/Constructor.scala
7474
covtest
7575
O$
7676
Object

0 commit comments

Comments
 (0)