Skip to content

Commit 47abb66

Browse files
committed
Add test for basic reporting in Scala3doc
1 parent aba505b commit 47abb66

File tree

5 files changed

+141
-22
lines changed

5 files changed

+141
-22
lines changed

scala3doc/src/dotty/dokka/Scala3doc.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ class Scala3DocDokkaLogger(using DocContext) extends DokkaLogger:
2626

2727
def info(msg: String): Unit = report.inform(msg)
2828
def progress(msg: String): Unit = report.informProgress(msg)
29+
30+
private val dokkaAlphaWarning = "Dokka 1.4.* is an alpha project"
2931
def warn(msg: String): Unit =
30-
warnings += 1
31-
report.warning(msg)
32+
if msg != dokkaAlphaWarning then
33+
warnings += 1
34+
report.warning(msg)
3235

3336
private var errors = 0
3437
private var warnings = 0
@@ -80,12 +83,12 @@ object Scala3doc:
8083

8184
if (parsedArgs.output.exists()) IO.delete(parsedArgs.output)
8285

83-
run(updatedArgs, new Scala3DocDokkaLogger)
86+
run(updatedArgs)
8487
report.inform("Done")
8588
else report.error("Failure")
8689
reporter
8790

88-
private [dokka] def run(args: Args, logger: DokkaLogger = DokkaConsoleLogger.INSTANCE)(using DocContext) =
89-
new DokkaGenerator(new DottyDokkaConfig(args, summon[DocContext]), logger).generate()
91+
private [dokka] def run(args: Args)(using DocContext) =
92+
new DokkaGenerator(new DottyDokkaConfig(args, summon[DocContext]), new Scala3DocDokkaLogger).generate()
9093

9194

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package dotty.dokka
2+
3+
import java.nio.file.Files
4+
import java.nio.file.Path
5+
import java.nio.file.Paths
6+
import org.junit.Test
7+
import org.junit.Assert
8+
import org.jsoup.Jsoup
9+
import org.jsoup.nodes.Document
10+
import java.nio.charset.Charset
11+
import dotty.tools.dotc.core.Contexts._
12+
13+
class ReportingTest:
14+
import Scala3doc.Args
15+
16+
private def checkReportedDiagnostics(
17+
newArgs: Args => Args = identity,
18+
ctx: Context = testContext)(
19+
op: ReportedDiagnostics => Unit): Unit =
20+
21+
val dest = Files.createTempDirectory("test-doc")
22+
try
23+
val args = Args(
24+
name = "Test Project Name",
25+
output = dest.toFile,
26+
tastyFiles = tastyFiles("nested") // Random package
27+
)
28+
Scala3doc.run(newArgs(args))(using ctx)
29+
op(ctx.reportedDiagnostics)
30+
31+
finally IO.delete(dest.toFile)
32+
33+
@Test
34+
def noMessageForMostCases = checkReportedDiagnostics(){ diag =>
35+
assertNoWarning(diag)
36+
assertNoErrors(diag)
37+
assertNoInfos(diag)
38+
}
39+
40+
@Test
41+
def noClassesErors = checkReportedDiagnostics(_.copy(tastyFiles = Nil)){ diag =>
42+
assertMessagesAbout(diag.errorMsgs)("classes should no be empty")
43+
}
44+
45+
@Test
46+
def errorsInCaseOfIncompletClasspath =
47+
val notTasty = Files.createTempFile("broken", ".notTasty")
48+
try
49+
Files.write(notTasty, "Random file".getBytes)
50+
checkReportedDiagnostics(a => a.copy(tastyFiles = notTasty.toFile +: a.tastyFiles)){ diag =>
51+
assertMessagesAbout(diag.errorMsgs)("File extension is not `tasty` or `jar`")
52+
}
53+
finally Files.delete(notTasty)
54+
55+
@Test
56+
def verbosePrintsDokkaMessage =
57+
val ctx = testContext
58+
ctx.setSetting(ctx.settings.verbose, true)
59+
checkReportedDiagnostics(ctx = ctx){ diag =>
60+
assertNoWarning(diag)
61+
assertNoErrors(diag)
62+
63+
assertMessagesAbout(diag.infoMsgs)("dotty.dokka.DottyDokkaPlugin", "generation completed successfully")
64+
}

scala3doc/test/dotty/dokka/ScaladocTest.scala

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import scala.jdk.CollectionConverters.{ListHasAsScala, SeqHasAsJava}
1111
import org.junit.{Test, Rule}
1212
import org.junit.rules.{TemporaryFolder, ErrorCollector}
1313
import java.io.File
14+
import dotty.tools.dotc.core.Contexts._
1415

1516
abstract class ScaladocTest(val name: String):
1617
def assertions: Seq[Assertion]
@@ -22,22 +23,11 @@ abstract class ScaladocTest(val name: String):
2223

2324
private def args = Scala3doc.Args(
2425
name = "test",
25-
tastyFiles = tastyFiles.map(new File(_)),
26+
tastyFiles = tastyFiles(name),
2627
output = getTempDir().getRoot,
2728
projectVersion = Some("1.0")
2829
)
2930

