@@ -53,16 +53,21 @@ func TestPrompt(t *testing.T) {
53
53
})
54
54
55
55
t .Run ("Skip" , func (t * testing.T ) {
56
- t .Parallel ()
57
56
ptty := ptytest .New (t )
58
57
var buf bytes.Buffer
59
58
60
59
// Copy all data written out to a buffer. When we close the ptty, we can
61
60
// no longer read from the ptty.Output(), but we can read what was
62
61
// written to the buffer.
62
+ dataRead , doneReading := context .WithTimeout (context .Background (), time .Second * 2 )
63
63
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 ()
66
71
}()
67
72
68
73
doneChan := make (chan string )
@@ -79,10 +84,14 @@ func TestPrompt(t *testing.T) {
79
84
}()
80
85
81
86
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" )
83
93
require .Len (t , buf .Bytes (), 0 , "expect no output" )
84
94
})
85
-
86
95
t .Run ("JSON" , func (t * testing.T ) {
87
96
t .Parallel ()
88
97
ptty := ptytest .New (t )
@@ -104,7 +113,6 @@ func TestPrompt(t *testing.T) {
104
113
ptty := ptytest .New (t )
105
114
doneChan := make (chan string )
106
115
go func () {
107
-
108
116
resp , err := newPrompt (ptty , cliui.PromptOptions {
109
117
Text : "Example" ,
110
118
}, nil )
0 commit comments