@@ -729,6 +729,74 @@ trait ParallelTesting extends RunnerOrchestration { self =>
729729 override def onSuccess (testSource : TestSource , reporters : Seq [TestReporter ], logger : LoggedRunnable ): Unit =
730730 diffCheckfile(testSource, reporters, logger)
731731
732+ override def maybeFailureMessage (testSource : TestSource , reporters : Seq [TestReporter ]): Option [String ] =
733+ lazy val (map, expCount) = getWarnMapAndExpectedCount(testSource.sourceFiles.toIndexedSeq)
734+ lazy val obtCount = reporters.foldLeft(0 )(_ + _.warningCount)
735+ lazy val (expected, unexpected) = getMissingExpectedWarnings(map, reporters.iterator.flatMap(_.diagnostics))
736+ def hasMissingAnnotations = expected.nonEmpty || unexpected.nonEmpty
737+ def showDiagnostics = " -> following the diagnostics:\n " +
738+ reporters.flatMap(_.diagnostics.toSeq.sortBy(_.pos.line).map(e => s " ${e.pos.line + 1 }: ${e.message}" )).mkString(" at " , " \n at " , " " )
739+ Option :
740+ if reporters.exists(_.compilerCrashed) then s " Compiler crashed when compiling: ${testSource.title}"
741+ else if reporters.exists(_.errorCount > 0 ) then
742+ s """ Compilation failed for: ${testSource.title}
743+ | $showDiagnostics
744+ | """ .stripMargin.trim.linesIterator.mkString(" \n " , " \n " , " " )
745+ else if obtCount == 0 then s " \n No warnings found when compiling warn test $testSource"
746+ else if expCount == 0 then s " \n No warning expected/defined in $testSource -- use // warn "
747+ else if expCount != obtCount then
748+ s """ |Wrong number of warnings encountered when compiling $testSource
749+ |expected: $expCount, actual: $obtCount
750+ | ${expected.mkString(" Unfulfilled expectations:\n " , " \n " , " " )}
751+ | ${unexpected.mkString(" Unexpected warnings:\n " , " \n " , " " )}
752+ | $showDiagnostics
753+ | """ .stripMargin.trim.linesIterator.mkString(" \n " , " \n " , " " )
754+ else if hasMissingAnnotations then s " \n Warnings found on incorrect row numbers when compiling $testSource\n $showDiagnostics"
755+ else if ! map.isEmpty then s " \n Expected warnings(s) have {<warning position>=<unreported warning>}: $map"
756+ else null
757+ end maybeFailureMessage
758+
759+ def getWarnMapAndExpectedCount (files : Seq [JFile ]): (HashMap [String , Integer ], Int ) =
760+ val comment = raw " //( *)warn " .r
761+ val map = new HashMap [String , Integer ]()
762+ var count = 0
763+ def bump (key : String ): Unit =
764+ map.get(key) match
765+ case null => map.put(key, 1 )
766+ case n => map.put(key, n+ 1 )
767+ count += 1
768+ files.filter(isSourceFile).foreach { file =>
769+ Using (Source .fromFile(file, StandardCharsets .UTF_8 .name)) { source =>
770+ source.getLines.zipWithIndex.foreach { case (line, lineNbr) =>
771+ comment.findAllMatchIn(line).foreach { _ =>
772+ bump(s " ${file.getPath}: ${lineNbr+ 1 }" )
773+ }
774+ }
775+ }.get
776+ }
777+ (map, count)
778+
779+ def getMissingExpectedWarnings (map : HashMap [String , Integer ], reporterWarnings : Iterator [Diagnostic ]): (List [String ], List [String ]) =
780+ val unexpected, unpositioned = ListBuffer .empty[String ]
781+ def relativize (path : String ): String = path.split(JFile .separatorChar).dropWhile(_ != " tests" ).mkString(JFile .separator)
782+ def seenAt (key : String ): Boolean =
783+ map.get(key) match
784+ case null => false
785+ case 1 => map.remove(key) ; true
786+ case n => map.put(key, n - 1 ) ; true
787+ def sawDiagnostic (d : Diagnostic ): Unit =
788+ val srcpos = d.pos.nonInlined
789+ if srcpos.exists then
790+ val key = s " ${relativize(srcpos.source.file.toString())}: ${srcpos.line + 1 }"
791+ if ! seenAt(key) then unexpected += key
792+ else
793+ unpositioned += relativize(srcpos.source.file.toString())
794+
795+ reporterWarnings.foreach(sawDiagnostic)
796+
797+ (map.asScala.keys.toList, (unexpected ++ unpositioned).toList)
798+ end getMissingExpectedWarnings
799+
732800 private final class RewriteTest (testSources : List [TestSource ], checkFiles : Map [JFile , JFile ], times : Int , threadLimit : Option [Int ], suppressAllOutput : Boolean )(implicit summaryReport : SummaryReporting )
733801 extends Test (testSources, times, threadLimit, suppressAllOutput) {
734802 private def verifyOutput (testSource : TestSource , reporters : Seq [TestReporter ], logger : LoggedRunnable ) = {
0 commit comments