From 87b6f4898d14220cb2f3a4f28735a3c4976cb8b1 Mon Sep 17 00:00:00 2001 From: Matthias Geisler Date: Mon, 21 Mar 2022 09:36:08 +0100 Subject: [PATCH] Resolve hick-up when repetitive executed --- .../gradle/kmock/KMockPluginContract.kt | 7 ++ .../gradle/kmock/SharedSourceCopist.kt | 5 +- .../kmock/source/KmpSetupConfigurator.kt | 3 + .../kmock/source/KspSharedSourceCleaner.kt | 19 +++++ .../kmock/source/KmpSetupConfiguratorSpec.kt | 62 +++++++++++++++ .../source/KspSharedSourceCleanerSpec.kt | 75 +++++++++++++++++++ 6 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/source/KspSharedSourceCleaner.kt create mode 100644 kmock-gradle/src/test/kotlin/tech/antibytes/gradle/kmock/source/KspSharedSourceCleanerSpec.kt diff --git a/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/KMockPluginContract.kt b/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/KMockPluginContract.kt index c88bbbdc..bd92bf43 100644 --- a/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/KMockPluginContract.kt +++ b/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/KMockPluginContract.kt @@ -79,6 +79,13 @@ internal interface KMockPluginContract { ) } + interface KspSharedSourceCleaner { + fun cleanKspSources( + project: Project, + sourceSets: Set + ) + } + interface SourceSetConfigurator { fun configure(project: Project) } diff --git a/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/SharedSourceCopist.kt b/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/SharedSourceCopist.kt index b8d622fb..45d492fd 100644 --- a/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/SharedSourceCopist.kt +++ b/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/SharedSourceCopist.kt @@ -98,13 +98,12 @@ internal object SharedSourceCopist : KMockPluginContract.SharedSourceCopist { "moveTo${target.capitalize(Locale.ROOT)}For${platform.capitalize(Locale.ROOT)}", Copy::class.java ) - setUpTask( task = task, platform = platform, buildDir = buildDir, - source = source.substringBeforeLast("Test"), - target = target.substringBeforeLast("Test"), + source = source.substring(0, source.length - 4), + target = target.substring(0, target.length - 4), indicator = indicator, ) diff --git a/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/source/KmpSetupConfigurator.kt b/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/source/KmpSetupConfigurator.kt index fc71b660..d69584f9 100644 --- a/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/source/KmpSetupConfigurator.kt +++ b/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/source/KmpSetupConfigurator.kt @@ -320,6 +320,9 @@ internal object KmpSetupConfigurator : KMockPluginContract.KmpSetupConfigurator indicators[common] = common.toUpperCase(Locale.ROOT) + KspSharedSourceCleaner.cleanKspSources(project, indicators.keys) + KspSharedSourceCleaner.cleanKspSources(project, kspMapping.keys.map { source -> "${source}Test" }.toSet()) + wireSharedSourceTasks( project, indicators, diff --git a/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/source/KspSharedSourceCleaner.kt b/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/source/KspSharedSourceCleaner.kt new file mode 100644 index 00000000..c1cef702 --- /dev/null +++ b/kmock-gradle/src/main/kotlin/tech/antibytes/gradle/kmock/source/KspSharedSourceCleaner.kt @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2022 Matthias Geisler (bitPogo) / All rights reserved. + * + * Use of this source code is governed by Apache v2.0 + */ + +package tech.antibytes.gradle.kmock.source + +import org.gradle.api.Project +import tech.antibytes.gradle.kmock.KMockPluginContract + +internal object KspSharedSourceCleaner : KMockPluginContract.KspSharedSourceCleaner { + override fun cleanKspSources(project: Project, sourceSets: Set) { + sourceSets.forEach { sourceSet -> + val platform = sourceSet.substring(0, sourceSet.length - 4) + project.file("${project.buildDir.absolutePath}/generated/ksp/$platform").deleteRecursively() + } + } +} diff --git a/kmock-gradle/src/test/kotlin/tech/antibytes/gradle/kmock/source/KmpSetupConfiguratorSpec.kt b/kmock-gradle/src/test/kotlin/tech/antibytes/gradle/kmock/source/KmpSetupConfiguratorSpec.kt index 447145ba..db984e5c 100644 --- a/kmock-gradle/src/test/kotlin/tech/antibytes/gradle/kmock/source/KmpSetupConfiguratorSpec.kt +++ b/kmock-gradle/src/test/kotlin/tech/antibytes/gradle/kmock/source/KmpSetupConfiguratorSpec.kt @@ -33,11 +33,13 @@ class KmpSetupConfiguratorSpec { @BeforeEach fun setUp() { mockkObject(SharedSourceCopist) + mockkObject(KspSharedSourceCleaner) } @AfterEach fun tearDown() { unmockkObject(SharedSourceCopist) + unmockkObject(KspSharedSourceCleaner) } @Test @@ -69,6 +71,8 @@ class KmpSetupConfiguratorSpec { mockk() ) + every { KspSharedSourceCleaner.cleanKspSources(any(), any()) } just Runs + every { copyTasks[0].dependsOn(any()) } returns copyTasks[0] every { copyTasks[0].mustRunAfter(*anyVararg()) } returns copyTasks[0] @@ -126,6 +130,19 @@ class KmpSetupConfiguratorSpec { ) // Then + verify(exactly = 1) { + KspSharedSourceCleaner.cleanKspSources( + project, + setOf("commonTest") + ) + } + verify(exactly = 1) { + KspSharedSourceCleaner.cleanKspSources( + project, + setOf("jvmTest", "jsTest") + ) + } + verify(exactly = 1) { SharedSourceCopist.copySharedSource( project, @@ -277,6 +294,8 @@ class KmpSetupConfiguratorSpec { mockk() ) + every { KspSharedSourceCleaner.cleanKspSources(any(), any()) } just Runs + every { copyTasks[0].dependsOn(any()) } returns copyTasks[0] every { copyTasks[0].mustRunAfter(*anyVararg()) } returns copyTasks[0] every { copyTasks[1].dependsOn(any()) } returns copyTasks[1] @@ -348,8 +367,21 @@ class KmpSetupConfiguratorSpec { "commonTest" to setOf("jvm", "android"), ) ) + verify(exactly = 1) { + KspSharedSourceCleaner.cleanKspSources( + project, + setOf("jvmTest", "androidTest") + ) + } // Then + verify(exactly = 1) { + KspSharedSourceCleaner.cleanKspSources( + project, + setOf("commonTest") + ) + } + verify(exactly = 1) { SharedSourceCopist.copySharedSource( project, @@ -563,6 +595,8 @@ class KmpSetupConfiguratorSpec { mockk() ) + every { KspSharedSourceCleaner.cleanKspSources(any(), any()) } just Runs + every { copyTasks[0].dependsOn(any()) } returns copyTasks[0] every { copyTasks[0].mustRunAfter(*anyVararg()) } returns copyTasks[0] every { copyTasks[1].dependsOn(any()) } returns copyTasks[1] @@ -625,6 +659,19 @@ class KmpSetupConfiguratorSpec { ) // Then + verify(exactly = 1) { + KspSharedSourceCleaner.cleanKspSources( + project, + setOf("commonTest", "otherTest") + ) + } + verify(exactly = 1) { + KspSharedSourceCleaner.cleanKspSources( + project, + setOf("jvmTest", "jsTest") + ) + } + verify(exactly = 1) { SharedSourceCopist.copySharedSource( project, @@ -806,6 +853,8 @@ class KmpSetupConfiguratorSpec { mockk() ) + every { KspSharedSourceCleaner.cleanKspSources(any(), any()) } just Runs + every { copyTasks[0].dependsOn(any()) } returns copyTasks[0] every { copyTasks[0].mustRunAfter(*anyVararg()) } returns copyTasks[0] every { copyTasks[1].dependsOn(any()) } returns copyTasks[1] @@ -868,6 +917,19 @@ class KmpSetupConfiguratorSpec { ) // Then + verify(exactly = 1) { + KspSharedSourceCleaner.cleanKspSources( + project, + setOf("commonTest", "otherTest") + ) + } + verify(exactly = 1) { + KspSharedSourceCleaner.cleanKspSources( + project, + setOf("androidTest") + ) + } + verify(exactly = 1) { SharedSourceCopist.copySharedSource( project, diff --git a/kmock-gradle/src/test/kotlin/tech/antibytes/gradle/kmock/source/KspSharedSourceCleanerSpec.kt b/kmock-gradle/src/test/kotlin/tech/antibytes/gradle/kmock/source/KspSharedSourceCleanerSpec.kt new file mode 100644 index 00000000..5f8c4c96 --- /dev/null +++ b/kmock-gradle/src/test/kotlin/tech/antibytes/gradle/kmock/source/KspSharedSourceCleanerSpec.kt @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2022 Matthias Geisler (bitPogo) / All rights reserved. + * + * Use of this source code is governed by Apache v2.0 + */ + +package tech.antibytes.gradle.kmock.source + +import org.gradle.api.Project +import org.gradle.testfixtures.ProjectBuilder +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import tech.antibytes.gradle.kmock.KMockPluginContract +import tech.antibytes.util.test.fulfils +import tech.antibytes.util.test.mustBe +import java.io.File + +class KspSharedSourceCleanerSpec { + @TempDir + private lateinit var buildDir: File + private lateinit var file: File + private lateinit var project: Project + + @BeforeEach + fun setup() { + project = ProjectBuilder.builder().build() + project.buildDir = buildDir + } + + private fun createStubs(): File { + val generated = File(buildDir, "generated") + generated.mkdir() + + val ksp = File(generated, "ksp") + ksp.mkdir() + + val targetPlatform = File(ksp, "platform") + targetPlatform.mkdir() + + val target = File(targetPlatform, "target") + target.mkdir() + + val sub = File(target, "sub") + sub.mkdir() + + val file1 = File(target, "Something.kt") + file1.createNewFile() + + val file2 = File(sub, "SomethingElse.kt") + file2.createNewFile() + + file1.writeText("content") + file2.writeText("content") + + return targetPlatform + } + + @Test + fun `It fulfils KmpSharedSourceCleaner`() { + KspSharedSourceCleaner fulfils KMockPluginContract.KspSharedSourceCleaner::class + } + + @Test + fun `Given cleanMetaSources is called, it removes the given SharedSources from the generated KSP output`() { + // Given + val platform = createStubs() + + // When + KspSharedSourceCleaner.cleanKspSources(project, setOf("${platform.name}Test")) + + // Then + platform.exists() mustBe false + } +}