From 54b263e63429fec08ec5cb6d98de70382924c835 Mon Sep 17 00:00:00 2001 From: Pierrick Greze Date: Sat, 13 Feb 2021 15:29:08 +0900 Subject: [PATCH] Increase code coverage (#2) --- .gitignore | 2 + .../com/github/pgreze/process/Process.kt | 2 +- .../github/pgreze/process/InputSourceTest.kt | 17 +++++++++ .../github/pgreze/process/ProcessKtTest.kt | 37 +++++++++++++++++-- 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 3c34285..c154664 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,5 @@ fastlane/readme.md # Version control vcs.xml + +.netlify diff --git a/src/main/kotlin/com/github/pgreze/process/Process.kt b/src/main/kotlin/com/github/pgreze/process/Process.kt index a545a12..340e119 100644 --- a/src/main/kotlin/com/github/pgreze/process/Process.kt +++ b/src/main/kotlin/com/github/pgreze/process/Process.kt @@ -50,7 +50,7 @@ suspend fun process( process.inputStream.lineFlow(stdout.consumer) } if (stderr is Redirect.Consume) { - process.inputStream.lineFlow(stderr.consumer) + process.errorStream.lineFlow(stderr.consumer) } val output = async { diff --git a/src/test/kotlin/com/github/pgreze/process/InputSourceTest.kt b/src/test/kotlin/com/github/pgreze/process/InputSourceTest.kt index 8e31c29..653cafb 100644 --- a/src/test/kotlin/com/github/pgreze/process/InputSourceTest.kt +++ b/src/test/kotlin/com/github/pgreze/process/InputSourceTest.kt @@ -10,8 +10,11 @@ import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Nested import org.junit.jupiter.api.RepeatedTest import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir import java.io.ByteArrayInputStream +import java.nio.file.Path import java.util.Collections +import kotlin.io.path.writeText @ExperimentalCoroutinesApi class InputSourceTest { @@ -30,6 +33,20 @@ class InputSourceTest { output shouldBeEqualTo listOf(STRING) } + @Test + fun fromFile(@TempDir dir: Path) = runSuspendTest { + val input = dir.resolve("input.txt").toFile() + input.writeText(STRING) + + val output = process( + "cat", + stdin = InputSource.FromFile(input), + stdout = Redirect.CAPTURE, + ).unwrap() + + output shouldBeEqualTo listOf(STRING) + } + @Test fun fromStream() = runSuspendTest { val inputStream = ByteArrayInputStream(STRING.toByteArray()) diff --git a/src/test/kotlin/com/github/pgreze/process/ProcessKtTest.kt b/src/test/kotlin/com/github/pgreze/process/ProcessKtTest.kt index 0f68504..b8b38ba 100644 --- a/src/test/kotlin/com/github/pgreze/process/ProcessKtTest.kt +++ b/src/test/kotlin/com/github/pgreze/process/ProcessKtTest.kt @@ -18,6 +18,7 @@ import org.junit.jupiter.api.io.TempDir import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource import java.io.ByteArrayOutputStream +import java.io.File import java.io.PrintStream import java.nio.file.Path import kotlin.io.path.ExperimentalPathApi @@ -78,6 +79,18 @@ class ProcessKtTest { output shouldContain "$name=$value" } + @Test + fun `directory is allowing to change folder`() = runSuspendTest { + // Notice: use a temporary path in OSX is failing due to /tmp -> /private/var symlink... + val dir = File(".").absoluteFile.parentFile + val output = process( + "pwd", "-L", + directory = dir, + stdout = CAPTURE + ).unwrap() + output shouldBeEqualTo listOf(dir.path) + } + @Test fun `process redirect to files`(@TempDir dir: Path) = runSuspendTest { val script = dir.createScript() @@ -117,15 +130,33 @@ class ProcessKtTest { @Test fun `use Consume when CAPTURE is unnecessary`(@TempDir dir: Path) = runSuspendTest { val script = dir.createScript() - val consumer = mutableListOf() + val stdout = mutableListOf() + val stderr = mutableListOf() + + val output = process( + script.absolutePathString(), *ALL, + stdout = Consume { it.toList(stdout) }, + stderr = Consume { it.toList(stderr) }, + ).unwrap() + + output shouldBeEqualTo emptyList() + stdout shouldBeEqualTo OUT.toList() + stderr shouldBeEqualTo ERR.toList() + } + + @Test + fun `ensure Consume and CAPTURE are plawing well`(@TempDir dir: Path) = runSuspendTest { + val script = dir.createScript() + val stdout = mutableListOf() + val output = process( script.absolutePathString(), *ALL, - stdout = Consume { it.toList(consumer) }, + stdout = Consume { it.toList(stdout) }, stderr = CAPTURE, ).unwrap() output shouldBeEqualTo ERR.toList() - consumer shouldBeEqualTo OUT.toList() + stdout shouldBeEqualTo OUT.toList() } @Nested