Skip to content

Commit 26f3124

Browse files
committed
Consolidate test reporters in TestReporter always dumping log file
1 parent 7df4465 commit 26f3124

File tree

6 files changed

+108
-63
lines changed

6 files changed

+108
-63
lines changed

compiler/src/dotty/tools/dotc/reporting/ConsoleReporter.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ class ConsoleReporter(
1717

1818
import MessageContainer._
1919

20-
/** maximal number of error messages to be printed */
21-
protected def ErrorLimit = 100
22-
2320
/** Prints the message. */
2421
def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() }
2522

compiler/src/dotty/tools/dotc/reporting/Reporter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ trait Reporting { this: Context =>
6363
|This can be achieved by adding the import clause 'import $fqname'
6464
|or by setting the compiler option -language:$feature.
6565
|See the Scala docs for value $fqname for a discussion
66-
|why the feature $req be explicitly enabled."""
66+
|why the feature $req be explicitly enabled.""".stripMargin
6767
}
6868
}
6969

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ object CompilationTests {
249249
}
250250

251251
val noCheckOptions = Array(
252-
"-pagewidth", "120"
252+
"-pagewidth", "120",
253+
"-color:never"
253254
)
254255

255256
val checkOptions = Array(

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

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ package tools
33
package dotc
44

55
import java.io.{ File => JFile }
6-
import scala.io.Source
7-
8-
import core.Contexts._
9-
import reporting.{ Reporter, UniqueMessagePositions, HideNonSensicalMessages, MessageRendering }
10-
import reporting.diagnostic.MessageContainer
11-
import interfaces.Diagnostic.ERROR
6+
import java.text.SimpleDateFormat
7+
import java.util.HashMap
128
import java.lang.reflect.InvocationTargetException
139
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
1410
import java.nio.file.{ Files, Path, Paths, NoSuchFileException }
1511
import java.util.concurrent.{ Executors => JExecutors, TimeUnit, TimeoutException }
12+
13+
import scala.io.Source
1614
import scala.util.control.NonFatal
1715
import scala.util.Try
1816
import scala.collection.mutable
19-
import java.util.HashMap
17+
18+
import core.Contexts._
19+
import reporting.{ Reporter, TestReporter }
20+
import reporting.diagnostic.MessageContainer
21+
import interfaces.Diagnostic.ERROR
2022

2123
trait ParallelTesting {
2224

@@ -281,7 +283,7 @@ trait ParallelTesting {
281283
(errorMap, expectedErrors)
282284
}
283285

284-
def getMissingAnnotations(errorMap: HashMap[String, Integer], reporterErrors: List[MessageContainer]) = !reporterErrors.forall { error =>
286+
def getMissingAnnotations(errorMap: HashMap[String, Integer], reporterErrors: Iterator[MessageContainer]) = !reporterErrors.forall { error =>
285287
val getter = if (error.pos.exists) {
286288
val fileName = error.pos.source.file.toString
287289
s"$fileName:${error.pos.line}"
@@ -318,7 +320,7 @@ trait ParallelTesting {
318320
val (errorMap, expectedErrors) = errorMapAndExpected(compilationUnits.toArray.flatten)
319321
val reporters = compilationUnits.map(files => compile(files.filter(isCompilable), flags, true, times, outDir))
320322
val actualErrors = reporters.foldLeft(0)(_ + _.errorCount)
321-
val errors = reporters.flatMap(_.errors)
323+
val errors = reporters.iterator.flatMap(_.errors)
322324
(expectedErrors, actualErrors, () => getMissingAnnotations(errorMap, errors), errorMap)
323325
}
324326
}
@@ -347,33 +349,7 @@ trait ParallelTesting {
347349
}
348350
}
349351

350-
private class DaftReporter(suppress: Boolean)
351-
extends Reporter with UniqueMessagePositions with HideNonSensicalMessages
352-
with MessageRendering {
353-
private var _errors: List[MessageContainer] = Nil
354-
def errors = _errors
355-
356-
private var _summary = new StringBuilder
357-
def echoSummary(msg: String): this.type = {
358-
_summary.append(msg)
359-
this
360-
}
361-
362-
def printSummary(): this.type = {
363-
val msg = _summary.toString
364-
if (msg.nonEmpty) println(msg)
365-
this
366-
}
367-
368-
override def doReport(m: MessageContainer)(implicit ctx: Context) = {
369-
if (m.level == ERROR) {
370-
_errors = m :: _errors
371-
if (!suppress) System.err.println(messageAndPos(m.contained, m.pos, diagnosticLevel(m)))
372-
}
373-
}
374-
}
375-
376-
private def compile(files0: Array[JFile], flags0: Array[String], suppressErrors: Boolean, times: Int, targetDir: JFile): DaftReporter = {
352+
private def compile(files0: Array[JFile], flags0: Array[String], suppressErrors: Boolean, times: Int, targetDir: JFile): TestReporter = {
377353

378354
val flags = flags0 ++ Array("-d", targetDir.getAbsolutePath)
379355

@@ -418,7 +394,7 @@ trait ParallelTesting {
418394
val javaCompiledBefore = compileWithJavac(javaFiles)
419395

420396
// Then we compile the scala files:
421-
val reporter = new DaftReporter(suppress = suppressErrors)
397+
val reporter = TestReporter.parallelReporter(logLevel = if (suppressErrors) ERROR + 1 else ERROR)
422398
val driver =
423399
if (times == 1) new Driver { def newCompiler(implicit ctx: Context) = new Compiler }
424400
else new Driver {
@@ -428,7 +404,7 @@ trait ParallelTesting {
428404
(emptyReporter /: (1 to n)) ((_, i) => op(i))
429405

430406
private def echoSummary(rep: Reporter, msg: String)(implicit ctx: Context) =
431-
rep.asInstanceOf[DaftReporter].echoSummary(msg)
407+
rep.asInstanceOf[TestReporter].echoSummary(msg)
432408

433409
override def doCompile(comp: Compiler, files: List[String])(implicit ctx: Context) =
434410
ntimes(times) { run =>

compiler/test/dotty/tools/dotc/reporting/TestReporter.scala

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,61 @@ package dotty.tools
22
package dotc
33
package reporting
44

5+
import java.io.{ PrintWriter, File => JFile, FileOutputStream }
6+
import java.text.SimpleDateFormat
7+
import java.util.Date
8+
59
import scala.collection.mutable
10+
611
import util.SourcePosition
712
import core.Contexts._
813
import Reporter._
9-
import java.io.PrintWriter
1014
import diagnostic.{ Message, MessageContainer, NoExplanation }
1115
import diagnostic.messages._
16+
import interfaces.Diagnostic.{ ERROR, WARNING, INFO }
1217

13-
class TestReporter(writer: PrintWriter) extends Reporter
14-
with UniqueMessagePositions with HideNonSensicalMessages {
15-
18+
class TestReporter protected (outWriter: PrintWriter, protected val filePrintln: String => Unit, logLevel: Int) extends Reporter
19+
with UniqueMessagePositions with HideNonSensicalMessages with MessageRendering {
1620
import MessageContainer._
1721

18-
/** maximal number of error messages to be printed */
19-
protected def ErrorLimit = 100
22+
protected final val _errorBuf = mutable.ArrayBuffer.empty[MessageContainer]
23+
final def errors: Iterator[MessageContainer] = _errorBuf.iterator
2024

21-
def printPos(pos: SourcePosition): Unit =
25+
final def inlineInfo(pos: SourcePosition): String =
2226
if (pos.exists) {
23-
if (pos.outer.exists) {
24-
writer.println(s"\ninlined at ${pos.outer}:\n")
25-
printPos(pos.outer)
26-
}
27+
if (pos.outer.exists)
28+
s"\ninlined at ${pos.outer}:\n" + inlineInfo(pos.outer)
29+
else ""
30+
}
31+
else ""
32+
33+
final def printSummary(): this.type = {
34+
val msg = _summary.toString
35+
if (msg.nonEmpty) {
36+
outWriter.println(msg)
37+
filePrintln(msg)
2738
}
39+
this
40+
}
41+
42+
private var _summary = new StringBuilder
43+
final def echoSummary(msg: String): this.type = {
44+
_summary.append(msg)
45+
this
46+
}
2847

2948
/** Prints the message with the given position indication. */
30-
def printMessageAndPos(msg: String, pos: SourcePosition)(implicit ctx: Context): Unit = {
31-
val posStr = s"${pos.line + 1}: "
32-
writer.println(posStr + msg)
33-
printPos(pos)
49+
def printMessageAndPos(m: MessageContainer, extra: String)(implicit ctx: Context): Unit = {
50+
val msg = messageAndPos(m.contained, m.pos, diagnosticLevel(m))
51+
val extraInfo = inlineInfo(m.pos)
52+
53+
if (m.level >= logLevel) {
54+
outWriter.println(msg)
55+
if (extraInfo.nonEmpty) outWriter.println(extraInfo)
56+
}
57+
58+
filePrintln(msg)
59+
if (extraInfo.nonEmpty) filePrintln(extraInfo)
3460
}
3561

3662
override def doReport(m: MessageContainer)(implicit ctx: Context): Unit = {
@@ -41,11 +67,56 @@ with UniqueMessagePositions with HideNonSensicalMessages {
4167
}
4268

4369
m match {
44-
case m: Error =>
45-
printMessageAndPos(m.contained.kind + extra, m.pos)
70+
case m: Error => {
71+
_errorBuf.append(m)
72+
printMessageAndPos(m, extra)
73+
}
4674
case w: Warning =>
47-
printMessageAndPos(w.contained.kind + extra, w.pos)
75+
printMessageAndPos(w, extra)
4876
case _ =>
4977
}
5078
}
5179
}
80+
81+
object TestReporter {
82+
private[this] val logWriter = {
83+
val df = new SimpleDateFormat("yyyy-MM-dd-HH:mm")
84+
val timestamp = df.format(new Date)
85+
new PrintWriter(new FileOutputStream(new JFile(s"../tests-$timestamp.log"), true))
86+
}
87+
88+
def parallelReporter(logLevel: Int): TestReporter = new TestReporter(
89+
new PrintWriter(Console.err, true),
90+
str => logWriter.synchronized {
91+
logWriter.println(str)
92+
logWriter.flush()
93+
},
94+
logLevel
95+
)
96+
97+
def reporter(logLevel: Int): TestReporter = new TestReporter(
98+
new PrintWriter(Console.err, true),
99+
logWriter.println,
100+
logLevel
101+
)
102+
103+
def simplifiedReporter(writer: PrintWriter): TestReporter = new TestReporter(
104+
writer,
105+
logWriter.println,
106+
WARNING
107+
) {
108+
/** Prints the message with the given position indication in a simplified manner */
109+
override def printMessageAndPos(m: MessageContainer, extra: String)(implicit ctx: Context): Unit = {
110+
val msg = s"${m.pos.line + 1}: " + m.contained.kind + extra
111+
val extraInfo = inlineInfo(m.pos)
112+
113+
writer.println(msg)
114+
filePrintln(msg)
115+
116+
if (extraInfo.nonEmpty) {
117+
writer.println(extraInfo)
118+
filePrintln(extraInfo)
119+
}
120+
}
121+
}
122+
}

compiler/test/dotty/tools/dotc/transform/PatmatExhaustivityTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class PatmatExhaustivityTest {
1717

1818
private def compileFile(file: File) = {
1919
val stringBuffer = new StringWriter()
20-
val reporter = new TestReporter(new PrintWriter(stringBuffer))
20+
val reporter = TestReporter.simplifiedReporter(new PrintWriter(stringBuffer))
2121

2222
try {
2323
Main.process((file.getPath::options).toArray, reporter, null)
@@ -40,7 +40,7 @@ class PatmatExhaustivityTest {
4040
/** A single test with multiple files grouped in a folder */
4141
private def compileDir(file: File) = {
4242
val stringBuffer = new StringWriter()
43-
val reporter = new TestReporter(new PrintWriter(stringBuffer))
43+
val reporter = TestReporter.simplifiedReporter(new PrintWriter(stringBuffer))
4444

4545
val files = Directory(file.getPath).list.toList
4646
.filter(f => f.extension == "scala" || f.extension == "java" )

0 commit comments

Comments
 (0)