1 package org.jetbrains.dokka.gradle
2
3
4 import com.intellij.rt.execution.junit.FileComparisonFailure
5 import org.gradle.testkit.runner.GradleRunner
6 import org.junit.Rule
7 import org.junit.rules.TemporaryFolder
8 import java.io.File
9 import java.nio.file.Files
10 import java.nio.file.Paths
11
12
13 val testDataFolder = Paths.get("testData")
14
15 val pluginClasspathData = Paths.get("build", "createClasspathManifest", "dokka-plugin-classpath.txt")
16 val androidPluginClasspathData = pluginClasspathData.resolveSibling("android-dokka-plugin-classpath.txt")
17
18 val dokkaFatJarPathData = pluginClasspathData.resolveSibling("fatjar.txt")
19
<lambda>null20 val androidLocalProperties = testDataFolder.resolve("android.local.properties").let { if (Files.exists(it)) it else null }
21
22 abstract class AbstractDokkaGradleTest {
23 @get:Rule val testProjectDir = TemporaryFolder()
24
<lambda>null25 open val pluginClasspath: List<File> = pluginClasspathData.toFile().readLines().map { File(it) }
26
checkOutputStructurenull27 fun checkOutputStructure(expected: String, actualSubpath: String) {
28 val expectedPath = testDataFolder.resolve(expected)
29 val actualPath = testProjectDir.root.toPath().resolve(actualSubpath).normalize()
30
31 assertEqualsIgnoringSeparators(expectedPath.toFile(), buildString {
32 actualPath.toFile().writeStructure(this, File(actualPath.toFile(), "."))
33 })
34 }
35
checkNoErrorClassesnull36 fun checkNoErrorClasses(actualSubpath: String, extension: String = "html", errorClassMarker: String = "ERROR CLASS") {
37 val actualPath = testProjectDir.root.toPath().resolve(actualSubpath).normalize()
38 var checked = 0
39 Files.walk(actualPath).filter { Files.isRegularFile(it) && it.fileName.toString().endsWith(".$extension") }.forEach {
40 val text = it.toFile().readText()
41
42 val noErrorClasses = text.replace(errorClassMarker, "?!")
43
44 if (noErrorClasses != text) {
45 throw FileComparisonFailure("", noErrorClasses, text, null)
46 }
47
48 checked++
49 }
50 println("$checked files checked for error classes")
51 }
52
checkNoUnresolvedLinksnull53 fun checkNoUnresolvedLinks(actualSubpath: String, extension: String = "html", marker: Regex = "[\"']#[\"']".toRegex()) {
54 val actualPath = testProjectDir.root.toPath().resolve(actualSubpath).normalize()
55 var checked = 0
56 Files.walk(actualPath).filter { Files.isRegularFile(it) && it.fileName.toString().endsWith(".$extension") }.forEach {
57 val text = it.toFile().readText()
58
59 val noErrorClasses = text.replace(marker, "?!")
60
61 if (noErrorClasses != text) {
62 throw FileComparisonFailure("", noErrorClasses, text, null)
63 }
64
65 checked++
66 }
67 println("$checked files checked for unresolved links")
68 }
69
checkExternalLinknull70 fun checkExternalLink(actualSubpath: String, linkBody: String, fullLink: String, extension: String = "html") {
71 val match = "!!match!!"
72 val notMatch = "!!not-match!!"
73
74 val actualPath = testProjectDir.root.toPath().resolve(actualSubpath).normalize()
75 var checked = 0
76 var totalEntries = 0
77 Files.walk(actualPath).filter { Files.isRegularFile(it) && it.fileName.toString().endsWith(".$extension") }.forEach {
78 val text = it.toFile().readText()
79
80 val textWithoutMatches = text.replace(fullLink, match)
81
82 val textWithoutNonMatches = textWithoutMatches.replace(linkBody, notMatch)
83
84 if (textWithoutNonMatches != textWithoutMatches) {
85
86 val expected = textWithoutNonMatches.replace(notMatch, fullLink).replace(match, fullLink)
87 val actual = textWithoutMatches.replace(match, fullLink)
88
89 throw FileComparisonFailure("", expected, actual, null)
90 }
91 if (text != textWithoutMatches)
92 totalEntries++
93
94 checked++
95 }
96 println("$checked files checked for valid external links '$linkBody', found $totalEntries links")
97 }
98
configurenull99 fun configure(gradleVersion: String = "3.5", kotlinVersion: String = "1.1.2", arguments: Array<String>): GradleRunner {
100 val fatjar = dokkaFatJarPathData.toFile().readText()
101
102 return GradleRunner.create().withProjectDir(testProjectDir.root)
103 .withArguments("-Pdokka_fatjar=$fatjar", "-Ptest_kotlin_version=$kotlinVersion", *arguments)
104 .withPluginClasspath(pluginClasspath)
105 .withGradleVersion(gradleVersion)
106 .withDebug(true)
107 }
108 }