From 824b6f219b3ccdb949305850e2c57a01c28ad31f Mon Sep 17 00:00:00 2001 From: Matthew Nelson Date: Mon, 5 Feb 2024 21:10:59 -0500 Subject: [PATCH] Rework tests --- .../matthewnelson/process/ProcessBaseTest.kt | 56 ++++++++++++++----- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/library/process/src/commonTest/kotlin/io/matthewnelson/process/ProcessBaseTest.kt b/library/process/src/commonTest/kotlin/io/matthewnelson/process/ProcessBaseTest.kt index 2c122a3..7777150 100644 --- a/library/process/src/commonTest/kotlin/io/matthewnelson/process/ProcessBaseTest.kt +++ b/library/process/src/commonTest/kotlin/io/matthewnelson/process/ProcessBaseTest.kt @@ -20,6 +20,8 @@ import io.matthewnelson.kmp.file.path import io.matthewnelson.kmp.file.resolve import io.matthewnelson.kmp.tor.resource.tor.TorResources import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.currentCoroutineContext +import kotlinx.coroutines.job import kotlinx.coroutines.test.runTest import kotlinx.coroutines.withContext import kotlin.test.* @@ -38,15 +40,24 @@ abstract class ProcessBaseTest { @Test fun givenWaitFor_whenProcessExits_thenWaitForReturnsEarly() { - if (!isUnixDesktop || isNodeJS) { + if (isNodeJS) { println("Skipping...") return } val runTime = measureTime { - val p = Process.Builder("sleep") - .arg("0.25") - .start() + val p = try { + Process.Builder("sleep") + .arg("0.25") + .start() + } catch (e: ProcessException) { + // Host (Window or iOS) did not have sleep available + if (!isUnixDesktop) { + println("Skipping...") + return + } + throw e + } assertNull(p.waitFor(100.milliseconds)) assertTrue(p.isAlive) @@ -76,29 +87,42 @@ abstract class ProcessBaseTest { @Test fun givenWaitFor_whenCompletion_thenReturnsExitCode() = runTest { - if (!isUnixDesktop || isNodeJS) { + if (isNodeJS) { println("Skipping...") return@runTest } - val p = Process.Builder("sleep") - .arg("1") - .start() + val p = try { + Process.Builder("sleep") + .arg("1") + .start() + } catch (e: ProcessException) { + // Host (Window or iOS) did not have sleep available + if (!isUnixDesktop) { + println("Skipping...") + return@runTest + } + throw e + } assertEquals(0, p.waitFor()) } @Test fun givenWaitForAsync_whenCompletion_thenReturnsExitCode() = runTest { - if (!isUnixDesktop || isNodeJS) { - println("Skipping...") - return@runTest + val p = try { + Process.Builder("sleep") + .arg("1") + .start() + } catch (e: ProcessException) { + // Host (Window or iOS) did not have sleep available + if (!isUnixDesktop) { + println("Skipping...") + return@runTest + } + throw e } - val p = Process.Builder("sleep") - .arg("1") - .start() - val exitCode = withContext(Dispatchers.Default) { p.waitForAsync() } @@ -132,6 +156,8 @@ abstract class ProcessBaseTest { .environment("HOME", installer.installationDir.path) .start() + currentCoroutineContext().job.invokeOnCompletion { p.sigkill() } + println("CMD[${p.command}]") p.args.forEach { arg -> println("ARG[$arg]") }