44package org.jetbrains.dokka.gradle.engine.parameters
55
66import io.kotest.core.spec.style.FunSpec
7+ import io.kotest.core.test.TestScope
8+ import io.kotest.inspectors.shouldForAll
79import io.kotest.matchers.collections.shouldBeEmpty
10+ import io.kotest.matchers.collections.shouldContainExactly
811import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
12+ import io.kotest.matchers.file.shouldBeAFile
913import io.kotest.matchers.shouldBe
1014import org.gradle.api.Project
1115import org.gradle.kotlin.dsl.apply
@@ -16,6 +20,11 @@ import org.jetbrains.dokka.gradle.DokkaPlugin
1620import org.jetbrains.dokka.gradle.engine.parameters.VisibilityModifier.Public
1721import org.jetbrains.dokka.gradle.utils.create_
1822import org.jetbrains.dokka.gradle.utils.enableV2Plugin
23+ import java.nio.file.Path
24+ import kotlin.io.path.Path
25+ import kotlin.io.path.createDirectories
26+ import kotlin.io.path.relativeTo
27+ import kotlin.io.path.writeText
1928
2029class DokkaSourceSetSpecTest : FunSpec ({
2130
@@ -39,7 +48,7 @@ class DokkaSourceSetSpecTest : FunSpec({
3948 dss.displayName.orNull shouldBe " foo"
4049 }
4150 test("documentedVisibilities") {
42- dss.documentedVisibilities.orNull.shouldContainExactlyInAnyOrder (Public )
51+ dss.documentedVisibilities.orNull.shouldContainExactly (Public )
4352 }
4453 test("enableAndroidDocumentationLink") {
4554 dss.enableAndroidDocumentationLink.orNull shouldBe false
@@ -53,7 +62,7 @@ class DokkaSourceSetSpecTest : FunSpec({
5362 test("externalDocumentationLinks") {
5463 dss.externalDocumentationLinks
5564 .map { it.run { " $name enabled:${enabled.orNull} url:${url.orNull} packageList:${packageListUrl.orNull} " } }
56- .shouldContainExactlyInAnyOrder (
65+ .shouldContainExactly (
5766 "androidSdk enabled:false url:https://developer.android.com/reference/kotlin/ packageList:https://developer.android.com/reference/kotlin/package-list",
5867 "androidX enabled:false url:https://developer.android.com/reference/kotlin/ packageList:https://developer.android.com/reference/kotlin/androidx/package-list",
5968 "jdk enabled:true url:https://docs.oracle.com/en/java/javase/11/docs/api/ packageList:https://docs.oracle.com/en/java/javase/11/docs/api/element-list",
@@ -107,7 +116,10 @@ class DokkaSourceSetSpecTest : FunSpec({
107116 dss.suppressGeneratedFiles.orNull shouldBe true
108117 }
109118 test("suppressedFiles") {
110- dss.suppressedFiles.shouldBeEmpty()
119+ dss.suppressedFiles.toList() shouldBe listOf(project.file("build/generated"))
120+ }
121+ test("inputSourceFiles") {
122+ dss.inputSourceFiles.shouldBeEmpty() // because `sourceRoots` is empty
111123 }
112124 }
113125
@@ -141,6 +153,165 @@ class DokkaSourceSetSpecTest : FunSpec({
141153 }
142154 }
143155 }
156+
157+ context("DokkaSourceSetSpec inputSourceFiles -> ") {
158+
159+ val project = createProject()
160+
161+ val projectDir = project.projectDir.toPath()
162+ fun Path .createClass(name: String ) {
163+ createDirectories()
164+ resolve("$name.kt").writeText("class $name")
165+ }
166+
167+ val mainSourceDir =
168+ projectDir.resolve("src/main/kotlin").apply {
169+ resolve("com/example").apply {
170+ createClass("MainCls ")
171+ resolve("sub").apply {
172+ createClass("Sub1MainCls ")
173+ createClass("Sub2MainCls ")
174+ }
175+ }
176+ }
177+ val generatedSrcDir =
178+ projectDir.resolve("build/generated/kotlin").apply {
179+ createClass("BuildGenCls ")
180+ resolve("sub").apply {
181+ createClass("Sub1BuildGenCls ")
182+ createClass("Sub2BuildGenCls ")
183+ }
184+ }
185+ val customGeneratedSrcDir =
186+ projectDir.resolve("src/customGenerated/kotlin").apply {
187+ createClass("CustomGenCls ")
188+ resolve("sub").apply {
189+ createClass("Sub1CustomGenCls ")
190+ createClass("Sub2CustomGenCls ")
191+ }
192+ }
193+
194+ fun TestScope .createDss(): DokkaSourceSetSpec {
195+ val dss = project.createDokkaSourceSetSpec(testCase.name.testName)
196+ dss.sourceRoots.from(mainSourceDir, generatedSrcDir, customGeneratedSrcDir)
197+ return dss
198+ }
199+
200+ fun DokkaSourceSetSpec .inputSourceFilesToRelativePaths(): List <Path > =
201+ inputSourceFiles.map { it.toPath().relativeTo(projectDir) }
202+
203+ test("build/generated should be excluded by default") {
204+ val dss = createDss()
205+ dss.inputSourceFilesToRelativePaths() shouldContainExactlyInAnyOrder listOf(
206+ Path ("src/customGenerated/kotlin/CustomGenCls .kt"),
207+ Path ("src/customGenerated/kotlin/sub/Sub1CustomGenCls .kt"),
208+ Path ("src/customGenerated/kotlin/sub/Sub2CustomGenCls .kt"),
209+ Path ("src/main/kotlin/com/example/MainCls .kt"),
210+ Path ("src/main/kotlin/com/example/sub/Sub1MainCls .kt"),
211+ Path ("src/main/kotlin/com/example/sub/Sub2MainCls .kt"),
212+ )
213+ }
214+ test("when suppressGeneratedFiles is set to false, expect all source files are included") {
215+ val dss = createDss()
216+ dss.suppressGeneratedFiles.set(false)
217+ dss.inputSourceFilesToRelativePaths() shouldContainExactlyInAnyOrder listOf(
218+ Path ("build/generated/kotlin/BuildGenCls .kt"),
219+ Path ("build/generated/kotlin/sub/Sub1BuildGenCls .kt"),
220+ Path ("build/generated/kotlin/sub/Sub2BuildGenCls .kt"),
221+ Path ("src/customGenerated/kotlin/CustomGenCls .kt"),
222+ Path ("src/customGenerated/kotlin/sub/Sub1CustomGenCls .kt"),
223+ Path ("src/customGenerated/kotlin/sub/Sub2CustomGenCls .kt"),
224+ Path ("src/main/kotlin/com/example/MainCls .kt"),
225+ Path ("src/main/kotlin/com/example/sub/Sub1MainCls .kt"),
226+ Path ("src/main/kotlin/com/example/sub/Sub2MainCls .kt"),
227+ )
228+ }
229+ test("input source files should only contain files, not directories") {
230+ val dss = createDss()
231+ dss.suppressGeneratedFiles.set(false)
232+ dss.inputSourceFiles.files.shouldForAll { it.shouldBeAFile() }
233+ }
234+ test("expect files can be excluded by exact path") {
235+ val dss = createDss()
236+ dss.suppressGeneratedFiles.set(false)
237+ dss.suppressedFiles.from(
238+ "build/generated/kotlin/sub/Sub1BuildGenCls .kt",
239+ "src/customGenerated/kotlin/sub/Sub1CustomGenCls .kt",
240+ "src/main/kotlin/com/example/sub/Sub1MainCls .kt",
241+ )
242+ dss.inputSourceFilesToRelativePaths() shouldContainExactlyInAnyOrder listOf(
243+ Path ("build/generated/kotlin/BuildGenCls .kt"),
244+ Path ("build/generated/kotlin/sub/Sub2BuildGenCls .kt"),
245+ Path ("src/customGenerated/kotlin/CustomGenCls .kt"),
246+ Path ("src/customGenerated/kotlin/sub/Sub2CustomGenCls .kt"),
247+ Path ("src/main/kotlin/com/example/MainCls .kt"),
248+ Path ("src/main/kotlin/com/example/sub/Sub2MainCls .kt"),
249+ )
250+ }
251+ test("expect files can be excluded by base directories") {
252+ val dss = createDss()
253+ dss.suppressGeneratedFiles.set(false)
254+ dss.suppressedFiles.from(
255+ "build/",
256+ "src/",
257+ )
258+ dss.inputSourceFilesToRelativePaths().shouldBeEmpty()
259+ }
260+ test("expect files can be excluded by sub directories") {
261+ val dss = createDss()
262+ dss.suppressGeneratedFiles.set(false)
263+ dss.suppressedFiles.from(
264+ "build/generated/kotlin/sub/",
265+ "src/customGenerated/kotlin/sub/",
266+ "src/main/kotlin/com/example/sub/",
267+ )
268+ dss.inputSourceFilesToRelativePaths() shouldContainExactlyInAnyOrder listOf(
269+ Path ("build/generated/kotlin/BuildGenCls .kt"),
270+ Path ("src/customGenerated/kotlin/CustomGenCls .kt"),
271+ Path ("src/main/kotlin/com/example/MainCls .kt"),
272+ )
273+ }
274+ test("expect nested files can be excluded") {
275+ val dss = createDss()
276+ dss.suppressGeneratedFiles.set(false)
277+ dss.suppressedFiles.from(
278+ "build/generated/kotlin/sub/",
279+ "src/customGenerated/kotlin/sub/",
280+ "src/main/kotlin/com/example/sub/",
281+ )
282+ dss.inputSourceFilesToRelativePaths() shouldContainExactlyInAnyOrder listOf(
283+ Path ("build/generated/kotlin/BuildGenCls .kt"),
284+ Path ("src/customGenerated/kotlin/CustomGenCls .kt"),
285+ Path ("src/main/kotlin/com/example/MainCls .kt"),
286+ )
287+ }
288+ test("expect suppressGeneratedFiles and suppressedFiles (by specific files) exclude all generated and specified files") {
289+ val dss = createDss()
290+ dss.suppressGeneratedFiles.set(true)
291+ dss.suppressedFiles.from(
292+ "src/customGenerated/kotlin/sub/Sub1CustomGenCls .kt",
293+ "src/customGenerated/kotlin/sub/Sub2CustomGenCls .kt",
294+ "src/main/kotlin/com/example/sub/Sub1MainCls .kt",
295+ "src/main/kotlin/com/example/sub/Sub2MainCls .kt",
296+ )
297+ dss.inputSourceFilesToRelativePaths() shouldContainExactlyInAnyOrder listOf(
298+ Path ("src/customGenerated/kotlin/CustomGenCls .kt"),
299+ Path ("src/main/kotlin/com/example/MainCls .kt"),
300+ )
301+ }
302+ test("expect suppressGeneratedFiles and suppressedFiles (by directories) exclude all generated and specified files") {
303+ val dss = createDss()
304+ dss.suppressGeneratedFiles.set(true)
305+ dss.suppressedFiles.from(
306+ "src/customGenerated/kotlin/",
307+ )
308+ dss.inputSourceFilesToRelativePaths() shouldContainExactlyInAnyOrder listOf(
309+ Path ("src/main/kotlin/com/example/MainCls .kt"),
310+ Path ("src/main/kotlin/com/example/sub/Sub1MainCls .kt"),
311+ Path ("src/main/kotlin/com/example/sub/Sub2MainCls .kt"),
312+ )
313+ }
314+ }
144315}) {
145316 companion object {
146317 private fun createProject (): Project {
0 commit comments