From 6af794d4173d6aef9b84dbc7647eb3a1a54677d5 Mon Sep 17 00:00:00 2001 From: aSemy <897017+aSemy@users.noreply.github.com> Date: Mon, 27 Feb 2023 16:50:38 +0100 Subject: Use Gradle file property types for task inputs & outputs (#2707) --- .../dokka/gradle/AbstractDokkaParentTaskTest.kt | 7 +- .../dokka/gradle/DokkaCollectorTaskTest.kt | 102 ------------------- .../dokka/gradle/DokkaConfigurationJsonTest.kt | 26 +++-- .../gradle/DokkaConfigurationSerializableTest.kt | 25 ++++- .../dokka/gradle/DokkaMultiModuleFileLayoutTest.kt | 26 ++++- .../gradle/KotlinDslDokkaTaskConfigurationTest.kt | 9 +- .../dokka/gradle/tasks/DokkaCollectorTaskTest.kt | 109 +++++++++++++++++++++ .../dokka/gradle/tasks/DokkaMultiModuleTaskTest.kt | 36 ++++--- .../jetbrains/dokka/gradle/tasks/DokkaTaskTest.kt | 15 ++- .../gradle/utils/samWithReceiverWorkarounds.kt | 69 +++++++++++++ 10 files changed, 281 insertions(+), 143 deletions(-) delete mode 100644 runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaCollectorTaskTest.kt create mode 100644 runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/tasks/DokkaCollectorTaskTest.kt create mode 100644 runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/utils/samWithReceiverWorkarounds.kt (limited to 'runners/gradle-plugin/src/test/kotlin/org') diff --git a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/AbstractDokkaParentTaskTest.kt b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/AbstractDokkaParentTaskTest.kt index 2cac940f..c5ea42a5 100644 --- a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/AbstractDokkaParentTaskTest.kt +++ b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/AbstractDokkaParentTaskTest.kt @@ -7,6 +7,7 @@ import org.gradle.kotlin.dsl.create import org.gradle.kotlin.dsl.getByName import org.gradle.testfixtures.ProjectBuilder import org.jetbrains.dokka.DokkaConfigurationImpl +import org.jetbrains.dokka.gradle.utils.subprojects_ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -19,7 +20,7 @@ class AbstractDokkaParentTaskTest { private val subSubproject0 = ProjectBuilder.builder().withName("subSubproject0").withParent(subproject0).build() init { - rootProject.subprojects { + rootProject.subprojects_ { tasks.create("dokkaTask") } } @@ -190,12 +191,10 @@ class AbstractDokkaParentTaskTest { } } -internal open class TestDokkaParentTask : AbstractDokkaParentTask() { +internal abstract class TestDokkaParentTask : AbstractDokkaParentTask() { override fun buildDokkaConfiguration(): DokkaConfigurationImpl { throw NotImplementedError() } } private val Project.dokkaTask: DokkaTask get() = tasks.getByName("dokkaTask") - - diff --git a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaCollectorTaskTest.kt b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaCollectorTaskTest.kt deleted file mode 100644 index b9c20dce..00000000 --- a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaCollectorTaskTest.kt +++ /dev/null @@ -1,102 +0,0 @@ -package org.jetbrains.dokka.gradle - -import org.gradle.kotlin.dsl.create -import org.gradle.kotlin.dsl.withType -import org.gradle.testfixtures.ProjectBuilder -import org.jetbrains.dokka.DokkaConfigurationImpl -import org.jetbrains.dokka.DokkaException -import java.io.File -import kotlin.test.* - -class DokkaCollectorTaskTest { - - @Test - fun buildDokkaConfiguration() { - val rootProject = ProjectBuilder.builder().build() - val childProject = ProjectBuilder.builder().withParent(rootProject).build() - childProject.plugins.apply("org.jetbrains.kotlin.jvm") - - rootProject.allprojects { - plugins.apply("org.jetbrains.dokka") - tasks.withType().configureEach { - plugins.withDependencies { clear() } - } - tasks.withType().configureEach { - dokkaSourceSets.configureEach { - classpath.setFrom(emptyList()) - } - } - } - - val collectorTasks = rootProject.tasks.withType() - collectorTasks.configureEach { - moduleName.set("custom Module Name") - outputDirectory.set(File("customOutputDirectory")) - cacheRoot.set(File("customCacheRoot")) - failOnWarning.set(true) - offlineMode.set(true) - } - - assertTrue(collectorTasks.isNotEmpty(), "Expected at least one collector task") - - collectorTasks.forEach { task -> - val dokkaConfiguration = task.buildDokkaConfiguration() - assertEquals( - DokkaConfigurationImpl( - moduleName = "custom Module Name", - outputDir = File("customOutputDirectory"), - cacheRoot = File("customCacheRoot"), - failOnWarning = true, - offlineMode = true, - sourceSets = task.childDokkaTasks - .map { it.buildDokkaConfiguration() } - .map { it.sourceSets } - .reduce { acc, list -> acc + list }, - pluginsClasspath = task.childDokkaTasks - .map { it.plugins.resolve().toList() } - .reduce { acc, mutableSet -> acc + mutableSet } - ), - dokkaConfiguration, - ) - } - } - - @Test - fun `verify that cacheRoot is optional, and not required to build DokkaConfiguration`() { - val rootProject = ProjectBuilder.builder().build() - val childProject = ProjectBuilder.builder().withParent(rootProject).build() - childProject.plugins.apply("org.jetbrains.kotlin.jvm") - - rootProject.allprojects { - plugins.apply("org.jetbrains.dokka") - tasks.withType().configureEach { - plugins.withDependencies { clear() } - } - tasks.withType().configureEach { - dokkaSourceSets.configureEach { - classpath.setFrom(emptyList()) - } - } - } - - val collectorTasks = rootProject.tasks.withType() - collectorTasks.configureEach { - cacheRoot.set(null as File?) - } - - assertTrue(collectorTasks.isNotEmpty(), "Expected at least one collector task") - - collectorTasks.forEach { task -> - val dokkaConfiguration = task.buildDokkaConfiguration() - assertNull(dokkaConfiguration.cacheRoot, "Expect that cacheRoot is null") - } - } - - @Test - fun `with no child tasks throws DokkaException`() { - val project = ProjectBuilder.builder().build() - val collectorTask = project.tasks.create("collector") - project.configurations.all { withDependencies { clear() } } - assertFailsWith { collectorTask.generateDocumentation() } - } -} diff --git a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaConfigurationJsonTest.kt b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaConfigurationJsonTest.kt index 108c7fe0..d01847a0 100644 --- a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaConfigurationJsonTest.kt +++ b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaConfigurationJsonTest.kt @@ -2,7 +2,13 @@ package org.jetbrains.dokka.gradle import org.gradle.kotlin.dsl.withType import org.gradle.testfixtures.ProjectBuilder -import org.jetbrains.dokka.* +import org.jetbrains.dokka.DokkaConfiguration +import org.jetbrains.dokka.DokkaConfigurationImpl +import org.jetbrains.dokka.PluginConfigurationImpl +import org.jetbrains.dokka.gradle.utils.create_ +import org.jetbrains.dokka.gradle.utils.externalDocumentationLink_ +import org.jetbrains.dokka.gradle.utils.withDependencies_ +import org.jetbrains.dokka.toCompactJsonString import java.io.File import java.net.URL import kotlin.test.Test @@ -15,23 +21,31 @@ class DokkaConfigurationJsonTest { val project = ProjectBuilder.builder().build() project.plugins.apply("org.jetbrains.dokka") val dokkaTask = project.tasks.withType().first() - dokkaTask.plugins.withDependencies { clear() } + dokkaTask.plugins.withDependencies_ { clear() } dokkaTask.apply { this.failOnWarning.set(true) this.offlineMode.set(true) this.outputDirectory.set(File("customOutputDir")) this.cacheRoot.set(File("customCacheRoot")) this.pluginsConfiguration.add( - PluginConfigurationImpl("A", DokkaConfiguration.SerializationFormat.JSON, """ { "key" : "value1" } """) + PluginConfigurationImpl( + "A", + DokkaConfiguration.SerializationFormat.JSON, + """ { "key" : "value1" } """ + ) ) this.pluginsConfiguration.add( - PluginConfigurationImpl("B", DokkaConfiguration.SerializationFormat.JSON, """ { "key" : "value2" } """) + PluginConfigurationImpl( + "B", + DokkaConfiguration.SerializationFormat.JSON, + """ { "key" : "value2" } """ + ) ) - this.dokkaSourceSets.create("main") { + this.dokkaSourceSets.create_("main") { displayName.set("customSourceSetDisplayName") reportUndocumented.set(true) - externalDocumentationLink { + externalDocumentationLink_ { packageListUrl.set(URL("http://some.url")) url.set(URL("http://some.other.url")) } diff --git a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaConfigurationSerializableTest.kt b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaConfigurationSerializableTest.kt index 75e2b63e..b5230114 100644 --- a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaConfigurationSerializableTest.kt +++ b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaConfigurationSerializableTest.kt @@ -4,6 +4,9 @@ import org.gradle.kotlin.dsl.withType import org.gradle.testfixtures.ProjectBuilder import org.jetbrains.dokka.DokkaConfiguration import org.jetbrains.dokka.PluginConfigurationImpl +import org.jetbrains.dokka.gradle.utils.create_ +import org.jetbrains.dokka.gradle.utils.externalDocumentationLink_ +import org.jetbrains.dokka.gradle.utils.withDependencies_ import org.junit.Rule import org.junit.rules.TemporaryFolder import java.io.File @@ -24,19 +27,31 @@ class DokkaConfigurationSerializableTest { val project = ProjectBuilder.builder().build() project.plugins.apply("org.jetbrains.dokka") val dokkaTask = project.tasks.withType().first() - dokkaTask.plugins.withDependencies { clear() } + dokkaTask.plugins.withDependencies_ { clear() } dokkaTask.apply { this.failOnWarning.set(true) this.offlineMode.set(true) this.outputDirectory.set(File("customOutputDir")) this.cacheRoot.set(File("customCacheRoot")) - this.pluginsConfiguration.add(PluginConfigurationImpl("A", DokkaConfiguration.SerializationFormat.JSON, """ { "key" : "value1" } """)) - this.pluginsConfiguration.add(PluginConfigurationImpl("B", DokkaConfiguration.SerializationFormat.JSON, """ { "key" : "value2" } """)) - this.dokkaSourceSets.create("main") { + this.pluginsConfiguration.add( + PluginConfigurationImpl( + "A", + DokkaConfiguration.SerializationFormat.JSON, + """ { "key" : "value1" } """ + ) + ) + this.pluginsConfiguration.add( + PluginConfigurationImpl( + "B", + DokkaConfiguration.SerializationFormat.JSON, + """ { "key" : "value2" } """ + ) + ) + this.dokkaSourceSets.create_("main") { displayName.set("customSourceSetDisplayName") reportUndocumented.set(true) - externalDocumentationLink { + externalDocumentationLink_ { packageListUrl.set(URL("http://some.url")) url.set(URL("http://some.other.url")) } diff --git a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaMultiModuleFileLayoutTest.kt b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaMultiModuleFileLayoutTest.kt index d00664b5..6f0ce8a2 100644 --- a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaMultiModuleFileLayoutTest.kt +++ b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaMultiModuleFileLayoutTest.kt @@ -16,12 +16,15 @@ class DokkaMultiModuleFileLayoutTest { @Test fun `no copy`() { val project = ProjectBuilder.builder().build() + project.plugins.apply("org.jetbrains.dokka") + val child = project.tasks.create("child") val parent = project.tasks.create("parent") child.outputDirectory.set(File("some/path")) assertEquals( - File("some/path"), NoCopy.targetChildOutputDirectory(parent, child), + File("some/path"), + NoCopy.targetChildOutputDirectory(parent, child).get().asFile.relativeTo(project.projectDir), "Expected original file path returned" ) } @@ -29,16 +32,21 @@ class DokkaMultiModuleFileLayoutTest { @Test fun `compact in parent`() { val rootProject = ProjectBuilder.builder().build() + val parentProject = ProjectBuilder.builder().withName("parent").withParent(rootProject).build() + parentProject.plugins.apply("org.jetbrains.dokka") + val intermediateProject = ProjectBuilder.builder().withName("intermediate").withParent(parentProject).build() val childProject = ProjectBuilder.builder().withName("child").withParent(intermediateProject).build() + childProject.plugins.apply("org.jetbrains.dokka") val parentTask = parentProject.tasks.create("parentTask") val childTask = childProject.tasks.create("childTask") val targetOutputDirectory = CompactInParent.targetChildOutputDirectory(parentTask, childTask) assertEquals( - parentTask.outputDirectory.get().resolve("intermediate/child"), targetOutputDirectory, + parentTask.outputDirectory.get().asFile.resolve("intermediate/child"), + targetOutputDirectory.get().asFile, "Expected nested file structure representing project structure" ) } @@ -47,17 +55,21 @@ class DokkaMultiModuleFileLayoutTest { fun copyChildOutputDirectory() { /* Prepare */ val project = ProjectBuilder.builder().build() + project.plugins.apply("org.jetbrains.dokka") + val childTask = project.tasks.create("child") val parentTask = project.tasks.create("parent") - val sourceOutputDirectory = childTask.outputDirectory.get() + val sourceOutputDirectory = childTask.outputDirectory.get().asFile sourceOutputDirectory.mkdirs() sourceOutputDirectory.resolve("some.file").writeText("some text") val subFolder = sourceOutputDirectory.resolve("subFolder") subFolder.mkdirs() subFolder.resolve("other.file").writeText("other text") - parentTask.fileLayout.set(DokkaMultiModuleFileLayout { parent, _ -> parent.project.file("target/output") }) + parentTask.fileLayout.set(DokkaMultiModuleFileLayout { parent, _ -> + parent.project.provider { parent.project.layout.projectDirectory.dir("target/output") } + }) parentTask.copyChildOutputDirectory(childTask) /* Assertions */ @@ -99,10 +111,12 @@ class DokkaMultiModuleFileLayoutTest { @Test fun `copyChildOutputDirectory target output directory within itself throws DokkaException`() { val project = ProjectBuilder.builder().build() + project.plugins.apply("org.jetbrains.dokka") + val childTask = project.tasks.create("child") val parentTask = project.tasks.create("parent") parentTask.fileLayout.set(DokkaMultiModuleFileLayout { _, child -> - child.outputDirectory.get().resolve("subfolder") + child.outputDirectory.dir("subfolder") }) assertFailsWith { parentTask.copyChildOutputDirectory(childTask) } } @@ -110,6 +124,8 @@ class DokkaMultiModuleFileLayoutTest { @Test fun `copyChildOutputDirectory NoCopy`() { val project = ProjectBuilder.builder().build() + project.plugins.apply("org.jetbrains.dokka") + val childTask = project.tasks.create("child") val parentTask = project.tasks.create("parent") parentTask.fileLayout.set(NoCopy) diff --git a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/KotlinDslDokkaTaskConfigurationTest.kt b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/KotlinDslDokkaTaskConfigurationTest.kt index 52485cdc..fe6869e9 100644 --- a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/KotlinDslDokkaTaskConfigurationTest.kt +++ b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/KotlinDslDokkaTaskConfigurationTest.kt @@ -2,7 +2,8 @@ package org.jetbrains.dokka.gradle import org.gradle.kotlin.dsl.withType import org.gradle.testfixtures.ProjectBuilder -import org.jetbrains.dokka.DokkaSourceSetID +import org.jetbrains.dokka.gradle.utils.configureEach_ +import org.jetbrains.dokka.gradle.utils.create_ import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension import java.io.File import kotlin.test.Test @@ -13,12 +14,12 @@ class KotlinDslDokkaTaskConfigurationTest { fun `configure dokka task`() { val project = ProjectBuilder.builder().build() project.plugins.apply("org.jetbrains.dokka") - project.tasks.withType().configureEach { + project.tasks.withType().configureEach_ { outputDirectory.set(File("test")) } project.tasks.withType(DokkaTask::class.java).forEach { dokkaTask -> - assertEquals(File("test"), dokkaTask.outputDirectory.get()) + assertEquals(File("test"), dokkaTask.outputDirectory.get().asFile.relativeTo(project.projectDir)) } } @@ -30,7 +31,7 @@ class KotlinDslDokkaTaskConfigurationTest { project.tasks.withType(DokkaTask::class.java).forEach { dokkaTask -> dokkaTask.dokkaSourceSets.run { val commonMain = create("commonMain") - val jvmMain = create("jvmMain") { + val jvmMain = create_("jvmMain") { dependsOn("commonMain") } diff --git a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/tasks/DokkaCollectorTaskTest.kt b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/tasks/DokkaCollectorTaskTest.kt new file mode 100644 index 00000000..3c7b58a0 --- /dev/null +++ b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/tasks/DokkaCollectorTaskTest.kt @@ -0,0 +1,109 @@ +package org.jetbrains.dokka.gradle.tasks + +import org.gradle.kotlin.dsl.create +import org.gradle.kotlin.dsl.withType +import org.gradle.testfixtures.ProjectBuilder +import org.jetbrains.dokka.DokkaConfigurationImpl +import org.jetbrains.dokka.DokkaException +import org.jetbrains.dokka.gradle.AbstractDokkaTask +import org.jetbrains.dokka.gradle.DokkaCollectorTask +import org.jetbrains.dokka.gradle.DokkaTask +import org.jetbrains.dokka.gradle.utils.all_ +import org.jetbrains.dokka.gradle.utils.allprojects_ +import org.jetbrains.dokka.gradle.utils.configureEach_ +import org.jetbrains.dokka.gradle.utils.withDependencies_ +import java.io.File +import kotlin.test.* + +class DokkaCollectorTaskTest { + + @Test + fun buildDokkaConfiguration() { + val rootProject = ProjectBuilder.builder().build() + val childProject = ProjectBuilder.builder().withParent(rootProject).build() + childProject.plugins.apply("org.jetbrains.kotlin.jvm") + + rootProject.allprojects_ { + plugins.apply("org.jetbrains.dokka") + tasks.withType().configureEach_ { + plugins.withDependencies_ { clear() } + } + tasks.withType().configureEach_ { + dokkaSourceSets.configureEach_ { + classpath.setFrom(emptyList()) + } + } + } + + val collectorTasks = rootProject.tasks.withType() + collectorTasks.configureEach_ { + moduleName.set("custom Module Name") + outputDirectory.set(File("customOutputDirectory")) + cacheRoot.set(File("customCacheRoot")) + failOnWarning.set(true) + offlineMode.set(true) + } + + assertTrue(collectorTasks.isNotEmpty(), "Expected at least one collector task") + + collectorTasks.forEach { task -> + val dokkaConfiguration = task.buildDokkaConfiguration() + assertEquals( + DokkaConfigurationImpl( + moduleName = "custom Module Name", + outputDir = rootProject.projectDir.resolve("customOutputDirectory"), + cacheRoot = rootProject.projectDir.resolve("customCacheRoot"), + failOnWarning = true, + offlineMode = true, + sourceSets = task.childDokkaTasks + .map { it.buildDokkaConfiguration() } + .map { it.sourceSets } + .reduce { acc, list -> acc + list }, + pluginsClasspath = task.childDokkaTasks + .map { it.plugins.resolve().toList() } + .reduce { acc, mutableSet -> acc + mutableSet } + ), + dokkaConfiguration, + ) + } + } + + @Test + fun `verify that cacheRoot is optional, and not required to build DokkaConfiguration`() { + val rootProject = ProjectBuilder.builder().build() + val childProject = ProjectBuilder.builder().withParent(rootProject).build() + childProject.plugins.apply("org.jetbrains.kotlin.jvm") + + rootProject.allprojects_ { + plugins.apply("org.jetbrains.dokka") + tasks.withType().configureEach_ { + plugins.withDependencies_ { clear() } + } + tasks.withType().configureEach_ { + dokkaSourceSets.configureEach_ { + classpath.setFrom(emptyList()) + } + } + } + + val collectorTasks = rootProject.tasks.withType() + collectorTasks.configureEach_ { + cacheRoot.set(null as File?) + } + + assertTrue(collectorTasks.isNotEmpty(), "Expected at least one collector task") + + collectorTasks.forEach { task -> + val dokkaConfiguration = task.buildDokkaConfiguration() + assertNull(dokkaConfiguration.cacheRoot, "Expect that cacheRoot is null") + } + } + + @Test + fun `with no child tasks throws DokkaException`() { + val project = ProjectBuilder.builder().build() + val collectorTask = project.tasks.create("collector") + project.configurations.all_ { withDependencies_ { clear() } } + assertFailsWith { collectorTask.generateDocumentation() } + } +} diff --git a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/tasks/DokkaMultiModuleTaskTest.kt b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/tasks/DokkaMultiModuleTaskTest.kt index ecbe3355..6df809ff 100644 --- a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/tasks/DokkaMultiModuleTaskTest.kt +++ b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/tasks/DokkaMultiModuleTaskTest.kt @@ -6,6 +6,10 @@ import org.gradle.kotlin.dsl.create import org.gradle.kotlin.dsl.withType import org.gradle.testfixtures.ProjectBuilder import org.jetbrains.dokka.* +import org.jetbrains.dokka.gradle.utils.allprojects_ +import org.jetbrains.dokka.gradle.utils.configureEach_ +import org.jetbrains.dokka.gradle.utils.create_ +import org.jetbrains.dokka.gradle.utils.withDependencies_ import java.io.File import kotlin.test.* @@ -27,9 +31,11 @@ class DokkaMultiModuleTaskTest { } init { - rootProject.allprojects { - tasks.withType().configureEach { - plugins.withDependencies { clear() } + rootProject.plugins.apply("org.jetbrains.dokka") + childProject.plugins.apply("org.jetbrains.dokka") + rootProject.allprojects_ { + tasks.withType().configureEach_ { + plugins.withDependencies_ { clear() } } } } @@ -56,7 +62,7 @@ class DokkaMultiModuleTaskTest { childDokkaTask.apply { dokkaSourceSets.create("main") dokkaSourceSets.create("test") - dokkaSourceSets.configureEach { + dokkaSourceSets.configureEach_ { includes.from(include1, include2) } } @@ -84,7 +90,7 @@ class DokkaMultiModuleTaskTest { moduleName = "custom Module Name", moduleVersion = "1.5.0", outputDir = multiModuleTask.project.buildDir.resolve("customOutputDirectory"), - cacheRoot = File("customCacheRoot"), + cacheRoot = multiModuleTask.project.projectDir.resolve("customCacheRoot"), pluginsConfiguration = mutableListOf( PluginConfigurationImpl( "pluginA", @@ -101,7 +107,7 @@ class DokkaMultiModuleTaskTest { name = "child", relativePathToOutputDirectory = File("child"), includes = setOf(include1, include2), - sourceOutputDirectory = childDokkaTask.outputDirectory.get() + sourceOutputDirectory = childDokkaTask.outputDirectory.get().asFile ) ) ), @@ -151,7 +157,7 @@ class DokkaMultiModuleTaskTest { fun `multimodule task with no child tasks throws DokkaException`() { val project = ProjectBuilder.builder().build() val multimodule = project.tasks.create("multimodule") - project.configurations.configureEach { withDependencies { clear() } } + project.configurations.configureEach_ { withDependencies_ { clear() } } assertFailsWith { multimodule.generateDocumentation() } } @@ -162,17 +168,17 @@ class DokkaMultiModuleTaskTest { val childDokkaTaskInclude3 = childProject.file("include3") childDokkaTask.apply { - dokkaSourceSets.create("main") { + dokkaSourceSets.create_("main") { includes.from(childDokkaTaskInclude1, childDokkaTaskInclude2) } - dokkaSourceSets.create("main2") { + dokkaSourceSets.create_("main2") { includes.from(childDokkaTaskInclude3) } } val secondChildDokkaTaskInclude = childProject.file("include4") val secondChildDokkaTask = childProject.tasks.create("secondChildDokkaTask") { - dokkaSourceSets.create("main") { + dokkaSourceSets.create_("main") { includes.from(secondChildDokkaTaskInclude) } } @@ -194,7 +200,9 @@ class DokkaMultiModuleTaskTest { @Test fun sourceChildOutputDirectories() { val parent = ProjectBuilder.builder().build() + parent.plugins.apply("org.jetbrains.dokka") val child = ProjectBuilder.builder().withName("child").withParent(parent).build() + child.plugins.apply("org.jetbrains.dokka") val parentTask = parent.tasks.create("parent") val childTask = child.tasks.create("child") @@ -203,7 +211,8 @@ class DokkaMultiModuleTaskTest { childTask.outputDirectory.set(child.file("custom/output")) assertEquals( - listOf(parent.file("child/custom/output")), parentTask.sourceChildOutputDirectories, + listOf(parent.file("child/custom/output")), + parentTask.sourceChildOutputDirectories.files.toList(), "Expected child output directory being present" ) } @@ -219,11 +228,12 @@ class DokkaMultiModuleTaskTest { parentTask.addChildTask(childTask) parentTask.fileLayout.set(DokkaMultiModuleFileLayout { taskParent, taskChild -> - taskParent.project.buildDir.resolve(taskChild.name) + taskParent.project.layout.buildDirectory.dir(taskChild.name) }) assertEquals( - listOf(parent.project.buildDir.resolve("child")), parentTask.targetChildOutputDirectories, + listOf(parent.project.buildDir.resolve("child")), + parentTask.targetChildOutputDirectories.get().map { it.asFile }, "Expected child target output directory being present" ) } diff --git a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/tasks/DokkaTaskTest.kt b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/tasks/DokkaTaskTest.kt index 518a01f1..79b80fa3 100644 --- a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/tasks/DokkaTaskTest.kt +++ b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/tasks/DokkaTaskTest.kt @@ -4,6 +4,9 @@ package org.jetbrains.dokka.gradle import org.gradle.kotlin.dsl.create import org.gradle.testfixtures.ProjectBuilder +import org.jetbrains.dokka.gradle.utils.all_ +import org.jetbrains.dokka.gradle.utils.register_ +import org.jetbrains.dokka.gradle.utils.withDependencies_ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNull @@ -12,12 +15,14 @@ class DokkaTaskTest { @Test fun `no suppressed source sets are present after in built configuration`() { val project = ProjectBuilder.builder().build() + project.plugins.apply("org.jetbrains.dokka") + val task = project.tasks.create("dokkaTask") - project.configurations.all { withDependencies { clear() } } + project.configurations.all_ { withDependencies_ { clear() } } task.dokkaSourceSets.register("main") task.dokkaSourceSets.register("jvm") - task.dokkaSourceSets.register("test") { + task.dokkaSourceSets.register_("test") { suppress.set(true) } @@ -29,10 +34,12 @@ class DokkaTaskTest { } @Test - fun `module version is not present if not specified`(){ + fun `module version is not present if not specified`() { val project = ProjectBuilder.builder().build() + project.plugins.apply("org.jetbrains.dokka") + val task = project.tasks.create("dokkaTask") - project.configurations.all { withDependencies { clear() } } + project.configurations.all_ { withDependencies_ { clear() } } task.dokkaSourceSets.register("main") assertNull(task.buildDokkaConfiguration().moduleVersion) diff --git a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/utils/samWithReceiverWorkarounds.kt b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/utils/samWithReceiverWorkarounds.kt new file mode 100644 index 00000000..4738f7d8 --- /dev/null +++ b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/utils/samWithReceiverWorkarounds.kt @@ -0,0 +1,69 @@ +package org.jetbrains.dokka.gradle.utils + +import org.gradle.api.* +import org.gradle.api.artifacts.Configuration +import org.gradle.api.artifacts.DependencySet +import org.jetbrains.dokka.gradle.GradleDokkaSourceSetBuilder +import org.jetbrains.dokka.gradle.GradleExternalDocumentationLinkBuilder + + +/** + * Workarounds because `SamWithReceiver` not working in test sources + * https://youtrack.jetbrains.com/issue/KTIJ-14684 + * + * The `SamWithReceiver` plugin is automatically applied by the `kotlin-dsl` plugin. + * It converts all [org.gradle.api.Action] so the parameter is the receiver: + * + * ``` + * // with SamWithReceiver ✅ + * tasks.configureEach { + * val task: Task = this + * } + * + * // without SamWithReceiver + * tasks.configureEach { it -> + * val task: Task = it + * } + * ``` + * + * This is nice because it means that the Dokka Gradle Plugin more closely matches `build.gradle.kts` files. + * + * However, [IntelliJ is bugged](https://youtrack.jetbrains.com/issue/KTIJ-14684) and doesn't + * acknowledge that `SamWithReceiver` has been applied in test sources. The code works and compiles, + * but IntelliJ shows red errors. + * + * These functions are workarounds, and should be removed ASAP. + */ +@Suppress("unused") +private object Explain + +fun Project.subprojects_(configure: Project.() -> Unit) = + subprojects(configure) + +@Suppress("SpellCheckingInspection") +fun Project.allprojects_(configure: Project.() -> Unit) = + allprojects(configure) + +fun DomainObjectCollection.configureEach_(configure: T.() -> Unit) = + configureEach(configure) + +fun DomainObjectCollection.all_(configure: T.() -> Unit) = + all(configure) + +fun Configuration.withDependencies_(action: DependencySet.() -> Unit): Configuration = + withDependencies(action) + + +fun NamedDomainObjectContainer.create_(name: String, configure: T.() -> Unit): T = + create(name, configure) + +fun NamedDomainObjectContainer.register_( + name: String, + configure: T.() -> Unit +): NamedDomainObjectProvider = + register(name, configure) + + +fun GradleDokkaSourceSetBuilder.externalDocumentationLink_( + action: GradleExternalDocumentationLinkBuilder.() -> Unit +) = externalDocumentationLink(action) -- cgit