From 9630e23edec720864355ed963b594f7e0f8169e4 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Thu, 12 Sep 2024 20:33:10 +0200 Subject: [PATCH] add outTrimmed & useHomeDirAsWorkingDir for LocalProcessor --- .../provs/framework/core/ProvResult.kt | 2 ++ .../framework/core/processors/LocalProcessor.kt | 10 ++++++++-- .../core/processors/LocalProcessorTest.kt | 15 ++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/ProvResult.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/ProvResult.kt index 291e30a..1e8f048 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/ProvResult.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/ProvResult.kt @@ -8,6 +8,8 @@ data class ProvResult(val success: Boolean, val exception: Exception? = null, val exit: String? = null) { + val outTrimmed: String? = out?.trim() + constructor(returnCode : Int) : this(returnCode == 0) override fun toString(): String { diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/processors/LocalProcessor.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/processors/LocalProcessor.kt index 6ab774d..3a443cc 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/processors/LocalProcessor.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/core/processors/LocalProcessor.kt @@ -5,13 +5,14 @@ import org.slf4j.LoggerFactory import java.io.File import java.io.IOException import java.nio.charset.Charset +import java.nio.file.Paths private fun getOsName(): String { return System.getProperty("os.name") } -open class LocalProcessor : Processor { +open class LocalProcessor(val useHomeDirAsWorkingDir: Boolean = true) : Processor { companion object { @Suppress("JAVA_CLASS_ON_COMPANION") @@ -26,7 +27,12 @@ open class LocalProcessor : Processor { private fun workingDir() : String { - return System.getProperty("user.home") ?: File.separator + return if (useHomeDirAsWorkingDir) { + System.getProperty("user.home") ?: File.separator + } else { + // folder in which program was started + Paths.get("").toAbsolutePath().toString() + } } override fun exec(vararg args: String): ProcessResult { diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/processors/LocalProcessorTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/processors/LocalProcessorTest.kt index ea5a91c..8de7775 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/processors/LocalProcessorTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/core/processors/LocalProcessorTest.kt @@ -6,6 +6,7 @@ import org.domaindrivenarchitecture.provs.framework.core.escapeProcentForPrintf import org.domaindrivenarchitecture.provs.framework.core.escapeSingleQuoteForShell import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test +import java.nio.file.Paths internal class LocalProcessorTest { @@ -24,6 +25,18 @@ internal class LocalProcessorTest { assertTrue(res.out == text) } + @Test + fun cmd_in_folder_where_program_was_started() { + // given + val prov = Prov.newInstance(LocalProcessor(false)) + + // when + val pwd = prov.cmd("pwd").outTrimmed + + // then + assertEquals(Paths.get("").toAbsolutePath().toString(), pwd) + } + @Test fun cmd_with_nested_shell_and_printf() { @@ -59,7 +72,7 @@ internal class LocalProcessorTest { @Test - fun cmd_forUnkownCommand_resultWithError() { + fun cmd_forUnknownCommand_resultWithError() { // given val prov = Prov.newInstance()