Skip to content

tests: Read tool args in test files #14144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions compiler/test/dotty/tools/utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dotty
package tools

import java.io.File
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets.UTF_8
import java.nio.file.{Files, Path => JPath}

Expand Down Expand Up @@ -45,8 +46,8 @@ def assertThrows[T <: Throwable: ClassTag](p: T => Boolean)(body: => Any): Unit
case NonFatal(other) => throw AssertionError(s"Wrong exception: expected ${implicitly[ClassTag[T]]} but was ${other.getClass.getName}").tap(_.addSuppressed(other))
end assertThrows

def toolArgsFor(files: List[JPath]): List[String] =
files.flatMap(path => toolArgsParse(Files.lines(path, UTF_8).limit(10).toScala(List)))
def toolArgsFor(files: List[JPath], charset: Charset = UTF_8): List[String] =
files.flatMap(path => toolArgsParse(Files.lines(path, charset).limit(10).toScala(List)))

// Inspect the first 10 of the given lines for compiler options of the form
// `// scalac: args`, `/* scalac: args`, ` * scalac: args`.
Expand All @@ -58,7 +59,8 @@ def toolArgsParse(lines: List[String]): List[String] = {
val endc = "*" + "/" // be forgiving of /* scalac: ... */
def stripped(s: String) = s.substring(s.indexOf(tag) + tag.length).stripSuffix(endc)
val args = lines.to(LazyList).take(10).filter { s =>
s.contains("// " + tag)
s.contains("//" + tag)
|| s.contains("// " + tag)
|| s.contains("/* " + tag)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these also be accepted?

  • "// " + tag with 2 or more spaces or a tab
  • "/*" + tag with no spaces

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should consider a regex

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less (ways) is more, IMO. I wouldn't have even added "/* ", but someone did back in scala/scala so I kept it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're called s'mores for a reason. I see partest doesn't look at opening comment. Could filter on tag and then on open comment preceding it. As sample UX, I learned yesterday that commenting out an // error line does not suppress the error expectation. So temporarily commenting out requires two edits. My takeaway is that a test can be forgiving, but should help disallow "my check file was not used" or "my options were not used" and I didn't know it and the test has been broken all along.

|| s.contains(" * " + tag)
// but avoid picking up comments like "% scalac ./a.scala" and "$ scalac a.scala"
Expand Down
15 changes: 11 additions & 4 deletions compiler/test/dotty/tools/vulpix/ParallelTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.io.{File => JFile, IOException}
import java.lang.System.{lineSeparator => EOL}
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
import java.nio.file.{Files, NoSuchFileException, Path, Paths}
import java.nio.charset.StandardCharsets
import java.nio.charset.{Charset, StandardCharsets}
import java.text.SimpleDateFormat
import java.util.{HashMap, Timer, TimerTask}
import java.util.concurrent.{TimeUnit, TimeoutException, Executors => JExecutors}
Expand Down Expand Up @@ -441,15 +441,17 @@ trait ParallelTesting extends RunnerOrchestration { self =>
throw e

protected def compile(files0: Array[JFile], flags0: TestFlags, suppressErrors: Boolean, targetDir: JFile): TestReporter = {
val flags = flags0.and("-d", targetDir.getPath)
.withClasspath(targetDir.getPath)

def flattenFiles(f: JFile): Array[JFile] =
if (f.isDirectory) f.listFiles.flatMap(flattenFiles)
else Array(f)

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

val flags = flags0
.and(toolArgsFor(files.toList.map(_.toPath), getCharsetFromEncodingOpt(flags0)): _*)
.and("-d", targetDir.getPath)
.withClasspath(targetDir.getPath)

def compileWithJavac(fs: Array[String]) = if (fs.nonEmpty) {
val fullArgs = Array(
"javac",
Expand Down Expand Up @@ -1358,6 +1360,11 @@ trait ParallelTesting extends RunnerOrchestration { self =>
// Create a CompilationTest and let the user decide whether to execute a pos or a neg test
new CompilationTest(targets)
}

private def getCharsetFromEncodingOpt(flags: TestFlags) =
flags.options.sliding(2).collectFirst {
case Array("-encoding", encoding) => Charset.forName(encoding)
}.getOrElse(StandardCharsets.UTF_8)
}

object ParallelTesting {
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/literals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ trait RejectedLiterals {
def missingHex: Int = { 0x } // error: invalid literal number
}
/*
// scalac: -Ywarn-octal-literal -Xfatal-warnings -deprecation
// nsc: -Ywarn-octal-literal -Xfatal-warnings -deprecation
trait RejectedLiterals {

def missingHex: Int = { 0x } // line 4: was: not reported, taken as zero
Expand Down
2 changes: 1 addition & 1 deletion tests/run/1938-2.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
object ProdNonEmpty {
def _1: Int = 0
def _2: String = "???" // Slight variation with scalac: this test passes
def _2: String = "???" // Slight variation with nsc: this test passes
// with ??? here. I think dotty behavior is fine
// according to the spec given that methods involved
// in pattern matching should be pure.
Expand Down