Skip to content

Commit 142ab00

Browse files
committed
Fixes scala#7051 Detect typos in neg tests and give a warning
1 parent a80df17 commit 142ab00

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,10 @@ trait ParallelTesting extends RunnerOrchestration { self =>
631631

632632
if (compilerCrashed) Some(s"Compiler crashed when compiling: ${testSource.title}" )
633633
else if (actualErrors == 0) Some(s"\nNo errors found when compiling neg test $testSource" )
634-
else if (expectedErrors != actualErrors) Some(s"\nWrong number of errors encountered when compiling $testSource, expected: $expectedErrors, actual: $actualErrors")
634+
else if (expectedErrors != actualErrors) {
635+
getWarnings(testSource.sourceFiles) foreach echo
636+
Some(s"\nWrong number of errors encountered when compiling $testSource, expected: $expectedErrors, actual: $actualErrors")
637+
}
635638
else if (hasMissingAnnotations) Some(s"\nErrors found on incorrect row numbers when compiling $testSource" )
636639
else if (!errorMap.isEmpty) Some(s"\nExpected error(s) have {<error position>=<unreported error>}: $errorMap" )
637640
else None
@@ -672,6 +675,35 @@ trait ParallelTesting extends RunnerOrchestration { self =>
672675
(errorMap, expectedErrors)
673676
}
674677

678+
/**
679+
* We want to warn the developer about some of the possible typos when trying to point "error" and "nopos-error"
680+
* For example - A warning could be emitted if the developer mistakenly types in "//error" instead of "// error"
681+
* or "//nopos-error" instead of "// nopos-error"
682+
*
683+
* @param files test files
684+
* @return Sequence of warning messages
685+
*/
686+
def getWarnings(files : Seq[JFile]) : Seq[String] = {
687+
688+
def hasTypo(line : String) : Boolean = line.contains("//error") || line.contains("//nopos-error")
689+
690+
def getWarning(line: String, fileName: String): String = if (line.contains("//error")) {
691+
s"Wrong : //error\n Correct : // error"
692+
} else if (line.contains("//nopos-error")) {
693+
s"Wrong : //nopos-error\n Correct : // nopos-error"
694+
} else ""
695+
696+
val fileNamesAndLineNumber: Seq[(String, String, Int)] = for {
697+
file <- files.filter(_.getName.endsWith(".scala"))
698+
(line, lineNbr) <- Source.fromFile(file, "UTF-8").getLines().zipWithIndex.filter({ case (line, _) => hasTypo(line) })
699+
} yield {
700+
(file.getName, line, lineNbr)
701+
}
702+
fileNamesAndLineNumber.map({
703+
case (fileName, line, lineNbr) => s"Possible typo in $fileName at line number $lineNbr \n " +
704+
getWarning(line, fileName)})
705+
}
706+
675707
def getMissingExpectedErrors(errorMap: HashMap[String, Integer], reporterErrors: Iterator[MessageContainer]) = !reporterErrors.forall { error =>
676708
val key = if (error.pos.exists) {
677709
def toRelative(path: String): String = // For some reason, absolute paths leak from the compiler itself...

0 commit comments

Comments
 (0)