|
1 | 1 | package dotty.tools.dotc.coverage
|
2 | 2 |
|
3 | 3 | import org.junit.Test
|
| 4 | +import org.junit.AfterClass |
4 | 5 | import org.junit.Assert.*
|
5 | 6 | import org.junit.experimental.categories.Category
|
6 | 7 |
|
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.* |
16 | 11 |
|
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 |
19 | 16 |
|
20 | 17 | @Category(Array(classOf[BootstrappedOnlyTests]))
|
21 | 18 | class CoverageTests:
|
| 19 | + import CoverageTests.* |
| 20 | + import CoverageTests.given |
22 | 21 |
|
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") |
30 | 25 |
|
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 |
34 | 31 |
|
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") |
41 | 38 |
|
42 | 39 | if updateCheckfiles then
|
43 | 40 | Files.copy(targetFile, expectFile, StandardCopyOption.REPLACE_EXISTING)
|
44 | 41 | 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") |
48 | 50 |
|
49 | 51 | })
|
50 | 52 |
|
51 | 53 | /** 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 = |
53 | 55 | 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() |
0 commit comments