Skip to content

Commit 6ae68a4

Browse files
Merge pull request #51 from go-cmd/dn/tweak-test
Test previuos commit in its own test
2 parents 6f93998 + 7267ee3 commit 6ae68a4

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## v1.2
44

5+
### v1.2.2 (2020-10-31)
6+
7+
* Fixed last line not flushed if incomplete (PR #48) (@greut)
8+
59
### v1.2.1 (2020-07-11)
610

711
* Added `StartWithStdin(io.Reader)` (PR #50) (@juanenriqueescobar)

cmd_test.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,54 @@ func TestStreamingMultipleLines(t *testing.T) {
560560
}
561561

562562
// Write two short lines
563-
input := "foo\nbar"
563+
input := "foo\nbar\n"
564+
n, err := out.Write([]byte(input))
565+
if n != len(input) {
566+
t.Errorf("Write n = %d, expected %d", n, len(input))
567+
}
568+
if err != nil {
569+
t.Errorf("got err '%v', expected nil", err)
570+
}
571+
572+
// Get one line
573+
var gotLine string
574+
select {
575+
case gotLine = <-lines:
576+
default:
577+
t.Fatal("blocked on <-lines")
578+
}
579+
580+
// "foo" should be sent before "bar" because that was the input
581+
if gotLine != "foo" {
582+
t.Errorf("got line: '%s', expected 'foo'", gotLine)
583+
}
584+
585+
// Get next line
586+
select {
587+
case gotLine = <-lines:
588+
default:
589+
t.Fatal("blocked on <-lines")
590+
}
591+
592+
if gotLine != "bar" {
593+
t.Errorf("got line: '%s', expected 'bar'", gotLine)
594+
}
595+
}
596+
597+
func TestStreamingMultipleLinesLastNotTerminated(t *testing.T) {
598+
// If last line isn't \n terminated, go-cmd should flush it anyway
599+
// https://github.com/go-cmd/cmd/pull/48
600+
lines := make(chan string, 5)
601+
out := cmd.NewOutputStream(lines)
602+
603+
// Quick side test: Lines() chan string should be the same chan string
604+
// we created the object with
605+
if out.Lines() != lines {
606+
t.Errorf("Lines() does not return the given string chan")
607+
}
608+
609+
// Write two short lines
610+
input := "foo\nbar" // <- last line doesn't have \n
564611
n, err := out.Write([]byte(input))
565612
if n != len(input) {
566613
t.Errorf("Write n = %d, expected %d", n, len(input))
@@ -582,7 +629,7 @@ func TestStreamingMultipleLines(t *testing.T) {
582629
t.Errorf("got line: '%s', expected 'foo'", gotLine)
583630
}
584631

585-
out.Flush()
632+
out.Flush() // flush our output so go-cmd receives it
586633

587634
// Get next line
588635
select {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/go-cmd/cmd
22

3-
go 1.13
3+
go 1.14
44

55
require github.com/go-test/deep v1.0.6

0 commit comments

Comments
 (0)