|
| 1 | +/* |
| 2 | + * Copyright 2014-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | +package org.jetbrains.dokka.gradle |
| 5 | + |
| 6 | +import io.kotest.core.spec.style.FunSpec |
| 7 | +import io.kotest.core.test.TestScope |
| 8 | +import io.kotest.matchers.collections.shouldBeEmpty |
| 9 | +import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder |
| 10 | +import io.kotest.matchers.file.shouldExist |
| 11 | +import org.gradle.testkit.runner.TaskOutcome.SUCCESS |
| 12 | +import org.jetbrains.dokka.gradle.internal.DokkaConstants.DOKKA_VERSION |
| 13 | +import org.jetbrains.dokka.gradle.utils.* |
| 14 | +import kotlin.io.path.invariantSeparatorsPathString |
| 15 | +import kotlin.io.path.name |
| 16 | +import kotlin.io.path.relativeToOrSelf |
| 17 | + |
| 18 | +class DumpDokkaConfigurationTest : FunSpec({ |
| 19 | + |
| 20 | + context("enabling dumpDokkaConfigurationDebugFile should dump configuration to file") { |
| 21 | + |
| 22 | + val project = initProject() |
| 23 | + |
| 24 | + project.gradleProperties { |
| 25 | + dokka { |
| 26 | + dumpDokkaConfigurationDebugFile = true |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + project.runner |
| 31 | + .addArguments( |
| 32 | + ":dokkaGenerateModuleHtml", |
| 33 | + ":dokkaGeneratePublicationHtml", |
| 34 | + "--rerun-tasks", |
| 35 | + ) |
| 36 | + .forwardOutput() |
| 37 | + .build { |
| 38 | + test("expect dokka tasks run successfully") { |
| 39 | + shouldHaveTasksWithOutcome( |
| 40 | + ":dokkaGenerateModuleHtml" to SUCCESS, |
| 41 | + ":dokkaGeneratePublicationHtml" to SUCCESS, |
| 42 | + ) |
| 43 | + } |
| 44 | + |
| 45 | + test("expect dokka tasks generate config files") { |
| 46 | + project.dir("build/tmp") |
| 47 | + .findFiles { it.name == "dokka-configuration.json" } |
| 48 | + .map { it.relativeToOrSelf(project.projectDir).invariantSeparatorsPathString } |
| 49 | + .toList() |
| 50 | + .shouldContainExactlyInAnyOrder( |
| 51 | + "build/tmp/dokkaGeneratePublicationHtml/dokka-configuration.json", |
| 52 | + "build/tmp/dokkaGenerateModuleHtml/dokka-configuration.json" |
| 53 | + ) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + context("implicit task outputs should not contain Dokka config file") { |
| 59 | + listOf( |
| 60 | + "dumpDokkaConfigurationDebugFile is not set" to null, |
| 61 | + "dumpDokkaConfigurationDebugFile is disabled" to false, |
| 62 | + ).forEach { (testName, value) -> |
| 63 | + context("when $testName ") { |
| 64 | + val project = initProject() |
| 65 | + |
| 66 | + project.gradleProperties { |
| 67 | + dokka { |
| 68 | + dumpDokkaConfigurationDebugFile = value |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + project.runner |
| 73 | + .forwardOutput() |
| 74 | + .addArguments( |
| 75 | + ":syncDokkaOutput", |
| 76 | + "--rerun", |
| 77 | + ) |
| 78 | + .build { |
| 79 | + test("expect syncDokkaOutput runs successfully") { |
| 80 | + shouldHaveTasksWithOutcome( |
| 81 | + ":syncDokkaOutput" to SUCCESS, |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + test("verify sync task collects Dokka output files") { |
| 86 | + // Just a simple check to make sure that some files were copied, because testing the config |
| 87 | + // files don't exist could mean that nothing was copied and something else broke. |
| 88 | + project.file("build/tmp/syncDokkaOutput/module/module-descriptor.json") |
| 89 | + .toFile() |
| 90 | + .shouldExist() |
| 91 | + project.file("build/tmp/syncDokkaOutput/publication/index.html") |
| 92 | + .toFile() |
| 93 | + .shouldExist() |
| 94 | + } |
| 95 | + |
| 96 | + test("expect no Dokka config file in output") { |
| 97 | + project |
| 98 | + .dir("build/tmp/syncDokkaOutput") |
| 99 | + .findFiles { it.name == "dokka-configuration.json" } |
| 100 | + .map { it.relativeToOrSelf(project.projectDir).invariantSeparatorsPathString } |
| 101 | + .toList() |
| 102 | + .shouldBeEmpty() |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +}) |
| 109 | + |
| 110 | +private fun TestScope.initProject( |
| 111 | + config: GradleProjectTest.() -> Unit = {}, |
| 112 | +): GradleProjectTest { |
| 113 | + val baseDirName = testCase.descriptor.ids() |
| 114 | + .joinToString("/") { it.value.substringAfter("org.jetbrains.dokka.gradle.").replaceNonAlphaNumeric() } |
| 115 | + |
| 116 | + return gradleKtsProjectTest( |
| 117 | + projectLocation = baseDirName, |
| 118 | + rootProjectName = "DumpDokkaConfigurationTest", |
| 119 | + ) { |
| 120 | + buildGradleKts = """ |
| 121 | + |plugins { |
| 122 | + | kotlin("jvm") version embeddedKotlinVersion |
| 123 | + | id("org.jetbrains.dokka") version "$DOKKA_VERSION" |
| 124 | + |} |
| 125 | + | |
| 126 | + |val syncDokkaOutput by tasks.registering(Sync::class) { |
| 127 | + | // fetch the implicit output files of the DokkaGenerate tasks |
| 128 | + | from(tasks.dokkaGeneratePublicationHtml) { |
| 129 | + | into("publication") |
| 130 | + | } |
| 131 | + | from(tasks.dokkaGenerateModuleHtml) { |
| 132 | + | into("module") |
| 133 | + | } |
| 134 | + | into(temporaryDir) |
| 135 | + |} |
| 136 | + | |
| 137 | + """.trimMargin() |
| 138 | + |
| 139 | + createKotlinFile("src/main/kotlin/Foo.kt", "class Foo") |
| 140 | + |
| 141 | + config() |
| 142 | + } |
| 143 | +} |
0 commit comments