@@ -122,12 +122,14 @@ func TestCommand(t *testing.T) {
122
122
}
123
123
124
124
func TestCommandContext (t * testing.T ) {
125
+ testError := errors .New ("test-error: the command was canceled" )
126
+
125
127
tests := []struct {
126
128
doc string
127
129
cmdAndArgs []string
128
130
cancel bool
129
131
expOut string
130
- expError bool
132
+ expError error
131
133
}{
132
134
{
133
135
doc : "basename" ,
@@ -148,13 +150,13 @@ func TestCommandContext(t *testing.T) {
148
150
doc : "context canceled" ,
149
151
cancel : true ,
150
152
cmdAndArgs : []string {testReExec2 },
151
- expError : true ,
153
+ expError : context . Canceled ,
152
154
},
153
155
{
154
156
doc : "context timeout" ,
155
157
cmdAndArgs : []string {testReExec3 },
156
158
expOut : "Hello test-reexec3" ,
157
- expError : true ,
159
+ expError : testError ,
158
160
},
159
161
}
160
162
@@ -167,6 +169,9 @@ func TestCommandContext(t *testing.T) {
167
169
if ! reflect .DeepEqual (cmd .Args , tc .cmdAndArgs ) {
168
170
t .Fatalf ("got %+v, want %+v" , cmd .Args , tc .cmdAndArgs )
169
171
}
172
+ cmd .Cancel = func () error {
173
+ return testError
174
+ }
170
175
171
176
w , err := cmd .StdinPipe ()
172
177
if err != nil {
@@ -177,17 +182,8 @@ func TestCommandContext(t *testing.T) {
177
182
cancel ()
178
183
}
179
184
out , err := cmd .CombinedOutput ()
180
- if tc .cancel {
181
- if ! errors .Is (err , context .Canceled ) {
182
- t .Errorf ("got %[1]v (%[1]T), want %v" , err , context .Canceled )
183
- }
184
- }
185
- if tc .expError {
186
- if err == nil {
187
- t .Errorf ("expected error, got nil" )
188
- }
189
- } else if err != nil {
190
- t .Errorf ("error on re-exec cmd: %v, out: %v" , err , string (out ))
185
+ if ! errors .Is (err , tc .expError ) {
186
+ t .Errorf ("expected %v, got: %v" , tc .expError , err )
191
187
}
192
188
193
189
actual := strings .TrimSpace (string (out ))
0 commit comments