Skip to content

Commit 8d3062d

Browse files
authored
Checking parts of the message to combat flaky tests (#1986)
1 parent 1375703 commit 8d3062d

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

pkg/output/tui/spinner_test.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package tui_test
1818

1919
import (
20+
"strings"
2021
"testing"
2122
"time"
2223

@@ -25,6 +26,9 @@ import (
2526
"knative.dev/client/pkg/output/tui"
2627
)
2728

29+
// TestSpinner describes the functionality of the Spinner widget in TUI.
30+
// This test verifies that the Spinner widget correctly updates its message
31+
// and completes when all updates have been applied.
2832
func TestSpinner(t *testing.T) {
2933
t.Parallel()
3034
ctx := context.TestContext(t)
@@ -34,24 +38,30 @@ func TestSpinner(t *testing.T) {
3438
s := w.NewSpinner("message")
3539

3640
if s == nil {
37-
t.Errorf("want spinner, got nil")
41+
t.Fatal("want spinner, got nil")
3842
}
3943
if err := s.With(func(sc tui.SpinnerControl) error {
40-
time.Sleep(3 * time.Millisecond)
44+
time.Sleep(5 * time.Millisecond)
4145
sc.UpdateMessage("msg-1")
42-
time.Sleep(3 * time.Millisecond)
46+
time.Sleep(5 * time.Millisecond)
4347
sc.UpdateMessage("msg-2")
44-
time.Sleep(3 * time.Millisecond)
48+
time.Sleep(5 * time.Millisecond)
4549
return nil
4650
}); err != nil {
4751
t.Errorf("want nil, got %v", err)
4852
}
4953
got := prt.Outputs().Out.String()
50-
want := "\x1b[?25lmessage ▰▱▱\x1b[0D" +
51-
"\x1b[0D\x1b[2Kmsg-1 ▰▰▱\x1b[0D" +
52-
"\x1b[0D\x1b[2Kmsg-2 ▰▰▰\x1b[0D" +
53-
"\x1b[2K\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006lmsg-2 Done\n"
54-
if got != want {
55-
t.Errorf("text missmatch\nwant %q,\n got %q", want, got)
54+
expectedMsgs := []string{
55+
"message", "msg-1", "msg-2",
56+
"▰▱▱", "▰▰▱", "▰▰▰",
57+
"Done",
58+
"\u001B[?25l", "\u001B[0D",
59+
"\u001B[0D\u001B[2K",
60+
}
61+
for _, expected := range expectedMsgs {
62+
if !strings.Contains(got, expected) {
63+
t.Errorf("Expected to contain %#v within:\n%#v",
64+
expected, got)
65+
}
5666
}
5767
}

0 commit comments

Comments
 (0)