Skip to content

Commit

Permalink
Trim last new line char from buffer if it is present (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm authored Feb 28, 2024
1 parent 54e8845 commit 1ada871
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,15 @@ class ProcessAndroidTest: ProcessBaseTest() {
f.delete()
f.deleteOnExit()

val dStdio = Stdio.File.of(d)
val fStdio = Stdio.File.of(f)
val stdioDir = Stdio.File.of(d)
val stdioFile = Stdio.File.of(f)

val b = Process.Builder(command = "sh")
.args("-c")
.args("sleep 0.25")
.stdin(fStdio)

// Should fail for not existing
assertFailsWith<IOException> { b.spawn {} }

b.stdin(dStdio)
assertTrue(d.mkdirs())

// Should fail for not being a file
b.stdin(stdioFile)
assertFailsWith<IOException> { b.spawn {} }

assertTrue(f.createNewFile())
Expand All @@ -107,23 +101,29 @@ class ProcessAndroidTest: ProcessBaseTest() {

// Should fail for not being able to read
assertFailsWith<IOException> { b.spawn {} }

assertTrue(d.mkdirs())

// Should fail for not being a file
b.stdin(stdioDir)
assertFailsWith<IOException> { b.spawn {} }
b.stdin(Stdio.Pipe)

// Should fail for not being a file
b.stdout(dStdio)
b.stdout(stdioDir)
assertFailsWith<IOException> { b.spawn {} }

// Should fail for not being able to write
b.stdout(fStdio)
b.stdout(stdioFile)
assertFailsWith<IOException> { b.spawn {} }
b.stdout(Stdio.Pipe)

// Should fail for not being a file
b.stderr(dStdio)
b.stderr(stdioDir)
assertFailsWith<IOException> { b.spawn {} }

// Should fail for not being able to write
b.stderr(fStdio)
b.stderr(stdioFile)
assertFailsWith<IOException> { b.spawn {} }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ abstract class ProcessBaseTest {
println(output.stdout)
println(output.stderr)
println(output)
assertEquals(d.canonicalPath(), output.stdout.trim())
assertEquals(d.canonicalPath(), output.stdout)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ internal actual class PlatformBuilder private actual constructor() {
val pid = output["pid"] as Int

val stdout = Buffer.wrap(output["stdout"]).let { buf ->
val utf8 = buf.toUtf8()
val utf8 = buf.toUtf8Trimmed()
buf.fill()
utf8
}

val stderr = Buffer.wrap(output["stderr"]).let { buf ->
val utf8 = buf.toUtf8()
val utf8 = buf.toUtf8Trimmed()
buf.fill()
utf8
}
Expand Down Expand Up @@ -219,5 +219,16 @@ internal actual class PlatformBuilder private actual constructor() {

return result
}

private const val N = '\n'.code.toByte()

@Suppress("NOTHING_TO_INLINE")
private inline fun Buffer.toUtf8Trimmed(): String {
var limit = length.toInt()
if (limit == 0) return ""

if (readInt8(limit - 1) == N) limit--
return toUtf8(end = limit)
}
}
}

0 comments on commit 1ada871

Please sign in to comment.