Skip to content

Commit 805ae0b

Browse files
authored
Fix deleting stale files in Gradle transform task (#550)
When source files are deleted Atomicfu would not delete the transformed files. Previously transformed files should be deleted whenever the transformer runs, so stale files do not linger. #547
1 parent 2b7b810 commit 805ae0b

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ private fun Project.checkCompatibility(afuPluginVersion: String) {
7474
)
7575
}
7676
if (!kotlinVersion.atLeast(1, 9, 0)) {
77-
// Since Kotlin 1.9.0 the logic of the Gradle plugin from the Kotlin repo (AtomicfuKotlinGradleSubplugin)
78-
// may be moved to the Gradle plugin in the library. The sources of the compiler plugin
77+
// Since Kotlin 1.9.0 the logic of the Gradle plugin from the Kotlin repo (AtomicfuKotlinGradleSubplugin)
78+
// may be moved to the Gradle plugin in the library. The sources of the compiler plugin
7979
// are published as `kotlin-atomicfu-compiler-plugin-embeddable` since Kotlin 1.9.0 and may be accessed out of the Kotlin repo.
8080
error(
8181
"You are applying `kotlinx-atomicfu` plugin of version $afuPluginVersion. " +
@@ -487,6 +487,7 @@ abstract class AtomicFUTransformTask : ConventionTask() {
487487

488488
@TaskAction
489489
fun transform() {
490+
destinationDirectory.get().asFile.deleteRecursively()
490491
val cp = classPath.files.map { it.absolutePath }
491492
inputFiles.files.forEach { inputDir ->
492493
AtomicFUTransformer(cp, inputDir, destinationDirectory.get().asFile).let { t ->

integration-testing/src/functionalTest/kotlin/kotlinx.atomicfu.gradle.plugin.test/cases/JvmProjectTest.kt

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,50 @@ class JvmProjectTest {
3333
jvmSample.checkConsumableDependencies(false)
3434
jvmSample.buildAndCheckBytecode()
3535
}
36-
36+
3737
// This test checks that jar is packed without duplicates, see #303
3838
@Test
3939
fun testJar() {
4040
assertTrue(jvmSample.cleanAndJar().isSuccessful)
4141
}
42+
43+
@Test
44+
fun testFilesDeleted() {
45+
46+
val buildClassesAtomicfuDir = jvmSample.targetDir.resolve("build/classes/atomicfu")
47+
48+
/** Get all that Atomicfu generates in `build/classes/atomicfu/` */
49+
fun buildClassesAtomicFuDirFiles(): String =
50+
buildClassesAtomicfuDir.walk()
51+
.filter { it.isFile }
52+
.map { it.relativeTo(jvmSample.targetDir).invariantSeparatorsPath }
53+
.sorted()
54+
.joinToString("\n")
55+
56+
jvmSample.build().apply {
57+
assertTrue { buildClassesAtomicfuDir.exists() }
58+
assertEquals(
59+
"""
60+
build/classes/atomicfu/main/IntArithmetic.class
61+
build/classes/atomicfu/main/META-INF/jvm-sample.kotlin_module
62+
build/classes/atomicfu/main/MainKt.class
63+
build/classes/atomicfu/test/ArithmeticTest.class
64+
build/classes/atomicfu/test/META-INF/jvm-sample_test.kotlin_module
65+
""".trimIndent(),
66+
buildClassesAtomicFuDirFiles()
67+
)
68+
}
69+
70+
val projectSrcDir = jvmSample.targetDir.resolve("src")
71+
projectSrcDir.deleteRecursively()
72+
assertFalse { projectSrcDir.exists() }
73+
74+
jvmSample.build().apply {
75+
assertTrue { buildClassesAtomicfuDir.exists() }
76+
assertEquals(
77+
"",
78+
buildClassesAtomicFuDirFiles(),
79+
)
80+
}
81+
}
4282
}

integration-testing/src/functionalTest/kotlin/kotlinx.atomicfu.gradle.plugin.test/framework/runner/BuildRunner.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ package kotlinx.atomicfu.gradle.plugin.test.framework.runner
77
import java.io.File
88
import java.nio.file.Files
99

10+
/**
11+
* @param[targetDir] The root Gradle project directory.
12+
*/
1013
internal class GradleBuild(val projectName: String, val targetDir: File) {
1114
var enableJvmIrTransformation = false
1215
var enableJsIrTransformation = false

integration-testing/src/functionalTest/kotlin/kotlinx.atomicfu.gradle.plugin.test/framework/runner/Commands.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@ package kotlinx.atomicfu.gradle.plugin.test.framework.runner
66

77
import kotlinx.atomicfu.gradle.plugin.test.framework.checker.getProjectClasspath
88

9+
internal fun GradleBuild.build(): BuildResult =
10+
runGradle(listOf("build")).also {
11+
require(it.isSuccessful) { "${this.projectName}:build task FAILED: ${it.output} " }
12+
}
13+
914
internal fun GradleBuild.cleanAndBuild(): BuildResult =
1015
runGradle(listOf("clean", "build")).also {
1116
require(it.isSuccessful) { "${this.projectName}:build task FAILED: ${it.output} " }
1217
}
1318

1419
internal fun GradleBuild.cleanAndJar(): BuildResult = runGradle(listOf("clean", "jar"))
1520

16-
internal fun GradleBuild.dependencies(): BuildResult =
17-
runGradle(listOf("dependencies")).also {
21+
internal fun GradleBuild.dependencies(): BuildResult =
22+
runGradle(listOf("dependencies")).also {
1823
require(it.isSuccessful) { "${this.projectName}:dependencies task FAILED: ${it.output} " }
1924
}
2025

0 commit comments

Comments
 (0)