Skip to content

Commit 74376f3

Browse files
committed
Read tool args in test files
1 parent f819617 commit 74376f3

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

compiler/test/dotty/tools/dotc/transform/PatmatExhaustivityTest.scala

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import reporting.TestReporter
1010
import dotty.tools.io.Directory
1111

1212
import java.io._
13-
import java.nio.file.{Files, Path => JPath}
13+
import java.nio.file.{Path => JPath}
1414

15-
import scala.io.Source._
1615
import org.junit.Test
1716

1817
class PatmatExhaustivityTest {
@@ -75,22 +74,4 @@ class PatmatExhaustivityTest {
7574

7675
println(msg)
7776
}
78-
79-
// inspect given files for tool args of the form `tool: args`
80-
// if args string ends in close comment, drop the `*` `/`
81-
// if split, parse the args string as command line.
82-
// (from scala.tools.partest.nest.Runner#toolArgsFor)
83-
private def toolArgsFor(files: List[JPath]): List[String] = {
84-
import scala.jdk.OptionConverters._
85-
import config.CommandLineParser.tokenize
86-
files.flatMap { path =>
87-
val tag = "scalac:"
88-
val endc = "*" + "/" // be forgiving of /* scalac: ... */
89-
def stripped(s: String) = s.substring(s.indexOf(tag) + tag.length).stripSuffix(endc)
90-
val args = scala.util.Using.resource(Files.lines(path, scala.io.Codec.UTF8.charSet))(
91-
_.limit(10).filter(_.contains(tag)).map(stripped).findAny.toScala
92-
)
93-
args.map(tokenize).getOrElse(Nil)
94-
}
95-
}
9677
}

compiler/test/dotty/tools/utils.scala

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package dotty.tools
22

33
import java.io.File
44
import java.nio.charset.StandardCharsets.UTF_8
5+
import java.nio.file.{Files, Path => JPath}
56

6-
import scala.io.Source
7+
import scala.io.{Codec, Source}
78
import scala.reflect.ClassTag
89
import scala.util.Using.resource
910
import scala.util.chaining.given
@@ -39,3 +40,21 @@ def assertThrows[T <: Throwable: ClassTag](p: T => Boolean)(body: => Any): Unit
3940
case failed: T => throw AssertionError(s"Exception failed check: $failed").tap(_.addSuppressed(failed))
4041
case NonFatal(other) => throw AssertionError(s"Wrong exception: expected ${implicitly[ClassTag[T]]} but was ${other.getClass.getName}").tap(_.addSuppressed(other))
4142
end assertThrows
43+
44+
// inspect given files for tool args of the form `tool: args`
45+
// if args string ends in close comment, drop the `*` `/`
46+
// if split, parse the args string as command line.
47+
// (from scala.tools.partest.nest.Runner#toolArgsFor)
48+
def toolArgsFor(files: List[JPath])(implicit codec: Codec = Codec.UTF8): List[String] =
49+
import scala.jdk.OptionConverters._
50+
import dotc.config.CommandLineParser.tokenize
51+
files.flatMap { path =>
52+
val tag = "scalac:"
53+
val endc = "*" + "/" // be forgiving of /* scalac: ... */
54+
def stripped(s: String) = s.substring(s.indexOf(tag) + tag.length).stripSuffix(endc)
55+
val args = scala.util.Using.resource(Files.lines(path, codec.charSet))(
56+
_.limit(10).filter(_.contains(tag)).map(stripped).findAny.toScala
57+
)
58+
args.map(tokenize).getOrElse(Nil)
59+
}
60+
end toolArgsFor

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,15 +441,17 @@ trait ParallelTesting extends RunnerOrchestration { self =>
441441
throw e
442442

443443
protected def compile(files0: Array[JFile], flags0: TestFlags, suppressErrors: Boolean, targetDir: JFile): TestReporter = {
444-
val flags = flags0.and("-d", targetDir.getPath)
445-
.withClasspath(targetDir.getPath)
446-
447444
def flattenFiles(f: JFile): Array[JFile] =
448445
if (f.isDirectory) f.listFiles.flatMap(flattenFiles)
449446
else Array(f)
450447

451448
val files: Array[JFile] = files0.flatMap(flattenFiles)
452449

450+
val flags = flags0
451+
.and(toolArgsFor(files.toList.map(_.toPath))(using getCodecFromEncodingOpt(flags0)): _*)
452+
.and("-d", targetDir.getPath)
453+
.withClasspath(targetDir.getPath)
454+
453455
def compileWithJavac(fs: Array[String]) = if (fs.nonEmpty) {
454456
val fullArgs = Array(
455457
"javac",
@@ -1358,6 +1360,11 @@ trait ParallelTesting extends RunnerOrchestration { self =>
13581360
// Create a CompilationTest and let the user decide whether to execute a pos or a neg test
13591361
new CompilationTest(targets)
13601362
}
1363+
1364+
private def getCodecFromEncodingOpt(flags: TestFlags) =
1365+
flags.options.sliding(2).collectFirst {
1366+
case Array("-encoding", encoding) => scala.io.Codec(encoding)
1367+
}.getOrElse(scala.io.Codec.UTF8)
13611368
}
13621369

13631370
object ParallelTesting {

0 commit comments

Comments
 (0)