From cecfa650af55e026a4b9d3076ddd3a39a2b03a43 Mon Sep 17 00:00:00 2001 From: suntala Date: Wed, 5 Mar 2025 22:59:58 +0100 Subject: [PATCH] Add outputWriter tests --- src/testing/sub_test.go | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/testing/sub_test.go b/src/testing/sub_test.go index 576efcea1c..4b58a7c218 100644 --- a/src/testing/sub_test.go +++ b/src/testing/sub_test.go @@ -993,3 +993,61 @@ func TestNestedCleanup(t *T) { t.Errorf("unexpected cleanup count: got %d want 3", ranCleanup) } } + +func TestOutputWriter(t *T) { + tstate := newTestState(1, allMatcher()) + buf := &strings.Builder{} + root := &T{ + common: common{ + signal: make(chan bool), + barrier: make(chan bool), + name: "", + w: buf, + }, + tstate: tstate, + } + + f := func(t *T) { + t.Run("", func(t *T) { + w := outputWriter{&t.common, nil} + w.Write([]byte("a\n")) + w.Write([]byte("b\n")) + w.Write([]byte("a\nb\n")) + t.Fail() + }) + } + root.Run("check output of outputWriter", f) + tstate.release() + + if tstate.running != 0 || tstate.numWaiting != 0 { + t.Errorf("running and waiting non-zero: got %d and %d", tstate.running, tstate.numWaiting) + } + got := strings.TrimSpace(buf.String()) + output := ` +--- FAIL: check output of outputWriter (0.00s) + --- FAIL: check output of outputWriter/#00 (0.00s) + a + b + a + b + ` + want := strings.TrimSpace(output) + re := makeRegexp(want) + if ok, err := regexp.MatchString(re, got); !ok || err != nil { + t.Errorf("output:\ngot:\n%s\nwant:\n%s", got, want) + } +} + +func TestOutputWriterBuffering(t *T) { + w := outputWriter{&t.common, nil} + + w.Write([]byte("Hel")) + w.Write([]byte("lo\nWorld\nInput to log\n\n\nMore logging\nShouldn't be logged")) + w.Write([]byte("Also shouldn't be logged")) + + bufContents := string(w.b) + expected := "Shouldn't be loggedAlso shouldn't be logged" + if bufContents != expected { + t.Errorf("unexpected buffer contents: got %q want %q", bufContents, expected) + } +}