-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
37 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 32 additions & 1 deletion
33
testlib/src/main/kotlin/rocks/aur/cursedpublish/testlib/Utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |