@@ -122,12 +122,14 @@ func TestCommand(t *testing.T) {
122122}
123123
124124func TestCommandContext (t * testing.T ) {
125+ testError := errors .New ("test-error: the command was canceled" )
126+
125127 tests := []struct {
126128 doc string
127129 cmdAndArgs []string
128130 cancel bool
129131 expOut string
130- expError bool
132+ expError error
131133 }{
132134 {
133135 doc : "basename" ,
@@ -148,13 +150,13 @@ func TestCommandContext(t *testing.T) {
148150 doc : "context canceled" ,
149151 cancel : true ,
150152 cmdAndArgs : []string {testReExec2 },
151- expError : true ,
153+ expError : context . Canceled ,
152154 },
153155 {
154156 doc : "context timeout" ,
155157 cmdAndArgs : []string {testReExec3 },
156158 expOut : "Hello test-reexec3" ,
157- expError : true ,
159+ expError : testError ,
158160 },
159161 }
160162
@@ -167,6 +169,9 @@ func TestCommandContext(t *testing.T) {
167169 if ! reflect .DeepEqual (cmd .Args , tc .cmdAndArgs ) {
168170 t .Fatalf ("got %+v, want %+v" , cmd .Args , tc .cmdAndArgs )
169171 }
172+ cmd .Cancel = func () error {
173+ return testError
174+ }
170175
171176 w , err := cmd .StdinPipe ()
172177 if err != nil {
@@ -177,17 +182,8 @@ func TestCommandContext(t *testing.T) {
177182 cancel ()
178183 }
179184 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 )
191187 }
192188
193189 actual := strings .TrimSpace (string (out ))
0 commit comments