diff --git a/cmd.go b/cmd.go index a1417f8..8902d11 100644 --- a/cmd.go +++ b/cmd.go @@ -334,6 +334,8 @@ func (c *Cmd) run(in io.Reader) { // 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 @@ -643,3 +645,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 + } +} diff --git a/cmd_test.go b/cmd_test.go index 0e0a1fe..1660d3c 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -560,7 +560,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)) @@ -582,6 +582,8 @@ func TestStreamingMultipleLines(t *testing.T) { t.Errorf("got line: '%s', expected 'foo'", gotLine) } + out.Flush() + // Get next line select { case gotLine = <-lines: