From e940349980c40f87309c9ae80593519952d9dd7f Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Fri, 10 Jul 2020 12:34:15 +0200 Subject: Implement basic assertNoUnresolvedHref --- .../jetbrains/dokka/it/AbstractIntegrationTest.kt | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'integration-tests/src/main/kotlin/org/jetbrains/dokka/it') diff --git a/integration-tests/src/main/kotlin/org/jetbrains/dokka/it/AbstractIntegrationTest.kt b/integration-tests/src/main/kotlin/org/jetbrains/dokka/it/AbstractIntegrationTest.kt index 4af523a2..0a83045e 100644 --- a/integration-tests/src/main/kotlin/org/jetbrains/dokka/it/AbstractIntegrationTest.kt +++ b/integration-tests/src/main/kotlin/org/jetbrains/dokka/it/AbstractIntegrationTest.kt @@ -1,9 +1,11 @@ package org.jetbrains.dokka.it +import org.jsoup.Jsoup import org.junit.Rule import org.junit.rules.TemporaryFolder import java.io.File import kotlin.test.assertFalse +import kotlin.test.assertTrue abstract class AbstractIntegrationTest { @@ -36,4 +38,30 @@ abstract class AbstractIntegrationTest { "Unexpected unresolved link in ${file.path}\n" + fileText ) } + + protected fun assertNoHrefToMissingLocalFileOrDirectory( + file: File, fileExtensions: Set = setOf("html") + ) { + val fileText = file.readText() + val html = Jsoup.parse(fileText) + html.allElements.toList().forEach { element -> + val href = element.attr("href") ?: return@forEach + + if (href.startsWith("#")) return@forEach + if (href.startsWith("https")) return@forEach + if (href.startsWith("http")) return@forEach + + val targetFile = File(file.parent, href) + if (targetFile.extension.isNotEmpty() && targetFile.extension !in fileExtensions) return@forEach + + if ( + targetFile.extension.isEmpty() || targetFile.extension == "html" && !href.startsWith("#")) { + assertTrue( + targetFile.exists(), + "${file.relativeTo(projectDir).path}: href=\"$href\"\n" + + "file does not exist: ${targetFile.relativeTo(projectDir).path}" + ) + } + } + } } -- cgit