aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-11-27 16:04:05 +0100
committerLinnea Gräf <nea@nea.moe>2024-11-27 16:05:19 +0100
commitee0526ac67e093f77d17747d9316765b2db272a2 (patch)
tree1e2f8a26475afcc8c0d36b85068e1a2b282410ab /src/main
parent9df1f12970dafdbf4cb13c76c1df1caed66f4a59 (diff)
downloadFirmament-ee0526ac67e093f77d17747d9316765b2db272a2.tar.gz
Firmament-ee0526ac67e093f77d17747d9316765b2db272a2.tar.bz2
Firmament-ee0526ac67e093f77d17747d9316765b2db272a2.zip
test: Use kotest
Diffstat (limited to 'src/main')
-rw-r--r--src/main/kotlin/util/JvmUtil.kt32
-rw-r--r--src/main/kotlin/util/MC.kt2
-rw-r--r--src/main/kotlin/util/TestUtil.kt5
3 files changed, 37 insertions, 2 deletions
diff --git a/src/main/kotlin/util/JvmUtil.kt b/src/main/kotlin/util/JvmUtil.kt
new file mode 100644
index 0000000..5be5ebd
--- /dev/null
+++ b/src/main/kotlin/util/JvmUtil.kt
@@ -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)
+ }
+ }
+
+}
diff --git a/src/main/kotlin/util/MC.kt b/src/main/kotlin/util/MC.kt
index f7c81da..a60d5c4 100644
--- a/src/main/kotlin/util/MC.kt
+++ b/src/main/kotlin/util/MC.kt
@@ -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
diff --git a/src/main/kotlin/util/TestUtil.kt b/src/main/kotlin/util/TestUtil.kt
index 68a406f..2d38f35 100644
--- a/src/main/kotlin/util/TestUtil.kt
+++ b/src/main/kotlin/util/TestUtil.kt
@@ -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.")
+ }
}