add outTrimmed & useHomeDirAsWorkingDir for LocalProcessor
This commit is contained in:
parent
e8c0c97dbe
commit
9630e23ede
3 changed files with 24 additions and 3 deletions
|
@ -8,6 +8,8 @@ data class ProvResult(val success: Boolean,
|
||||||
val exception: Exception? = null,
|
val exception: Exception? = null,
|
||||||
val exit: String? = null) {
|
val exit: String? = null) {
|
||||||
|
|
||||||
|
val outTrimmed: String? = out?.trim()
|
||||||
|
|
||||||
constructor(returnCode : Int) : this(returnCode == 0)
|
constructor(returnCode : Int) : this(returnCode == 0)
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
|
|
|
@ -5,13 +5,14 @@ import org.slf4j.LoggerFactory
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
|
import java.nio.file.Paths
|
||||||
|
|
||||||
|
|
||||||
private fun getOsName(): String {
|
private fun getOsName(): String {
|
||||||
return System.getProperty("os.name")
|
return System.getProperty("os.name")
|
||||||
}
|
}
|
||||||
|
|
||||||
open class LocalProcessor : Processor {
|
open class LocalProcessor(val useHomeDirAsWorkingDir: Boolean = true) : Processor {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@Suppress("JAVA_CLASS_ON_COMPANION")
|
@Suppress("JAVA_CLASS_ON_COMPANION")
|
||||||
|
@ -26,7 +27,12 @@ open class LocalProcessor : Processor {
|
||||||
|
|
||||||
private fun workingDir() : String
|
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 {
|
override fun exec(vararg args: String): ProcessResult {
|
||||||
|
|
|
@ -6,6 +6,7 @@ import org.domaindrivenarchitecture.provs.framework.core.escapeProcentForPrintf
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.escapeSingleQuoteForShell
|
import org.domaindrivenarchitecture.provs.framework.core.escapeSingleQuoteForShell
|
||||||
import org.junit.jupiter.api.Assertions.*
|
import org.junit.jupiter.api.Assertions.*
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
import java.nio.file.Paths
|
||||||
|
|
||||||
|
|
||||||
internal class LocalProcessorTest {
|
internal class LocalProcessorTest {
|
||||||
|
@ -24,6 +25,18 @@ internal class LocalProcessorTest {
|
||||||
assertTrue(res.out == text)
|
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
|
@Test
|
||||||
fun cmd_with_nested_shell_and_printf() {
|
fun cmd_with_nested_shell_and_printf() {
|
||||||
|
@ -59,7 +72,7 @@ internal class LocalProcessorTest {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun cmd_forUnkownCommand_resultWithError() {
|
fun cmd_forUnknownCommand_resultWithError() {
|
||||||
// given
|
// given
|
||||||
val prov = Prov.newInstance()
|
val prov = Prov.newInstance()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue