diff --git a/src/test/kotlin/rocks/aur/cursedpublish/internal/infer/FabricModInferSpec.kt b/src/test/kotlin/rocks/aur/cursedpublish/internal/infer/FabricModInferSpec.kt index 7413a0d..fab7384 100644 --- a/src/test/kotlin/rocks/aur/cursedpublish/internal/infer/FabricModInferSpec.kt +++ b/src/test/kotlin/rocks/aur/cursedpublish/internal/infer/FabricModInferSpec.kt @@ -5,24 +5,24 @@ package rocks.aur.cursedpublish.internal.infer import io.github.z4kn4fein.semver.* import io.kotest.core.spec.style.* import io.kotest.datatest.* -import io.kotest.engine.spec.* import io.kotest.matchers.collections.* import kotlinx.serialization.* import org.gradle.kotlin.dsl.* import rocks.aur.cursedpublish.* import rocks.aur.cursedpublish.internal.* import rocks.aur.cursedpublish.testlib.* -import java.io.* +import java.nio.file.* import java.util.jar.* import kotlin.collections.component1 import kotlin.collections.component2 import kotlin.collections.set +import kotlin.io.path.* object FabricModInferSpec : FunSpec({ - val modFile: File by ThreadLocal.withInitial { tempfile() } + val modFile: Path by tempfilePerTest() fun infer() = with(FabricModInfer) { - TestInferScope.inferGameVersions(modFile) + TestInferScope.inferGameVersions(modFile.toFile()) } test("should infer fabric loader") { @@ -95,7 +95,7 @@ object FabricModInferSpec : FunSpec({ }) private fun genDummyFabricMod( - file: File, + file: Path, modId: String = "dummy", modVersion: Version = Version.parse("1.2.3"), minecraftVersion: String = "1.20.4", diff --git a/testlib/src/main/kotlin/rocks/aur/cursedpublish/testlib/Utils.kt b/testlib/src/main/kotlin/rocks/aur/cursedpublish/testlib/Utils.kt index bb3987e..33f444f 100644 --- a/testlib/src/main/kotlin/rocks/aur/cursedpublish/testlib/Utils.kt +++ b/testlib/src/main/kotlin/rocks/aur/cursedpublish/testlib/Utils.kt @@ -1,5 +1,36 @@ package rocks.aur.cursedpublish.testlib +import io.kotest.core.* +import io.kotest.core.listeners.* +import io.kotest.core.test.* +import java.nio.file.* +import kotlin.io.path.* +import kotlin.properties.* import kotlin.reflect.* -operator fun ThreadLocal.getValue(thisRef: Any?, property: KProperty<*>): T = get() \ No newline at end of file +fun TestConfiguration.tempfilePerTest(prefix: String? = null, suffix: String? = null): ReadOnlyProperty = + object : TempPathProvider() { + override fun createPath(): Path { + return createTempFile(prefix ?: this@tempfilePerTest.javaClass.name, suffix) + } + }.also(::register) + +@OptIn(ExperimentalPathApi::class) +private abstract class TempPathProvider : ReadOnlyProperty, TestListener { + private val stack = ArrayDeque() + + abstract fun createPath(): Path + + override suspend fun beforeAny(testCase: TestCase) { + stack.add(createPath()) + } + + override suspend fun afterAny(testCase: TestCase, result: TestResult) { + val file = stack.removeLast() + file.deleteRecursively() + } + + override fun getValue(thisRef: Any?, property: KProperty<*>): Path { + return stack.last() + } +} \ No newline at end of file