Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hick-up on repetitive execution #56

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ internal interface KMockPluginContract {
)
}

interface KspSharedSourceCleaner {
fun cleanKspSources(
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,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,
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 KspSharedSourceCleaner : KMockPluginContract.KspSharedSourceCleaner {
override fun cleanKspSources(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 @@ -33,11 +33,13 @@ class KmpSetupConfiguratorSpec {
@BeforeEach
fun setUp() {
mockkObject(SharedSourceCopist)
mockkObject(KspSharedSourceCleaner)
}

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

@Test
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
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 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
}
}