Skip to content

Commit 5abd9d9

Browse files
committed
Add automated test to check if sourcelinks are correctly pointing to remote repository
1 parent 1803076 commit 5abd9d9

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

project/Build.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,10 @@ object Build {
15931593
Build.testcasesOutputDir.in(Test).value,
15941594
"scaladoc testcases",
15951595
"scaladoc/output/testcases",
1596-
"master")
1596+
"master",
1597+
Seq(
1598+
"-source-links:github://lampepfl/dotty"
1599+
))
15971600
}.value,
15981601

15991602
buildInfoKeys in Test := Seq[BuildInfoKey](

scaladoc/test/dotty/tools/scaladoc/ScaladocTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ abstract class ScaladocTest(val name: String):
2525
name = "test",
2626
tastyFiles = tastyFiles(name),
2727
output = getTempDir().getRoot,
28-
projectVersion = Some("1.0")
28+
projectVersion = Some("1.0"),
29+
sourceLinks = List("github://lampepfl/dotty/master")
2930
)
3031

3132
@Test
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package dotty.tools.scaladoc
2+
package sourcelinks
3+
4+
import scala.io.Source
5+
import scala.jdk.CollectionConverters._
6+
import scala.util.matching.Regex
7+
import dotty.tools.scaladoc.test.BuildInfo
8+
import java.nio.file.Path;
9+
import org.jsoup.Jsoup
10+
import util.IO
11+
import org.junit.Assert.assertTrue
12+
13+
class RemoteLinksTest extends ScaladocTest(""): // empty since it is unused in our case
14+
15+
def runTest = afterRendering {
16+
val mtsl = membersToSourceLinks
17+
val pageToMtsl: Map[String, List[(String, String)]] = mtsl.groupMap(_._2.split("#L").head)(v => (v._1, v._2.split("#L").last))
18+
pageToMtsl.foreach { case (link, members) =>
19+
try
20+
val doc = Jsoup.connect(link).get
21+
members.foreach { (member, line) =>
22+
if !member.startsWith("given_") then // TODO: handle synthetic givens, for now we disable them from testing
23+
val loc = doc.select(s"#LC$line").text
24+
assertTrue(s"Expected to find $member at $link at line $line", loc.contains(member))
25+
}
26+
catch
27+
case e: org.jsoup.HttpStatusException => e.getStatusCode match
28+
case 404 => throw AssertionError(s"Page $link does not exists")
29+
case n => report.warning(s"Could not open link for $link, return code $n")
30+
}
31+
}
32+
33+
private def membersToSourceLinks(using DocContext): List[(String, String)] =
34+
val output = summon[DocContext].args.output.toPath.resolve("api")
35+
val mtsl = List.newBuilder[(String, String)]
36+
def processFile(path: Path): Unit =
37+
val document = Jsoup.parse(IO.read(path))
38+
document.select(".documentableElement").remove()
39+
document.select("dt").forEach { elem =>
40+
val content = elem.text
41+
if content == "Source" then
42+
mtsl += document.select("h1").first.text -> elem.nextSibling.childNode(0).attr("href")
43+
}
44+
IO.foreachFileIn(output, processFile)
45+
mtsl.result
46+
47+

scaladoc/test/dotty/tools/scaladoc/SourceLinksTests.scala renamed to scaladoc/test/dotty/tools/scaladoc/source-links/SourceLinksTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package dotty.tools.scaladoc
2+
package sourcelinks
23

34
import java.nio.file._
45
import org.junit.Assert._
@@ -156,4 +157,4 @@ class SourceLinksTest:
156157
("src/lib/core.scala", 33, edit) -> "https://gitlab.com/lampepfl/dotty/-/edit/develop/lib/core.scala#L33",
157158
("src/generated.scala", 33, edit) -> "https://gitlab.com/lampepfl/dotty/-/edit/develop/generated.scala#L33",
158159
("src/generated/template.scala", 1, edit) -> "/template.scala#1"
159-
)
160+
)

scaladoc/test/dotty/tools/scaladoc/testUtils.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class TestReporter extends ConsoleReporter:
5353
def testArgs(files: Seq[File] = Nil, dest: File = new File("notUsed")) = Scaladoc.Args(
5454
name = "Test Project Name",
5555
output = dest,
56-
tastyFiles = files,
56+
tastyFiles = files
5757
)
5858

5959
def testContext = (new ContextBase).initialCtx.fresh.setReporter(new TestReporter)

0 commit comments

Comments
 (0)