Skip to content
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
10 changes: 10 additions & 0 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ func (c *Cmd) run() {
// who's waiting for us to close them.
if c.stdoutStream != nil {
defer func() {
c.stdoutStream.Flush()
c.stderrStream.Flush()
// exec.Cmd.Wait has already waited for all output:
// Otherwise, during the execution of the command a separate goroutine
// reads from the process over a pipe and delivers that data to the
Expand Down Expand Up @@ -635,3 +637,11 @@ func (rw *OutputStream) SetLineBufferSize(n int) {
rw.bufSize = n
rw.buf = make([]byte, rw.bufSize)
}

// Flush empties the buffer of its last line.
func (rw *OutputStream) Flush() {
if rw.lastChar > 0 {
line := string(rw.buf[0:rw.lastChar])
rw.streamChan <- line
}
}
4 changes: 3 additions & 1 deletion cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ func TestStreamingMultipleLines(t *testing.T) {
}

// Write two short lines
input := "foo\nbar\n"
input := "foo\nbar"
n, err := out.Write([]byte(input))
if n != len(input) {
t.Errorf("Write n = %d, expected %d", n, len(input))
Expand All @@ -581,6 +581,8 @@ func TestStreamingMultipleLines(t *testing.T) {
t.Errorf("got line: '%s', expected 'foo'", gotLine)
}

out.Flush()

// Get next line
select {
case gotLine = <-lines:
Expand Down