-
Notifications
You must be signed in to change notification settings - Fork 411
/
ExampleProjectsTest.kt
350 lines (302 loc) · 12.6 KB
/
ExampleProjectsTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright 2014-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package org.jetbrains.dokka.it.gradle.examples
import io.kotest.assertions.asClue
import io.kotest.assertions.withClue
import io.kotest.matchers.paths.shouldBeADirectory
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome.FROM_CACHE
import org.gradle.testkit.runner.TaskOutcome.UP_TO_DATE
import org.jetbrains.dokka.gradle.utils.*
import org.jetbrains.dokka.it.gradle.loadConfigurationCacheReportData
import org.jetbrains.dokka.it.systemProperty
import org.junit.jupiter.api.Assumptions.assumeTrue
import org.junit.jupiter.api.Named.named
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.api.io.TempDir
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.Arguments.arguments
import org.junit.jupiter.params.provider.ArgumentsProvider
import org.junit.jupiter.params.provider.ArgumentsSource
import java.nio.file.Path
import java.util.stream.Stream
import kotlin.io.path.*
import kotlin.streams.asStream
class ExampleProjectsTest {
class TestCaseProvider : ArgumentsProvider {
override fun provideArguments(context: ExtensionContext): Stream<Arguments> {
val exampleProjectDirs = exampleGradleProjectsDir
.listDirectoryEntries()
.filter { it.isDirectory() }
val temporaryDir = createTempDirectory()
return exampleProjectDirs
.asSequence()
.map { exampleProjectDir ->
val projectDestinationDir = temporaryDir.resolve(exampleProjectDir.name)
val exampleProject = initProject(exampleProjectDir, projectDestinationDir)
val expectedDataDir = expectedDataDir.resolve("exampleProjects").resolve(exampleProjectDir.name)
named(
exampleProjectDir.name,
TestCase(
project = exampleProject,
expectedDataDir = expectedDataDir,
)
)
}
.map { arguments(it) }
.asStream()
}
companion object {
/** Base directory that contains all Dokka Gradle example projects. */
private val exampleGradleProjectsDir by systemProperty(::Path)
/** Base directory that contains all expected output data for the Gradle example projects. */
private val expectedDataDir by systemProperty(::Path)
/** Create a new [GradleProjectTest] copied from the source project in [sourceProjectDir]. */
private fun initProject(
sourceProjectDir: Path,
destinationDir: Path,
): GradleProjectTest {
return GradleProjectTest(destinationDir).apply {
sourceProjectDir.copyToRecursively(projectDir, overwrite = true, followLinks = false)
updateSettingsRepositories()
}
}
}
}
data class TestCase(
val project: GradleProjectTest,
val expectedDataDir: Path,
) {
val exampleProjectName = ExampleProject.of(project.projectDir)
/** `true` if the project produces Dokka HTML. */
val outputsHtml: Boolean =
when (exampleProjectName) {
ExampleProject.Javadoc -> false
else -> true
}
/** `true` if the project produces Dokka Javadoc. */
val outputsJavadoc: Boolean =
when (exampleProjectName) {
ExampleProject.Javadoc -> true
ExampleProject.LibraryPublishing -> true
else -> false
}
/** Base directory for the */
private val dokkaOutputPath: String =
when (exampleProjectName) {
ExampleProject.VersioningMultimodule -> "docs/build/dokka/"
ExampleProject.Multimodule -> "docs/build/dokka/"
ExampleProject.CompositeBuild -> "docs/build/dokka/"
else -> "build/dokka/"
}
val dokkaOutputDir: Path = project.projectDir.resolve(dokkaOutputPath)
val dokkaGenerateTask: String = when (exampleProjectName) {
ExampleProject.VersioningMultimodule -> ":docs:dokkaGenerate"
ExampleProject.Multimodule -> ":docs:dokkaGenerate"
ExampleProject.CompositeBuild -> ":build"
else -> ":dokkaGenerate"
}
}
/**
* Identifier for each example project, to avoid string comparisons everywhere.
*/
enum class ExampleProject {
BasicGradle,
CompositeBuild,
CustomStyling,
Java,
Javadoc,
KotlinAsJava,
KotlinMultiplatform,
LibraryPublishing,
Multimodule,
VersioningMultimodule,
;
companion object {
fun of(dir: Path): ExampleProject {
return when (dir.name) {
"basic-gradle-example" -> BasicGradle
"javadoc-example" -> Javadoc
"composite-build-example" -> CompositeBuild
"custom-styling-example" -> CustomStyling
"java-example" -> Java
"kotlin-as-java-example" -> KotlinAsJava
"kotlin-multiplatform-example" -> KotlinMultiplatform
"library-publishing-example" -> LibraryPublishing
"multimodule-example" -> Multimodule
"versioning-multimodule-example" -> VersioningMultimodule
else -> error("Undeclared example project: $dir")
}
}
}
}
@ParameterizedTest
@ArgumentsSource(TestCaseProvider::class)
fun `test HTML output`(testCase: TestCase) {
assumeTrue(testCase.outputsHtml)
testDokkaOutput(
testCase = testCase,
format = "html",
)
}
@ParameterizedTest
@ArgumentsSource(TestCaseProvider::class)
fun `test Javadoc output`(testCase: TestCase) {
assumeTrue(testCase.outputsJavadoc)
testDokkaOutput(
testCase = testCase,
format = "javadoc",
)
}
private fun testDokkaOutput(
testCase: TestCase,
format: String,
) {
val expectedDataDir = testCase.expectedDataDir.resolve(format)
val dokkaOutputDir = testCase.dokkaOutputDir.resolve(format)
assert(expectedDataDir.isDirectory()) {
"Missing expectedDataDir: ${expectedDataDir.toUri()}"
}
testCase.project.runner
.addArguments(
testCase.dokkaGenerateTask,
"--stacktrace",
)
.build {
dokkaOutputDir.shouldBeADirectory()
withClue({
"""
expectedDataDir: ${expectedDataDir.toUri()}
actualOutputDir: ${dokkaOutputDir.toUri()}
""".trimIndent()
}) {
withClue("expect file trees are the same") {
val expectedFileTree = expectedDataDir.toTreeString()
val actualFileTree = dokkaOutputDir.toTreeString()
expectedFileTree shouldBe actualFileTree
}
withClue("expect directories are the same") {
dokkaOutputDir shouldBeADirectoryWithSameContentAs expectedDataDir
}
}
}
}
@ParameterizedTest
@ArgumentsSource(TestCaseProvider::class)
fun `test build cache`(
testCase: TestCase,
@TempDir tempDir: Path,
) {
val buildCacheDir = tempDir.resolve("build-cache").apply {
deleteRecursively()
createDirectories()
}
testCase.project.settingsGradleKts += """
|buildCache {
| local {
| directory = File("${buildCacheDir.invariantSeparatorsPathString}")
| }
|}
""".trimMargin()
// Initial build, to populate the build cache.
testCase.project.runner
.addArguments(
testCase.dokkaGenerateTask,
"--stacktrace",
)
.build {
output shouldContain "BUILD SUCCESSFUL"
}
// Clean local directories, to ensure tasks are loaded from build cache.
testCase.project.runner
.addArguments(
"clean",
"--stacktrace",
)
.build {
output shouldContain "BUILD SUCCESSFUL"
}
// Re-run generate, and verify that the Dokka tasks are cached.
testCase.project.runner
.addArguments(
testCase.dokkaGenerateTask,
"--stacktrace",
"--build-cache",
)
.build {
when (testCase.exampleProjectName) {
ExampleProject.Javadoc -> {
shouldHaveTasksWithOutcome(
":dokkaGeneratePublicationJavadoc" to FROM_CACHE,
":dokkaGenerate" to UP_TO_DATE,
)
}
ExampleProject.CompositeBuild -> {
shouldHaveTasksWithOutcome(
":module-kakapo:dokkaGenerateModuleHtml" to FROM_CACHE,
":module-kea:dokkaGenerateModuleHtml" to FROM_CACHE,
":docs:dokkaGeneratePublicationHtml" to FROM_CACHE,
":docs:dokkaGenerate" to UP_TO_DATE,
)
}
ExampleProject.Multimodule -> {
shouldHaveTasksWithOutcome(
":childProjectA:dokkaGenerateModuleHtml" to FROM_CACHE,
":childProjectB:dokkaGenerateModuleHtml" to FROM_CACHE,
":docs:dokkaGeneratePublicationHtml" to FROM_CACHE,
":docs:dokkaGenerate" to UP_TO_DATE,
)
}
ExampleProject.VersioningMultimodule -> {
shouldHaveTasksWithOutcome(
":childProjectA:dokkaGenerateModuleHtml" to FROM_CACHE,
":childProjectB:dokkaGenerateModuleHtml" to FROM_CACHE,
":docs:dokkaGeneratePublicationHtml" to FROM_CACHE,
":docs:dokkaGenerate" to UP_TO_DATE,
)
}
else -> {
shouldHaveTasksWithOutcome(
":dokkaGeneratePublicationHtml" to FROM_CACHE,
":dokkaGenerate" to UP_TO_DATE,
)
}
}
}
}
@ParameterizedTest
@ArgumentsSource(TestCaseProvider::class)
fun `test configuration cache`(testCase: TestCase) {
// delete old configuration cache results and reports, to make sure we can fetch the newest report
testCase.project.findFiles {
val isCCDir = it.invariantSeparatorsPathString.endsWith(".gradle/configuration-cache")
val isCCReportDir = it.invariantSeparatorsPathString.endsWith("build/reports/configuration-cache")
it.isDirectory() && (isCCReportDir || isCCDir)
}.forEach { it.deleteRecursively() }
val configCacheRunner: GradleRunner =
testCase.project.runner
.addArguments(
testCase.dokkaGenerateTask,
"--stacktrace",
"--configuration-cache",
)
//first build should store the configuration cache
configCacheRunner.build {
output shouldContain "BUILD SUCCESSFUL"
output shouldContain "Configuration cache entry stored"
loadConfigurationCacheReportData(projectDir = testCase.project.projectDir)
.asClue { ccReport ->
ccReport.totalProblemCount shouldBe 0
}
}
// second build should reuse the configuration cache
configCacheRunner.build {
output shouldContain "BUILD SUCCESSFUL"
output shouldContain "Configuration cache entry reused"
}
}
}