Skip to content

Commit

Permalink
use directives instead of scalac options in tests (#18560)
Browse files Browse the repository at this point in the history
- update remaining `// scalac:` to `//> using options` in tests
- throw error when using old syntax in tests

fixes `scalac:` portion of #18149
still to do:
- rewrite `// test: -jvm 15+` to use a directive
- support `// scalajs: --skip` with a directive or similar

once this is done, we can remove the `toolArg` regex
  • Loading branch information
bishabosha committed Sep 28, 2023
2 parents 5adef26 + c16fb4e commit 090710a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion compiler/test-resources/repl/i13208.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// scalac: -source:future -deprecation
//> using options -source:future -deprecation
scala> type M[X] = X match { case Int => String case _ => Int }
scala> type N[X] = X match { case List[_] => Int }
1 warning found
Expand Down
2 changes: 1 addition & 1 deletion compiler/test-resources/repl/rewrite-messages
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// scalac: -source:future-migration -deprecation -Werror
//> using options -source:future-migration -deprecation -Werror
scala> import scala.util._
-- Error: ----------------------------------------------------------------------
1 | import scala.util._
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/repl/ReplTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ extends ReplDriver(options, new PrintStream(out, true, StandardCharsets.UTF_8.na

val expectedOutput = lines.filter(nonBlank)
val actualOutput = {
val opts = toolArgsFor(ToolName.Scalac)(lines.take(1))
val opts = toolArgsFor(ToolName.Scalac, scriptFile.map(_.toString))(lines.take(1))
val (optsLine, inputLines) = if opts.isEmpty then ("", lines) else (lines.head, lines.drop(1))
resetToInitial(opts)

Expand Down
31 changes: 18 additions & 13 deletions compiler/test/dotty/tools/utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type ToolArgs = Map[ToolName, List[String]]
*/
def toolArgsFor(files: List[JPath], charset: Charset = UTF_8): ToolArgs =
files.foldLeft(Map.empty[ToolName, List[String]]) { (res, path) =>
val toolargs = toolArgsParse(resource(Files.lines(path, charset))(_.limit(10).toScala(List)))
val toolargs = toolArgsParse(resource(Files.lines(path, charset))(_.limit(10).toScala(List)), Some(path.toString))
toolargs.foldLeft(res) {
case (acc, (tool, args)) =>
val name = ToolName.named(tool)
Expand All @@ -74,31 +74,36 @@ def toolArgsFor(files: List[JPath], charset: Charset = UTF_8): ToolArgs =
}
}

def toolArgsFor(tool: ToolName)(lines: List[String]): List[String] =
toolArgsParse(lines).collectFirst { case (name, args) if tool eq ToolName.named(name) => CommandLineParser.tokenize(args) }.getOrElse(Nil)
def toolArgsFor(tool: ToolName, filename: Option[String])(lines: List[String]): List[String] =
toolArgsParse(lines, filename).collectFirst { case (name, args) if tool eq ToolName.named(name) => CommandLineParser.tokenize(args) }.getOrElse(Nil)

// scalac: arg1 arg2, with alternative opening, optional space, alt names, text that is not */ up to end.
// scalajs: arg1 arg2, with alternative opening, optional space, alt names, text that is not */ up to end.
// groups are (name, args)
// note: ideally we would replace everything that requires this to use directive syntax, however scalajs: --skip has no directive equivalent yet.
private val toolArg = raw"(?://|/\*| \*) ?(?i:(${ToolName.values.mkString("|")})):((?:[^*]|\*(?!/))*)".r.unanchored
private val directiveOptionsArg = raw"//> using options (.*)".r.unanchored

// Inspect the lines for compiler options of the form
// `// scalac: args`, `/* scalac: args`, ` * scalac: args`.
// `//> using options args`, `// scalajs: args`, `/* scalajs: args`, ` * scalajs: args` etc.
// If args string ends in close comment, stop at the `*` `/`.
// Returns all the matches by the regex.
def toolArgsParse(lines: List[String]): List[(String,String)] =
lines.flatMap { case toolArg(name, args) => List((name, args)) case _ => Nil } ++
def toolArgsParse(lines: List[String], filename: Option[String]): List[(String,String)] =
lines.flatMap {
case toolArg("scalac", _) => sys.error(s"`// scalac: args` not supported. Please use `//> using options args`${filename.fold("")(f => s" in file $f")}")
case toolArg(name, args) => List((name, args))
case _ => Nil
} ++
lines.flatMap { case directiveOptionsArg(args) => List(("scalac", args)) case _ => Nil }

import org.junit.Test
import org.junit.Assert._

class ToolArgsTest:
@Test def `missing toolarg is absent`: Unit = assertEquals(Nil, toolArgsParse(List("")))
@Test def `toolarg is present`: Unit = assertEquals(("test", " -hey") :: Nil, toolArgsParse("// test: -hey" :: Nil))
@Test def `tool is present`: Unit = assertEquals("-hey" :: Nil, toolArgsFor(ToolName.Test)("// test: -hey" :: Nil))
@Test def `missing tool is absent`: Unit = assertEquals(Nil, toolArgsFor(ToolName.Javac)("// test: -hey" :: Nil))
@Test def `missing toolarg is absent`: Unit = assertEquals(Nil, toolArgsParse(List(""), None))
@Test def `toolarg is present`: Unit = assertEquals(("test", " -hey") :: Nil, toolArgsParse("// test: -hey" :: Nil, None))
@Test def `tool is present`: Unit = assertEquals("-hey" :: Nil, toolArgsFor(ToolName.Test, None)("// test: -hey" :: Nil))
@Test def `missing tool is absent`: Unit = assertEquals(Nil, toolArgsFor(ToolName.Javac, None)("// test: -hey" :: Nil))
@Test def `multitool is present`: Unit =
assertEquals("-hey" :: Nil, toolArgsFor(ToolName.Test)("// test: -hey" :: "// javac: -d /tmp" :: Nil))
assertEquals("-d" :: "/tmp" :: Nil, toolArgsFor(ToolName.Javac)("// test: -hey" :: "// javac: -d /tmp" :: Nil))
assertEquals("-hey" :: Nil, toolArgsFor(ToolName.Test, None)("// test: -hey" :: "// javac: -d /tmp" :: Nil))
assertEquals("-d" :: "/tmp" :: Nil, toolArgsFor(ToolName.Javac, None)("// test: -hey" :: "// javac: -d /tmp" :: Nil))
end ToolArgsTest
4 changes: 1 addition & 3 deletions tests/disabled/macro/pos/t8013/inpervolated_2.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
* scalac: -Xfatal-warnings -Xlint
*/
//> using options -Xfatal-warnings -Xlint
package t8013

// unsuspecting user of perverse macro
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i18409.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// scalac: -Werror -Wunused:all
//> using options -Werror -Wunused:all

import scala.quoted.*

Expand Down

0 comments on commit 090710a

Please sign in to comment.