Skip to content

Commit

Permalink
Merge pull request #48 from greut/flush-then-close
Browse files Browse the repository at this point in the history
fix: last line is never sent
  • Loading branch information
daniel-nichter authored Oct 31, 2020
2 parents 75bd92d + b66c7d8 commit 6f93998
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 10 additions & 0 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
4 changes: 3 additions & 1 deletion cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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:
Expand Down

0 comments on commit 6f93998

Please sign in to comment.