From c16fb4e13c9fbddce0d7541f7c566955a6f65023 Mon Sep 17 00:00:00 2001 From: LydiaSkuse <31440012+LydiaSkuse@users.noreply.github.com> Date: Fri, 15 Sep 2023 15:45:32 +0200 Subject: [PATCH] use directives instead of scalac options in tests --- compiler/test-resources/repl/i13208.scala | 2 +- compiler/test-resources/repl/rewrite-messages | 2 +- compiler/test/dotty/tools/repl/ReplTest.scala | 2 +- compiler/test/dotty/tools/utils.scala | 31 +++++++++++-------- .../macro/pos/t8013/inpervolated_2.scala | 4 +-- tests/pos-macros/i18409.scala | 2 +- 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/compiler/test-resources/repl/i13208.scala b/compiler/test-resources/repl/i13208.scala index ce4fcf0d9ed8..61ace43c732d 100644 --- a/compiler/test-resources/repl/i13208.scala +++ b/compiler/test-resources/repl/i13208.scala @@ -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 diff --git a/compiler/test-resources/repl/rewrite-messages b/compiler/test-resources/repl/rewrite-messages index eee2fe034c43..a63a72195019 100644 --- a/compiler/test-resources/repl/rewrite-messages +++ b/compiler/test-resources/repl/rewrite-messages @@ -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._ diff --git a/compiler/test/dotty/tools/repl/ReplTest.scala b/compiler/test/dotty/tools/repl/ReplTest.scala index 34cad747fde6..8fbf635c9a17 100644 --- a/compiler/test/dotty/tools/repl/ReplTest.scala +++ b/compiler/test/dotty/tools/repl/ReplTest.scala @@ -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) diff --git a/compiler/test/dotty/tools/utils.scala b/compiler/test/dotty/tools/utils.scala index 8c154a38850d..8161631acb44 100644 --- a/compiler/test/dotty/tools/utils.scala +++ b/compiler/test/dotty/tools/utils.scala @@ -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) @@ -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 diff --git a/tests/disabled/macro/pos/t8013/inpervolated_2.scala b/tests/disabled/macro/pos/t8013/inpervolated_2.scala index 90e571b42c8c..cbe5139cef5a 100644 --- a/tests/disabled/macro/pos/t8013/inpervolated_2.scala +++ b/tests/disabled/macro/pos/t8013/inpervolated_2.scala @@ -1,6 +1,4 @@ -/* - * scalac: -Xfatal-warnings -Xlint - */ +//> using options -Xfatal-warnings -Xlint package t8013 // unsuspecting user of perverse macro diff --git a/tests/pos-macros/i18409.scala b/tests/pos-macros/i18409.scala index e1dd8cef674d..800e192b81bb 100644 --- a/tests/pos-macros/i18409.scala +++ b/tests/pos-macros/i18409.scala @@ -1,4 +1,4 @@ -// scalac: -Werror -Wunused:all +//> using options -Werror -Wunused:all import scala.quoted.*