diff options
Diffstat (limited to 'integration-tests/src')
3 files changed, 138 insertions, 0 deletions
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 new file mode 100644 index 00000000..aeebe552 --- /dev/null +++ b/integration-tests/src/main/kotlin/org/jetbrains/dokka/it/AbstractIntegrationTest.kt @@ -0,0 +1,71 @@ +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 { + + @get:Rule + val temporaryTestFolder = TemporaryFolder() + + val projectDir get() = File(temporaryTestFolder.root, "project") + + fun File.allDescendentsWithExtension(extension: String): Sequence<File> { + return this.walkTopDown().filter { it.isFile && it.extension == extension } + } + + fun File.allHtmlFiles(): Sequence<File> { + return allDescendentsWithExtension("html") + } + + protected fun assertContainsNoErrorClass(file: File) { + val fileText = file.readText() + assertFalse( + fileText.contains("ERROR CLASS", ignoreCase = true), + "Unexpected `ERROR CLASS` in ${file.path}\n" + fileText + ) + } + + protected fun assertNoUnresolvedLInks(file: File) { + val regex = Regex("[\"']#[\"']") + val fileText = file.readText() + assertFalse( + fileText.contains(regex), + "Unexpected unresolved link in ${file.path}\n" + fileText + ) + } + + protected fun assertNoHrefToMissingLocalFileOrDirectory( + file: File, fileExtensions: Set<String> = 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("https")) return@forEach + if (href.startsWith("http")) return@forEach + + val hrefWithoutAnchors = if (href.contains("#")) { + val hrefSplits = href.split("#") + if (hrefSplits.count() != 2) return@forEach + hrefSplits.first() + } else href + + val targetFile = File(file.parent, hrefWithoutAnchors) + 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}" + ) + } + } + } +} diff --git a/integration-tests/src/main/kotlin/org/jetbrains/dokka/it/environmentUtils.kt b/integration-tests/src/main/kotlin/org/jetbrains/dokka/it/environmentUtils.kt new file mode 100644 index 00000000..eadf5a8c --- /dev/null +++ b/integration-tests/src/main/kotlin/org/jetbrains/dokka/it/environmentUtils.kt @@ -0,0 +1,16 @@ +package org.jetbrains.dokka.it + +import java.io.File + +/** + * Indicating whether or not the current machine executing the test is a CI + */ +val isCI: Boolean get() = System.getenv("CI") == "true" + +val isAndroidSdkInstalled: Boolean = System.getenv("ANDROID_SDK_ROOT") != null || + System.getenv("ANDROID_HOME") != null + +val isMavenInstalled: Boolean = System.getenv("PATH").orEmpty() + .split(File.pathSeparator) + .flatMap { pathElement -> File(pathElement).listFiles().orEmpty().toList() } + .any { pathElement -> "mvn" == pathElement.name } diff --git a/integration-tests/src/main/kotlin/org/jetbrains/dokka/it/processUtils.kt b/integration-tests/src/main/kotlin/org/jetbrains/dokka/it/processUtils.kt new file mode 100644 index 00000000..d2c048ac --- /dev/null +++ b/integration-tests/src/main/kotlin/org/jetbrains/dokka/it/processUtils.kt @@ -0,0 +1,51 @@ +package org.jetbrains.dokka.it + +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.async +import kotlinx.coroutines.runBlocking +import kotlin.concurrent.thread + +class ProcessResult( + val exitCode: Int, + val output: String +) + +fun Process.awaitProcessResult() = runBlocking { + val exitCode = async { awaitExitCode() } + val output = async { awaitOutput() } + ProcessResult( + exitCode.await(), + output.await() + ) +} + +private suspend fun Process.awaitExitCode(): Int { + val deferred = CompletableDeferred<Int>() + thread { + try { + deferred.complete(this.waitFor()) + } catch (e: Throwable) { + deferred.completeExceptionally(e) + } + } + + return deferred.await() +} + +private suspend fun Process.awaitOutput(): String { + val deferred = CompletableDeferred<String>() + thread { + try { + var string = "" + this.inputStream.bufferedReader().forEachLine { line -> + println(line) + string += line + System.lineSeparator() + } + deferred.complete(string) + } catch (e: Throwable) { + deferred.completeExceptionally(e) + } + } + + return deferred.await() +} |