Skip to content

Commit

Permalink
Merge pull request #51 from go-cmd/dn/tweak-test
Browse files Browse the repository at this point in the history
Test previuos commit in its own test
  • Loading branch information
daniel-nichter authored Oct 31, 2020
2 parents 6f93998 + 7267ee3 commit 6ae68a4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 49 additions & 2 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6ae68a4

Please sign in to comment.