Skip to content

Commit

Permalink
test: fix flaky tests (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
Prototik committed Jan 13, 2024
1 parent e8b9099 commit fa1d15c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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",
Expand Down
33 changes: 32 additions & 1 deletion testlib/src/main/kotlin/rocks/aur/cursedpublish/testlib/Utils.kt
Original file line number Diff line number Diff line change
@@ -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 <T> ThreadLocal<T>.getValue(thisRef: Any?, property: KProperty<*>): T = get()
fun TestConfiguration.tempfilePerTest(prefix: String? = null, suffix: String? = null): ReadOnlyProperty<Any?, Path> =
object : TempPathProvider() {
override fun createPath(): Path {
return createTempFile(prefix ?: this@tempfilePerTest.javaClass.name, suffix)
}
}.also(::register)

@OptIn(ExperimentalPathApi::class)
private abstract class TempPathProvider : ReadOnlyProperty<Any?, Path>, TestListener {
private val stack = ArrayDeque<Path>()

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()
}
}

0 comments on commit fa1d15c

Please sign in to comment.