Skip to content

Commit 5da4ca9

Browse files
committed
use stable temp dir for test projects
1 parent 0095fff commit 5da4ca9

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

modules/bcv-gradle-plugin-functional-tests/build.gradle.kts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ testing.suites {
5959
"devMavenRepoDir",
6060
devPublish.devMavenRepo.asFile.get().invariantSeparatorsPath,
6161
)
62-
val tmpDir = layout.buildDirectory.dir("test-temp").get().asFile
62+
val testTempDir = layout.buildDirectory.dir("test-temp").get().asFile
6363
systemProperty(
64-
"java.io.tmpdir",
65-
tmpDir.absoluteFile.invariantSeparatorsPath
64+
"testTempDir",
65+
testTempDir.invariantSeparatorsPath
6666
)
6767
doFirst {
68-
tmpDir.mkdirs()
68+
testTempDir.mkdirs()
6969
}
7070
}
7171
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package dev.adamko.kotlin.binary_compatibility_validator.test.utils
2+
3+
import java.nio.file.Path
4+
import kotlin.io.path.ExperimentalPathApi
5+
import kotlin.io.path.Path
6+
import kotlin.io.path.createDirectories
7+
import kotlin.io.path.deleteRecursively
8+
import kotlin.jvm.optionals.getOrNull
9+
import org.junit.jupiter.api.extension.AnnotatedElementContext
10+
import org.junit.jupiter.api.extension.ExtensionContext
11+
import org.junit.jupiter.api.io.TempDirFactory
12+
13+
/**
14+
* Create temp dir with a stable name based on the current test name.
15+
*
16+
* @see TempDirFactory.Standard
17+
*/
18+
class TempTestNameDirFactory : TempDirFactory {
19+
20+
@OptIn(ExperimentalPathApi::class)
21+
override fun createTempDirectory(
22+
elementContext: AnnotatedElementContext,
23+
extensionContext: ExtensionContext,
24+
): Path {
25+
26+
// convert `${TestClass.fqn}.${testDisplayName}`
27+
// to file path, e.g. `f/q/n/TestClass/test-display-name`
28+
val path = listOfNotNull(
29+
extensionContext.testClass.getOrNull()?.canonicalName,
30+
extensionContext.displayName,
31+
).joinToString("/") { segment ->
32+
segment
33+
.split(".")
34+
.joinToString(separator = "/") {
35+
it
36+
.map { c -> if (c.isLetterOrDigit()) c else '-' }
37+
.dropLastWhile(Char::isLetterOrDigit)
38+
.dropWhile(Char::isLetterOrDigit)
39+
.joinToString("")
40+
}
41+
}
42+
43+
val dir = testTempDir.resolve(path)
44+
dir.deleteRecursively()
45+
dir.createDirectories()
46+
return dir
47+
}
48+
49+
companion object {
50+
private val testTempDir: Path by systemProperty(::Path)
51+
}
52+
}

modules/bcv-gradle-plugin-functional-tests/src/testFixtures/kotlin/api/BaseKotlinGradleTest.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package dev.adamko.kotlin.binary_compatibility_validator.test.utils.api
22

3+
import dev.adamko.kotlin.binary_compatibility_validator.test.utils.TempTestNameDirFactory
34
import java.io.File
45
import org.junit.jupiter.api.io.CleanupMode.ON_SUCCESS
56
import org.junit.jupiter.api.io.TempDir
67

78
open class BaseKotlinGradleTest {
8-
@TempDir(cleanup = ON_SUCCESS)
9+
@TempDir(
10+
factory = TempTestNameDirFactory::class,
11+
cleanup = ON_SUCCESS,
12+
)
913
lateinit var testTempDir: File
1014

1115
val rootProjectDir: File get() = testTempDir.resolve("bcv-test-project")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package dev.adamko.kotlin.binary_compatibility_validator.test.utils
2+
3+
import kotlin.properties.ReadOnlyProperty
4+
5+
6+
internal fun <T> systemProperty(
7+
convert: (String) -> T
8+
): ReadOnlyProperty<Any, T> =
9+
ReadOnlyProperty { _, property ->
10+
val value = requireNotNull(System.getProperty(property.name)) {
11+
"system property ${property.name} is unavailable"
12+
}
13+
convert(value)
14+
}

0 commit comments

Comments
 (0)