30-
private def tastyFiles =
31-
def listFilesSafe(dir: File) = Option(dir.listFiles).getOrElse {
32-
throw AssertionError(s"$dir not found. The test name is incorrect or scala3doc-testcases were not recompiled.")
33-
}
34-
def collectFiles(dir: File): List[String] = listFilesSafe(dir).toList.flatMap {
35-
case f if f.isDirectory => collectFiles(f)
36-
case f if f.getName endsWith ".tasty" => f.getAbsolutePath :: Nil
37-
case _ => Nil
38-
}
39-
collectFiles(File(s"${BuildInfo.test_testcasesOutputDir}/tests/$name"))
40-
4131
@Rule
4232
def collector = _collector
4333
private val _collector = new ErrorCollector();
@@ -46,8 +36,8 @@ abstract class ScaladocTest(val name: String):
4636
@Test
4737
def executeTest =
4838
DokkaTestGenerator(
49-
DottyDokkaConfig(args),
50-
TestLogger(DokkaConsoleLogger.INSTANCE),
39+
DottyDokkaConfig(args, testContext),
40+
TestLogger(new Scala3DocDokkaLogger(using testContext)),
5141
assertions.asTestMethods,
5242
Nil.asJava
5343
).generate()

scala3doc/test/dotty/dokka/site/SiteGeneratationTest.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import org.junit.Assert._
99
import org.jsoup.Jsoup
1010
import org.jsoup.nodes.Document
1111
import java.nio.charset.Charset
12-
12+
import dotty.tools.dotc.core.Contexts._
1313

1414
class SiteGeneratationTest:
1515
val projectName = "Test Project Name"
@@ -24,10 +24,10 @@ class SiteGeneratationTest:
2424
docsRoot = Some(base.toAbsolutePath.toString),
2525
projectVersion = Some(projectVersion),
2626
)
27-
Scala3doc.run(args)
27+
Scala3doc.run(args)(using testContext)
2828
op(dest)
2929

30-
finally println(dest.toFile) // IO.delete(dest.toFile)
30+
finally IO.delete(dest.toFile)
3131

3232
val testDocPath = Paths.get(BuildInfo.testDocumentationRoot)
3333

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package dotty.dokka
2+
3+
import dotty.tools.dotc.core.Contexts._
4+
import dotty.tools.dotc.reporting.Diagnostic
5+
import dotty.tools.dotc.reporting.ConsoleReporter
6+
import dotty.tools.dotc.interfaces.Diagnostic.{ERROR, INFO, WARNING}
7+
import org.junit.Assert._
8+
import java.io.File
9+
10+
case class ReportedDiagnostics(errors: List[Diagnostic], warnings: List[Diagnostic], infos: List[Diagnostic]):
11+
def errorMsgs = errors.map(_.msg.rawMessage)
12+
def warningMsgs = warnings.map(_.msg.rawMessage)
13+
def infoMsgs = infos.map(_.msg.rawMessage)
14+
15+
16+
extension (c: Context) def reportedDiagnostics: ReportedDiagnostics =
17+
val t = c.reporter.asInstanceOf[TestReporter]
18+
ReportedDiagnostics(t.errors.result, t.warnings.result, t.infos.result)
19+
20+
def assertNoWarning(diag: ReportedDiagnostics) = assertEquals("Warnings should be empty", Nil, diag.warningMsgs)
21+
def assertNoErrors(diag: ReportedDiagnostics) = assertEquals("Erros should be empty", Nil, diag.errorMsgs)
22+
def assertNoInfos(diag: ReportedDiagnostics) = assertEquals("Infos should be empty", Nil, diag.infoMsgs)
23+
24+
def assertMessagesAbout(messages: Seq[String])(patterns: String*) =
25+
patterns.foldLeft(messages){ (toCheck, pattern) =>
26+
val (matching, rest) = toCheck.partition(_.contains(pattern))
27+
assertTrue(
28+
s"Unable to find messages matching `$pattern`in $toCheck" +
29+
" (not that some methods may be filtered out by previous patterns",
30+
matching.nonEmpty
31+
)
32+
rest
33+
}
34+
35+
class TestReporter extends ConsoleReporter:
36+
val errors = List.newBuilder[Diagnostic]
37+
val warnings = List.newBuilder[Diagnostic]
38+
val infos = List.newBuilder[Diagnostic]
39+
40+
41+
override def doReport(dia: Diagnostic)(using Context): Unit = dia.level match
42+
case INFO =>
43+
infos += dia
44+
case ERROR =>
45+
errors += dia
46+
super.doReport(dia)
47+
case WARNING =>
48+
warnings += dia
49+
super.doReport(dia)
50+
51+
def testContext = (new ContextBase).initialCtx.fresh.setReporter(new TestReporter)
52+
53+
def tastyFiles(name: String) =
54+
def listFilesSafe(dir: File) = Option(dir.listFiles).getOrElse {
55+
throw AssertionError(s"$dir not found. The test name is incorrect or scala3doc-testcases were not recompiled.")
56+
}
57+
def collectFiles(dir: File): List[File] = listFilesSafe(dir).toList.flatMap {
58+
case f if f.isDirectory => collectFiles(f)
59+
case f if f.getName endsWith ".tasty" => f :: Nil
60+
case _ => Nil
61+
}
62+
collectFiles(File(s"${BuildInfo.test_testcasesOutputDir}/tests/$name"))

0 commit comments

Comments
 (0)