Skip to content

Commit

Permalink
Increase code coverage (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgreze authored Feb 13, 2021
1 parent 43df029 commit 54b263e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ fastlane/readme.md

# Version control
vcs.xml

.netlify
2 changes: 1 addition & 1 deletion src/main/kotlin/com/github/pgreze/process/Process.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions src/test/kotlin/com/github/pgreze/process/InputSourceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
Expand Down
37 changes: 34 additions & 3 deletions src/test/kotlin/com/github/pgreze/process/ProcessKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<String>()
val stdout = mutableListOf<String>()
val stderr = mutableListOf<String>()

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

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
Expand Down

0 comments on commit 54b263e

Please sign in to comment.