test: Use kotest

This commit is contained in:
Linnea Gräf
2024-11-27 16:04:05 +01:00
parent 9df1f12970
commit ee0526ac67
14 changed files with 371 additions and 27 deletions

View File

@@ -0,0 +1,32 @@
package moe.nea.firmament.util
import com.sun.tools.attach.VirtualMachine
import java.lang.management.ManagementFactory
import java.nio.file.Path
import kotlin.io.path.absolutePathString
object JvmUtil {
fun guessJVMPid(): String {
val name = ManagementFactory.getRuntimeMXBean().name
val pid = name.substringBefore('@')
ErrorUtil.softCheck("Not a valid PID: $pid", pid.toIntOrNull() != null)
return pid
}
fun getVM(): VirtualMachine {
return VirtualMachine.attach(guessJVMPid())
}
fun useVM(block: (VirtualMachine) -> Unit) {
val vm = getVM()
block(vm)
vm.detach()
}
fun loadAgent(jarPath: Path, options: String? = null) {
useVM {
it.loadAgent(jarPath.absolutePathString(), options)
}
}
}

View File

@@ -105,7 +105,7 @@ object MC {
inline val currentRegistries: RegistryWrapper.WrapperLookup? get() = world?.registryManager
val defaultRegistries: RegistryWrapper.WrapperLookup by lazy { BuiltinRegistries.createWrapperLookup() }
inline val currentOrDefaultRegistries get() = currentRegistries ?: defaultRegistries
val defaultItems: RegistryWrapper.Impl<Item> = defaultRegistries.getOrThrow(RegistryKeys.ITEM)
val defaultItems: RegistryWrapper.Impl<Item> by lazy { defaultRegistries.getOrThrow(RegistryKeys.ITEM) }
var lastWorld: World? = null
get() {
field = world ?: field

View File

@@ -1,5 +1,8 @@
package moe.nea.firmament.util
object TestUtil {
val isInTest = Thread.currentThread().stackTrace.any { it.className.startsWith("org.junit.") }
val isInTest =
Thread.currentThread().stackTrace.any {
it.className.startsWith("org.junit.") || it.className.startsWith("io.kotest.")
}
}