diff --git a/CHANGELOG.md b/CHANGELOG.md index aeba1a9..3b7076e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## v1.2 +### v1.2.2 (2020-10-31) + +* Fixed last line not flushed if incomplete (PR #48) (@greut) + ### v1.2.1 (2020-07-11) * Added `StartWithStdin(io.Reader)` (PR #50) (@juanenriqueescobar) diff --git a/cmd_test.go b/cmd_test.go index 1660d3c..d3e5036 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -560,7 +560,54 @@ func TestStreamingMultipleLines(t *testing.T) { } // Write two short lines - input := "foo\nbar" + input := "foo\nbar\n" + n, err := out.Write([]byte(input)) + if n != len(input) { + t.Errorf("Write n = %d, expected %d", n, len(input)) + } + if err != nil { + t.Errorf("got err '%v', expected nil", err) + } + + // Get one line + var gotLine string + select { + case gotLine = <-lines: + default: + t.Fatal("blocked on <-lines") + } + + // "foo" should be sent before "bar" because that was the input + if gotLine != "foo" { + t.Errorf("got line: '%s', expected 'foo'", gotLine) + } + + // Get next line + select { + case gotLine = <-lines: + default: + t.Fatal("blocked on <-lines") + } + + if gotLine != "bar" { + t.Errorf("got line: '%s', expected 'bar'", gotLine) + } +} + +func TestStreamingMultipleLinesLastNotTerminated(t *testing.T) { + // If last line isn't \n terminated, go-cmd should flush it anyway + // https://github.com/go-cmd/cmd/pull/48 + lines := make(chan string, 5) + out := cmd.NewOutputStream(lines) + + // Quick side test: Lines() chan string should be the same chan string + // we created the object with + if out.Lines() != lines { + t.Errorf("Lines() does not return the given string chan") + } + + // Write two short lines + input := "foo\nbar" // <- last line doesn't have \n n, err := out.Write([]byte(input)) if n != len(input) { t.Errorf("Write n = %d, expected %d", n, len(input)) @@ -582,7 +629,7 @@ func TestStreamingMultipleLines(t *testing.T) { t.Errorf("got line: '%s', expected 'foo'", gotLine) } - out.Flush() + out.Flush() // flush our output so go-cmd receives it // Get next line select { diff --git a/go.mod b/go.mod index a65c665..8527fb3 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/go-cmd/cmd -go 1.13 +go 1.14 require github.com/go-test/deep v1.0.6