Skip to content

Commit 5567729

Browse files
committed
tests: Move toolArgsFor out of PatmatExhaustivityTest
1 parent b3ab102 commit 5567729

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

compiler/src/dotty/tools/dotc/CompilationUnit.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ object CompilationUnit {
122122
NoSource
123123
}
124124
else if (!source.file.exists) {
125-
report.error(s"not found: ${source.file.path}")
125+
report.error(s"source file not found: ${source.file.path}")
126126
NoSource
127127
}
128128
else source

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 {
@@ -79,22 +78,4 @@ class PatmatExhaustivityTest {
7978

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

compiler/test/dotty/tools/utils.scala

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package tools
33

44
import java.io.File
55
import java.nio.charset.StandardCharsets.UTF_8
6+
import java.nio.file.{Files, Path => JPath}
67

7-
import scala.io.Source
8+
import scala.io.{Codec, Source}
89
import scala.reflect.ClassTag
910
import scala.util.Using.resource
1011
import scala.util.chaining.given
@@ -25,11 +26,10 @@ extension (f: File) def absPath =
2526
extension (str: String) def dropExtension =
2627
str.reverse.dropWhile(_ != '.').drop(1).reverse
2728

28-
private def withFile[T](file: File)(action: Source => T): T =
29-
resource(Source.fromFile(file, UTF_8.name))(action)
30-
31-
def readLines(f: File): List[String] = withFile(f)(_.getLines.toList)
32-
def readFile(f: File): String = withFile(f)(_.mkString)
29+
private
30+
def withFile[T](file: File)(action: Source => T)(using Codec): T = resource(Source.fromFile(file))(action)
31+
def readLines(f: File)(using codec: Codec = Codec.UTF8): List[String] = withFile(f)(_.getLines.toList)
32+
def readFile(f: File)(using codec: Codec = Codec.UTF8): String = withFile(f)(_.mkString)
3333

3434
private object Unthrown extends ControlThrowable
3535

@@ -43,3 +43,25 @@ def assertThrows[T <: Throwable: ClassTag](p: T => Boolean)(body: => Any): Unit
4343
case failed: T => throw AssertionError(s"Exception failed check: $failed").tap(_.addSuppressed(failed))
4444
case NonFatal(other) => throw AssertionError(s"Wrong exception: expected ${implicitly[ClassTag[T]]} but was ${other.getClass.getName}").tap(_.addSuppressed(other))
4545
end assertThrows
46+
47+
def toolArgsFor(files: List[JPath])(using codec: Codec = Codec.UTF8): List[String] =
48+
files.flatMap(path => toolArgsParse(readLines(path.toFile)))
49+
50+
// Inspect the first 10 of the given lines for compiler options of the form
51+
// `// scalac: args`, `/* scalac: args`, ` * scalac: args`.
52+
// If args string ends in close comment, drop the `*` `/`.
53+
// If split, parse the args string as a command line.
54+
// (from scala.tools.partest.nest.Runner#toolArgsFor)
55+
def toolArgsParse(lines: List[String]): List[String] = {
56+
val tag = "scalac:"
57+
val endc = "*" + "/" // be forgiving of /* scalac: ... */
58+
def stripped(s: String) = s.substring(s.indexOf(tag) + tag.length).stripSuffix(endc)
59+
val args = lines.to(LazyList).take(10).filter { s =>
60+
s.contains("//" + tag)
61+
|| s.contains("// " + tag)
62+
|| s.contains("/* " + tag)
63+
|| s.contains(" * " + tag)
64+
// but avoid picking up comments like "% scalac ./a.scala" and "$ scalac a.scala"
65+
}.map(stripped).headOption
66+
args.map(dotc.config.CommandLineParser.tokenize).getOrElse(Nil)
67+
}

0 commit comments

Comments
 (0)