add outTrimmed & useHomeDirAsWorkingDir for LocalProcessor

This commit is contained in:
ansgarz 2024-09-12 20:33:10 +02:00
parent e8c0c97dbe
commit 9630e23ede
3 changed files with 24 additions and 3 deletions

View file

@ -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 {

View file

@ -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 {

View file

@ -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()