|
| 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 | + |
0 commit comments