From eae1ce49d18c2978b49166ea502bf2c109a85504 Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Sat, 18 Jul 2020 12:18:59 +0200 Subject: Simplify Dokka Gradle Plugin --- .../dokka/gradle/AbstractDokkaParentTaskTest.kt | 107 ++++++++++++++++++++ .../dokka/gradle/DokkaCollectorTaskTest.kt | 60 +++++++++++ .../dokka/gradle/DokkaConfigurationJsonTest.kt | 51 +--------- .../gradle/DokkaConfigurationSerializableTest.kt | 13 +-- .../dokka/gradle/DokkaMultiModuleTaskTest.kt | 112 +++++++++++++++++++++ .../gradle/KotlinDslDokkaTaskConfigurationTest.kt | 5 +- 6 files changed, 292 insertions(+), 56 deletions(-) create mode 100644 runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/AbstractDokkaParentTaskTest.kt create 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/DokkaMultiModuleTaskTest.kt (limited to 'runners/gradle-plugin/src/test/kotlin/org/jetbrains') 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 new file mode 100644 index 00000000..da0f3f35 --- /dev/null +++ b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/AbstractDokkaParentTaskTest.kt @@ -0,0 +1,107 @@ +package org.jetbrains.dokka.gradle + +import org.gradle.kotlin.dsl.register +import org.gradle.kotlin.dsl.withType +import org.gradle.testfixtures.ProjectBuilder +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class AbstractDokkaParentTaskTest { + + private val rootProject = ProjectBuilder.builder().build() + private val subproject0 = ProjectBuilder.builder().withName("subproject0").withParent(rootProject).build() + private val subproject1 = ProjectBuilder.builder().withName("subproject1").withParent(rootProject).build() + private val subSubproject0a = ProjectBuilder.builder().withName("subSubproject0a").withParent(subproject0).build() + + init { + rootProject.allprojects { project -> project.plugins.apply("org.jetbrains.dokka") } + } + + private val parentTasks = rootProject.tasks.withType().toList() + + @Test + fun `at least one parent task is registered`() { + assertTrue( + parentTasks.isNotEmpty(), + "Expected at least one ${AbstractDokkaParentTask::class.simpleName} task in rootProject" + ) + } + + @Test + fun `configuring subprojects`() { + parentTasks.forEach { task -> + assertEquals( + setOf(":subproject0", ":subproject1", ":subproject0:subSubproject0a"), task.subprojectPaths, + "Expected all sub projects registered by default" + ) + + assertEquals( + listOf(subproject0, subproject1, subSubproject0a), task.subprojects, + "Expected all sub projects registered by default" + ) + + assertEquals(3, task.dokkaTasks.size, "Expected three referenced dokka tasks") + assertTrue(listOf(subproject0, subproject1, subSubproject0a).all { project -> + task.dokkaTasks.any { task -> task in project.tasks } + }, "Expected all sub projects to contribute to referenced dokka tasks") + + task.removeSubproject(subproject0) + assertEquals( + setOf(":subproject1", ":subproject0:subSubproject0a"), task.subprojectPaths, + "Expected subproject0 to be removed (without removing its children)" + ) + + task.addSubproject(subproject0) + assertEquals( + setOf(":subproject1", ":subproject0:subSubproject0a", ":subproject0"), task.subprojectPaths, + "Expected subproject0 being added again" + ) + + task.addSubproject(subproject0) + assertEquals( + setOf(":subproject1", ":subproject0:subSubproject0a", ":subproject0"), task.subprojectPaths, + "Expected adding same project twice to be ignored" + ) + + task.removeAllProjects(subproject0) + assertEquals( + setOf(":subproject1"), task.subprojectPaths, + "Expected subproject0 and subSubproject0a to be removed" + ) + + task.addAllProjects(subproject0) + assertEquals( + setOf(":subproject1", ":subproject0", ":subproject0:subSubproject0a"), task.subprojectPaths, + "Expected subproject0 and subSubproject0a to be added again" + ) + } + } + + @Test + fun `configure dokkaTaskNames`() { + parentTasks.forEach { task -> + assertEquals( + 3, task.dokkaTasks.size, + "Expected 3 tasks referenced by default" + ) + + val customDokkaTaskName = "custom${task.name}" + task.dokkaTaskNames = setOf(customDokkaTaskName) + assertTrue(task.dokkaTasks.isEmpty(), "Expected no $customDokkaTaskName. Found: ${task.dokkaTasks}") + + rootProject.subprojects { subproject -> + subproject.tasks.register(customDokkaTaskName) + } + + assertEquals( + 3, task.dokkaTasks.size, + "Expected three $customDokkaTaskName found" + ) + + task.dokkaTasks.forEach { dokkaTask -> + assertEquals(customDokkaTaskName, dokkaTask.name) + } + } + } +} 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 new file mode 100644 index 00000000..2d8dc21a --- /dev/null +++ b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaCollectorTaskTest.kt @@ -0,0 +1,60 @@ +package org.jetbrains.dokka.gradle + +import org.gradle.api.artifacts.Dependency +import org.gradle.api.artifacts.FileCollectionDependency +import org.gradle.kotlin.dsl.withType +import org.gradle.testfixtures.ProjectBuilder +import org.jetbrains.dokka.DokkaConfigurationImpl +import java.io.File +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +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 { project -> + project.plugins.apply("org.jetbrains.dokka") + project.tasks.withType().configureEach { task -> + task.plugins.withDependencies { dependencies -> dependencies.clear() } + } + } + + val collectorTasks = rootProject.tasks.withType() + collectorTasks.configureEach { task -> + task.outputDirectory = File("customOutputDirectory") + task.cacheRoot = File("customCacheRoot") + task.failOnWarning = true + task.offlineMode = true + } + + assertTrue(collectorTasks.isNotEmpty(), "Expected at least one collector task") + + collectorTasks.forEach { task -> + val dokkaConfiguration = task.buildDokkaConfiguration() + assertEquals( + DokkaConfigurationImpl( + outputDir = File("customOutputDirectory"), + cacheRoot = File("customCacheRoot"), + failOnWarning = true, + offlineMode = true, + sourceSets = task.dokkaTasks + .map { it.buildDokkaConfiguration() } + .map { it.sourceSets } + .reduce { acc, list -> acc + list }, + pluginsClasspath = task.dokkaTasks + .map { it.plugins.resolve() } + .reduce { acc, mutableSet -> acc + mutableSet } + .toList() + ), + dokkaConfiguration + ) + } + + } +} 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 d80f43e4..f6a9c909 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 @@ -3,6 +3,7 @@ package org.jetbrains.dokka.gradle import org.gradle.kotlin.dsl.withType import org.gradle.testfixtures.ProjectBuilder import org.jetbrains.dokka.* +import java.io.File import java.net.URL import kotlin.test.Test import kotlin.test.assertEquals @@ -20,8 +21,8 @@ class DokkaConfigurationJsonTest { dokkaTask.apply { this.failOnWarning = true this.offlineMode = true - this.outputDirectory = "customOutputDir" - this.cacheRoot = "customCacheRoot" + this.outputDirectory = File("customOutputDir") + this.cacheRoot = File("customCacheRoot") this.pluginsConfiguration["0"] = "a" this.pluginsConfiguration["1"] = "b" this.dokkaSourceSets.create("main") { sourceSet -> @@ -33,12 +34,6 @@ class DokkaConfigurationJsonTest { link.packageListUrl = URL("http://some.url") link.url = URL("http://some.other.url") } - sourceSet.collectKotlinTasks = { - println(this@DokkaConfigurationJsonTest) - println("This lambda is capturing the entire test") - emptyList() - } - sourceSet.perPackageOption { packageOption -> packageOption.includeNonPublic = true packageOption.reportUndocumented = true @@ -47,47 +42,11 @@ class DokkaConfigurationJsonTest { } } - val sourceConfiguration = dokkaTask.getConfigurationOrThrow() + val sourceConfiguration = dokkaTask.buildDokkaConfiguration() val configurationJson = sourceConfiguration.toJsonString() val parsedConfiguration = DokkaConfigurationImpl(configurationJson) - assertEquals( - DokkaConfigurationImpl( - failOnWarning = sourceConfiguration.failOnWarning, - offlineMode = sourceConfiguration.offlineMode, - outputDir = sourceConfiguration.outputDir, - cacheRoot = sourceConfiguration.cacheRoot, - pluginsClasspath = emptyList(), - pluginsConfiguration = sourceConfiguration.pluginsConfiguration.toMap(), - sourceSets = listOf( - DokkaSourceSetImpl( - moduleDisplayName = sourceConfiguration.sourceSets.single().moduleDisplayName, - displayName = sourceConfiguration.sourceSets.single().displayName, - reportUndocumented = sourceConfiguration.sourceSets.single().reportUndocumented, - externalDocumentationLinks = sourceConfiguration.sourceSets.single().externalDocumentationLinks - .map { link -> - ExternalDocumentationLinkImpl( - url = link.url, - packageListUrl = link.packageListUrl - ) - }, - perPackageOptions = sourceConfiguration.sourceSets.single().perPackageOptions.map { option -> - PackageOptionsImpl( - prefix = option.prefix, - includeNonPublic = option.includeNonPublic, - reportUndocumented = option.reportUndocumented, - skipDeprecated = option.skipDeprecated, - suppress = option.suppress - ) - }, - sourceSetID = sourceConfiguration.sourceSets.single().sourceSetID, - sourceRoots = sourceConfiguration.sourceSets.single().sourceRoots.map { sourceRoot -> - SourceRootImpl(sourceRoot.path) - } - ) - ) - ), parsedConfiguration - ) + assertEquals(sourceConfiguration, parsedConfiguration) println(parsedConfiguration) } } 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 70ecfcf0..e4fd0676 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 @@ -6,6 +6,7 @@ import org.jetbrains.dokka.DokkaConfiguration import org.jetbrains.dokka.toJsonString import org.junit.Rule import org.junit.rules.TemporaryFolder +import java.io.File import java.io.ObjectInputStream import java.io.ObjectOutputStream import java.net.URL @@ -28,8 +29,8 @@ class DokkaConfigurationSerializableTest { dokkaTask.apply { this.failOnWarning = true this.offlineMode = true - this.outputDirectory = "customOutputDir" - this.cacheRoot = "customCacheRoot" + this.outputDirectory = File("customOutputDir") + this.cacheRoot = File("customCacheRoot") this.pluginsConfiguration["0"] = "a" this.pluginsConfiguration["1"] = "b" this.dokkaSourceSets.create("main") { sourceSet -> @@ -41,9 +42,6 @@ class DokkaConfigurationSerializableTest { link.packageListUrl = URL("http://some.url") link.url = URL("http://some.other.url") } - sourceSet.collectKotlinTasks = { - emptyList() - } sourceSet.perPackageOption { packageOption -> packageOption.includeNonPublic = true @@ -53,7 +51,7 @@ class DokkaConfigurationSerializableTest { } } - val sourceConfiguration = dokkaTask.getConfigurationOrThrow() + val sourceConfiguration = dokkaTask.buildDokkaConfiguration() val configurationFile = temporaryFolder.root.resolve("config.bin") ObjectOutputStream(configurationFile.outputStream()).use { stream -> stream.writeObject(sourceConfiguration) @@ -62,7 +60,6 @@ class DokkaConfigurationSerializableTest { stream.readObject() as DokkaConfiguration } - /* Abusing toJsonString, since there is no proper .equals on the gradle implementations yet */ - assertEquals(sourceConfiguration.toJsonString(), parsedConfiguration.toJsonString()) + assertEquals(sourceConfiguration, parsedConfiguration) } } diff --git a/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaMultiModuleTaskTest.kt b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaMultiModuleTaskTest.kt new file mode 100644 index 00000000..ff318bdf --- /dev/null +++ b/runners/gradle-plugin/src/test/kotlin/org/jetbrains/dokka/gradle/DokkaMultiModuleTaskTest.kt @@ -0,0 +1,112 @@ +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.DokkaModuleDescriptionImpl +import java.io.File +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class DokkaMultiModuleTaskTest { + + private val rootProject = ProjectBuilder.builder() + .withName("root") + .build() + + private val childProject = ProjectBuilder.builder() + .withName("child") + .withProjectDir(rootProject.projectDir.resolve("child")) + .withParent(rootProject).build() + + init { + rootProject.allprojects { project -> + project.plugins.apply("org.jetbrains.kotlin.jvm") + project.plugins.apply("org.jetbrains.dokka") + project.tasks.withType().configureEach { task -> + task.plugins.withDependencies { dependencies -> dependencies.clear() } + } + } + } + + @Test + fun `child project is withing root project`() { + assertEquals( + rootProject.projectDir, childProject.projectDir.parentFile, + "Expected child project being inside the root project" + ) + + assertEquals( + childProject.projectDir.name, "child", + "Expected folder of child project to be called 'child'" + ) + } + + @Test + fun buildDokkaConfiguration() { + childProject.tasks.withType().configureEach { task -> + task.outputDirectory = task.project.buildDir.resolve("output") + } + + val multimoduleTasks = rootProject.tasks.withType() + assertTrue(multimoduleTasks.isNotEmpty(), "Expected at least one multimodule task") + + multimoduleTasks.configureEach { task -> + task.documentationFileName = "customDocumentationFileName.md" + task.outputDirectory = task.project.buildDir.resolve("customOutputDirectory") + task.cacheRoot = File("customCacheRoot") + task.pluginsConfiguration["pluginA"] = "configA" + task.failOnWarning = true + task.offlineMode = true + } + + multimoduleTasks.forEach { task -> + val dokkaConfiguration = task.buildDokkaConfiguration() + assertEquals( + DokkaConfigurationImpl( + outputDir = task.project.buildDir.resolve("customOutputDirectory"), + cacheRoot = File("customCacheRoot"), + pluginsConfiguration = mapOf("pluginA" to "configA"), + pluginsClasspath = emptyList(), + failOnWarning = true, + offlineMode = true, + modules = listOf( + DokkaModuleDescriptionImpl( + name = "child", + path = File("../../child/build/output"), + docFile = childProject.projectDir.resolve("customDocumentationFileName.md") + ) + ) + ), + dokkaConfiguration + ) + } + } + + @Test + fun `setting dokkaTaskNames declares proper task dependencies`() { + val multimoduleTasks = rootProject.tasks.withType() + assertTrue(multimoduleTasks.isNotEmpty(), "Expected at least one multimodule task") + + multimoduleTasks.toList().forEach { task -> + val dependencies = task.taskDependencies.getDependencies(task).toSet() + assertEquals(1, dependencies.size, "Expected one dependency") + val dependency = dependencies.single() + + assertTrue(dependency is DokkaTask, "Expected dependency to be of Type ${DokkaTask::class.simpleName}") + assertEquals(childProject, dependency.project, "Expected dependency from child project") + } + + val customDokkaTask = childProject.tasks.create("customDokkaTask") + + multimoduleTasks.toList().forEach { task -> + task.dokkaTaskNames += "customDokkaTask" + val dependencies = task.taskDependencies.getDependencies(task).toSet() + + assertEquals(2, dependencies.size, "Expected two dependencies") + assertTrue(customDokkaTask in dependencies, "Expected 'customDokkaTask' in dependencies") + } + } +} 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 7b78fb55..f4029bd5 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 @@ -3,6 +3,7 @@ package org.jetbrains.dokka.gradle import org.gradle.testfixtures.ProjectBuilder import org.jetbrains.dokka.DokkaSourceSetID import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension +import java.io.File import kotlin.test.Test import kotlin.test.assertEquals @@ -13,11 +14,11 @@ class KotlinDslDokkaTaskConfigurationTest { val project = ProjectBuilder.builder().build() project.plugins.apply("org.jetbrains.dokka") project.tasks.withType(DokkaTask::class.java).forEach { dokkaTask -> - dokkaTask.outputDirectory = "test" + dokkaTask.outputDirectory = File("test") } project.tasks.withType(DokkaTask::class.java).forEach { dokkaTask -> - assertEquals("test", dokkaTask.outputDirectory) + assertEquals(File("test"), dokkaTask.outputDirectory) } } -- cgit