Skip to content

Commit 41002d9

Browse files
committed
fix: Moved Gradle TestKit directory to separate OS temp directory
In 748292c the GradleRuner used the temporary directory of the current test (projectDir) which comes from JUnit's `@TempDir`. When JUnit finishes its test it tries to cleanupt the temporary folder, however the daemon may still be running and lock files under this folder. With the change each fixture still gets its own isolated testkit dir to ensure the daemon isolation preserved. gradle/gradle#12535
1 parent 51fe2ea commit 41002d9

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

buildSrc/src/test/kotlin/datadog/gradle/plugin/GradleFixture.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@ import org.gradle.testkit.runner.UnexpectedBuildResultException
66
import org.intellij.lang.annotations.Language
77
import org.w3c.dom.Document
88
import java.io.File
9+
import java.nio.file.Files
910
import javax.xml.parsers.DocumentBuilderFactory
1011

1112
/**
1213
* Base fixture for Gradle plugin integration tests.
1314
* Provides common functionality for setting up test projects and running Gradle builds.
1415
*/
1516
internal open class GradleFixture(protected val projectDir: File) {
17+
// Keep test-kit caches outside @TempDir so Gradle daemon file-locks on
18+
// .testkit/caches/*/transforms don't cause JUnit @TempDir cleanup failures
19+
// (DirectoryNotEmptyException). Each fixture still gets its own testkit dir,
20+
// ensuring daemon isolation (fresh MAVEN_REPOSITORY_PROXY per test).
21+
// Cleanup is left to the OS (/tmp is ephemeral on CI containers).
22+
private val testKitDir: File by lazy {
23+
Files.createTempDirectory("gradle-testkit-").toFile()
24+
}
25+
1626
/**
1727
* Runs Gradle with the specified arguments.
1828
*
@@ -23,12 +33,7 @@ internal open class GradleFixture(protected val projectDir: File) {
2333
*/
2434
fun run(vararg args: String, expectFailure: Boolean = false, env: Map<String, String> = emptyMap()): BuildResult {
2535
val runner = GradleRunner.create()
26-
// Use a testkit dir scoped to this fixture's projectDir. The Tooling API always uses a
27-
// daemon and ignores org.gradle.daemon=false. By giving each test its own testkit dir,
28-
// we force a fresh daemon per test — ensuring withEnvironment() vars (e.g.
29-
// MAVEN_REPOSITORY_PROXY) are correctly set on the daemon JVM and not inherited from
30-
// a previously-started daemon with a different test's environment.
31-
.withTestKitDir(file(".testkit"))
36+
.withTestKitDir(testKitDir)
3237
.withPluginClasspath()
3338
.withProjectDir(projectDir)
3439
.withEnvironment(System.getenv() + env)

0 commit comments

Comments
 (0)