Skip to content

Commit 3b6cea6

Browse files
committed
Fix test flakes
1 parent 373cb23 commit 3b6cea6

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

cli/cliui/prompt_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,21 @@ func TestPrompt(t *testing.T) {
5353
})
5454

5555
t.Run("Skip", func(t *testing.T) {
56-
t.Parallel()
5756
ptty := ptytest.New(t)
5857
var buf bytes.Buffer
5958

6059
// Copy all data written out to a buffer. When we close the ptty, we can
6160
// no longer read from the ptty.Output(), but we can read what was
6261
// written to the buffer.
62+
dataRead, doneReading := context.WithTimeout(context.Background(), time.Second*2)
6363
go func() {
64-
_, err := io.Copy(&buf, ptty.Output())
65-
require.NoError(t, err, "copy")
64+
// This will throw an error sometimes. The underlying ptty
65+
// has its own cleanup routines in t.Cleanup. Instead of
66+
// trying to control the close perfectly, just let the ptty
67+
// double close. This error isn't important, we just
68+
// want to know the ptty is done sending output.
69+
_, _ = io.Copy(&buf, ptty.Output())
70+
doneReading()
6671
}()
6772

6873
doneChan := make(chan string)
@@ -79,10 +84,14 @@ func TestPrompt(t *testing.T) {
7984
}()
8085

8186
require.Equal(t, "yes", <-doneChan)
82-
require.NoError(t, ptty.Close(), "close ptty")
87+
// Close the reader to end the io.Copy
88+
require.NoError(t, ptty.Close(), "close eof reader")
89+
// Wait for the IO copy to finish
90+
<-dataRead.Done()
91+
// Timeout error means the output was hanging
92+
require.ErrorIs(t, dataRead.Err(), context.Canceled, "should be cancelled")
8393
require.Len(t, buf.Bytes(), 0, "expect no output")
8494
})
85-
8695
t.Run("JSON", func(t *testing.T) {
8796
t.Parallel()
8897
ptty := ptytest.New(t)
@@ -104,7 +113,6 @@ func TestPrompt(t *testing.T) {
104113
ptty := ptytest.New(t)
105114
doneChan := make(chan string)
106115
go func() {
107-
108116
resp, err := newPrompt(ptty, cliui.PromptOptions{
109117
Text: "Example",
110118
}, nil)

0 commit comments

Comments
 (0)