Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim last new line char from buffer if it is present #68

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}
Loading