77 "strings"
88 "sync/atomic"
99 "testing"
10+
11+ "github.com/stretchr/testify/assert"
12+ "github.com/stretchr/testify/require"
1013)
1114
1215// readCloseSpy records whether Close was called.
@@ -53,23 +56,15 @@ func TestGoStageHonorsCloseFlags(t *testing.T) {
5356 return err
5457 })
5558
56- if err := s .Start (
59+ require . NoError ( t , s .Start (
5760 context .Background (), StageOptions {},
5861 in , ! tc .leaveIn ,
5962 out , ! tc .leaveOut ,
60- ); err != nil {
61- t .Fatalf ("Start: %v" , err )
62- }
63- if err := s .Wait (); err != nil {
64- t .Fatalf ("Wait: %v" , err )
65- }
66-
67- if got , want := in .closed .Load (), ! tc .leaveIn ; got != want {
68- t .Errorf ("stdin closed = %v, want %v (closeStdin=%v)" , got , want , ! tc .leaveIn )
69- }
70- if got , want := out .closed .Load (), ! tc .leaveOut ; got != want {
71- t .Errorf ("stdout closed = %v, want %v (closeStdout=%v)" , got , want , ! tc .leaveOut )
72- }
63+ ))
64+ require .NoError (t , s .Wait ())
65+
66+ assert .Equal (t , ! tc .leaveIn , in .closed .Load (), "closeStdin=%v" , ! tc .leaveIn )
67+ assert .Equal (t , ! tc .leaveOut , out .closed .Load (), "closeStdout=%v" , ! tc .leaveOut )
7368 })
7469 }
7570}
@@ -79,21 +74,13 @@ func TestStagePanicsWhenOwnedStreamIsNotCloseable(t *testing.T) {
7974 return nil
8075 })
8176
82- defer func () {
83- r := recover ()
84- if r == nil {
85- t .Fatal ("expected Start to panic" )
86- }
87- if ! strings .Contains (r .(string ), "does not implement io.Closer" ) {
88- t .Fatalf ("unexpected panic: %v" , r )
89- }
90- }()
91-
92- _ = s .Start (
93- context .Background (), StageOptions {},
94- strings .NewReader ("not closeable" ), true ,
95- nil , false ,
96- )
77+ assert .PanicsWithValue (t , "stage asked to close *strings.Reader, which does not implement io.Closer" , func () {
78+ _ = s .Start (
79+ context .Background (), StageOptions {},
80+ strings .NewReader ("not closeable" ), true ,
81+ nil , false ,
82+ )
83+ })
9784}
9885
9986// TestCommandStageHonorsCloseStdin verifies that a command stage closes a
@@ -111,20 +98,14 @@ func TestCommandStageHonorsCloseStdin(t *testing.T) {
11198 cmd := exec .Command ("true" )
11299 s := CommandStage ("true" , cmd ).(* commandStage )
113100
114- if err := s .Start (
101+ require . NoError ( t , s .Start (
115102 context .Background (), StageOptions {},
116103 in , ! leave ,
117104 nil , false ,
118- ); err != nil {
119- t .Fatalf ("Start: %v" , err )
120- }
121- if err := s .Wait (); err != nil {
122- t .Fatalf ("Wait: %v" , err )
123- }
124-
125- if got , want := in .closed .Load (), ! leave ; got != want {
126- t .Errorf ("stdin closed = %v, want %v (closeStdin=%v)" , got , want , ! leave )
127- }
105+ ))
106+ require .NoError (t , s .Wait ())
107+
108+ assert .Equal (t , ! leave , in .closed .Load (), "closeStdin=%v" , ! leave )
128109 })
129110 }
130111}
@@ -144,20 +125,14 @@ func TestCommandStageHonorsCloseStdout(t *testing.T) {
144125 cmd := exec .Command ("true" )
145126 s := CommandStage ("true" , cmd ).(* commandStage )
146127
147- if err := s .Start (
128+ require . NoError ( t , s .Start (
148129 context .Background (), StageOptions {},
149130 nil , false ,
150131 out , ! leave ,
151- ); err != nil {
152- t .Fatalf ("Start: %v" , err )
153- }
154- if err := s .Wait (); err != nil {
155- t .Fatalf ("Wait: %v" , err )
156- }
157-
158- if got , want := out .closed .Load (), ! leave ; got != want {
159- t .Errorf ("stdout closed = %v, want %v (closeStdout=%v)" , got , want , ! leave )
160- }
132+ ))
133+ require .NoError (t , s .Wait ())
134+
135+ assert .Equal (t , ! leave , out .closed .Load (), "closeStdout=%v" , ! leave )
161136 })
162137 }
163138}
0 commit comments