Skip to content

Commit

Permalink
Resolve hick-up when repetitive executed
Browse files Browse the repository at this point in the history
  • Loading branch information
bitPogo committed Mar 22, 2022
1 parent f65b91f commit 5ae7db8
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ internal interface KMockPluginContract {
)
}

interface KmpSharedSourceCleaner {
fun cleanMetaSources(
project: Project,
sourceSets: Set<String>
)
}

interface SourceSetConfigurator {
fun configure(project: Project)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ internal object KmpSetupConfigurator : KMockPluginContract.KmpSetupConfigurator

indicators[common] = common.toUpperCase(Locale.ROOT)

KmpSharedSourceCleaner.cleanMetaSources(project, indicators.keys)

wireSharedSourceTasks(
project,
indicators,
Expand Down
Original file line number Diff line number Diff line change
@@ -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 KmpSharedSourceCleaner : KMockPluginContract.KmpSharedSourceCleaner {
override fun cleanMetaSources(project: Project, sourceSets: Set<String>) {
sourceSets.forEach { sourceSet ->
val platform = sourceSet.substring(0, sourceSet.length - 4)
project.file("${project.buildDir.absolutePath}/generated/ksp/$platform").deleteRecursively()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,11 @@ internal object KmpSourceSetsConfigurator : SourceSetConfigurator {
if (kspCollector.isNotEmpty()) {
propagatePrecedences(project, precedences)

project.afterEvaluate {
KmpSetupConfigurator.wireSharedSourceTasks(
project,
kspCollector,
allDependencies
)
}
KmpSetupConfigurator.wireSharedSourceTasks(
project,
kspCollector,
allDependencies
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ class KmpSetupConfiguratorSpec {
@BeforeEach
fun setUp() {
mockkObject(SharedSourceCopist)
mockkObject(KmpSharedSourceCleaner)
}

@AfterEach
fun tearDown() {
unmockkObject(SharedSourceCopist)
unmockkObject(KmpSharedSourceCleaner)
}

@Test
Expand Down Expand Up @@ -69,6 +71,8 @@ class KmpSetupConfiguratorSpec {
mockk()
)

every { KmpSharedSourceCleaner.cleanMetaSources(any(), any()) } just Runs

every { copyTasks[0].dependsOn(any()) } returns copyTasks[0]
every { copyTasks[0].mustRunAfter(*anyVararg()) } returns copyTasks[0]

Expand Down Expand Up @@ -126,6 +130,13 @@ class KmpSetupConfiguratorSpec {
)

// Then
verify(exactly = 1) {
KmpSharedSourceCleaner.cleanMetaSources(
project,
setOf("commonTest")
)
}

verify(exactly = 1) {
SharedSourceCopist.copySharedSource(
project,
Expand Down Expand Up @@ -277,6 +288,8 @@ class KmpSetupConfiguratorSpec {
mockk()
)

every { KmpSharedSourceCleaner.cleanMetaSources(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]
Expand Down Expand Up @@ -350,6 +363,13 @@ class KmpSetupConfiguratorSpec {
)

// Then
verify(exactly = 1) {
KmpSharedSourceCleaner.cleanMetaSources(
project,
setOf("commonTest")
)
}

verify(exactly = 1) {
SharedSourceCopist.copySharedSource(
project,
Expand Down Expand Up @@ -563,6 +583,8 @@ class KmpSetupConfiguratorSpec {
mockk()
)

every { KmpSharedSourceCleaner.cleanMetaSources(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]
Expand Down Expand Up @@ -625,6 +647,13 @@ class KmpSetupConfiguratorSpec {
)

// Then
verify(exactly = 1) {
KmpSharedSourceCleaner.cleanMetaSources(
project,
setOf("commonTest", "otherTest")
)
}

verify(exactly = 1) {
SharedSourceCopist.copySharedSource(
project,
Expand Down Expand Up @@ -806,6 +835,8 @@ class KmpSetupConfiguratorSpec {
mockk()
)

every { KmpSharedSourceCleaner.cleanMetaSources(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]
Expand Down Expand Up @@ -868,6 +899,13 @@ class KmpSetupConfiguratorSpec {
)

// Then
verify(exactly = 1) {
KmpSharedSourceCleaner.cleanMetaSources(
project,
setOf("commonTest", "otherTest")
)
}

verify(exactly = 1) {
SharedSourceCopist.copySharedSource(
project,
Expand Down
Original file line number Diff line number Diff line change
@@ -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 KmpSharedSourceCleanerSpec {
@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`() {
KmpSharedSourceCleaner fulfils KMockPluginContract.KmpSharedSourceCleaner::class
}

@Test
fun `Given cleanMetaSources is called, it removes the given SharedSources from the generated KSP output`() {
// Given
val platform = createStubs()

// When
KmpSharedSourceCleaner.cleanMetaSources(project, setOf("${platform.name}Test"))

// Then
platform.exists() mustBe false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,6 @@ class KmpSourceSetsConfiguratorSpec {
kotlin
)

invokeGradleAction(
{ probe -> project.afterEvaluate(probe) },
project
)

every { kotlin.sourceSets } returns sources
every { sources.iterator() } returns sourceSets.listIterator()
every { MainConfig.version } returns version
Expand Down Expand Up @@ -230,11 +225,6 @@ class KmpSourceSetsConfiguratorSpec {
kotlin
)

invokeGradleAction(
{ probe -> project.afterEvaluate(probe) },
project
)

every { kotlin.sourceSets } returns sources
every { sources.iterator() } returns sourceSets.listIterator()
every { MainConfig.version } returns version
Expand Down Expand Up @@ -330,11 +320,6 @@ class KmpSourceSetsConfiguratorSpec {
kotlin
)

invokeGradleAction(
{ probe -> project.afterEvaluate(probe) },
project
)

every { kotlin.sourceSets } returns sources
every { sources.iterator() } returns sourceSets.listIterator()
every { MainConfig.version } returns version
Expand Down Expand Up @@ -441,11 +426,6 @@ class KmpSourceSetsConfiguratorSpec {
kotlin
)

invokeGradleAction(
{ probe -> project.afterEvaluate(probe) },
project
)

every { kotlin.sourceSets } returns sources
every { sources.iterator() } returns sourceSets.listIterator()
every { MainConfig.version } returns version
Expand Down

0 comments on commit 5ae7db8

Please sign in to comment.