@@ -173,57 +173,49 @@ func (r *ContainerRunner) RunCmd(cmd *exec.Cmd) *Assertable {
173
173
174
174
// Assert runs the Assertable and
175
175
func (a Assertable ) Assert (t * testing.T , option ... Assertion ) {
176
- t . Run ( a . tname , func ( t * testing. T ) {
177
- var cmdResult CommandResult
176
+ slog . Helper ()
177
+ var cmdResult CommandResult
178
178
179
- var (
180
- stdout bytes.Buffer
181
- stderr bytes.Buffer
182
- )
179
+ var (
180
+ stdout bytes.Buffer
181
+ stderr bytes.Buffer
182
+ )
183
183
184
- a .cmd .Stdout = & stdout
185
- a .cmd .Stderr = & stderr
186
-
187
- start := time .Now ()
188
- err := a .cmd .Run ()
189
- cmdResult .Duration = time .Since (start )
190
-
191
- if exitErr , ok := err .(* exec.ExitError ); ok {
192
- cmdResult .ExitCode = exitErr .ExitCode ()
193
- } else if err != nil {
194
- cmdResult .ExitCode = - 1
195
- } else {
196
- cmdResult .ExitCode = 0
197
- }
198
-
199
- cmdResult .Stdout = stdout .Bytes ()
200
- cmdResult .Stderr = stderr .Bytes ()
201
-
202
- slogtest .Info (t , "command output" ,
203
- slog .F ("command" , a .cmd ),
204
- slog .F ("stdout" , string (cmdResult .Stdout )),
205
- slog .F ("stderr" , string (cmdResult .Stderr )),
206
- slog .F ("exit-code" , cmdResult .ExitCode ),
207
- slog .F ("duration" , cmdResult .Duration ),
208
- )
184
+ a .cmd .Stdout = & stdout
185
+ a .cmd .Stderr = & stderr
209
186
210
- for ix , o := range option {
211
- name := fmt .Sprintf ("assertion_#%v" , ix )
212
- if named , ok := o .(Named ); ok {
213
- name = named .Name ()
214
- }
215
- t .Run (name , func (t * testing.T ) {
216
- err := o .Valid (& cmdResult )
217
- assert .Success (t , name , err )
218
- })
219
- }
220
- })
187
+ start := time .Now ()
188
+ err := a .cmd .Run ()
189
+ cmdResult .Duration = time .Since (start )
190
+
191
+ if exitErr , ok := err .(* exec.ExitError ); ok {
192
+ cmdResult .ExitCode = exitErr .ExitCode ()
193
+ } else if err != nil {
194
+ cmdResult .ExitCode = - 1
195
+ } else {
196
+ cmdResult .ExitCode = 0
197
+ }
198
+
199
+ cmdResult .Stdout = stdout .Bytes ()
200
+ cmdResult .Stderr = stderr .Bytes ()
201
+
202
+ slogtest .Info (t , "command output" ,
203
+ slog .F ("command" , a .cmd ),
204
+ slog .F ("stdout" , string (cmdResult .Stdout )),
205
+ slog .F ("stderr" , string (cmdResult .Stderr )),
206
+ slog .F ("exit-code" , cmdResult .ExitCode ),
207
+ slog .F ("duration" , cmdResult .Duration ),
208
+ )
209
+
210
+ for _ , o := range option {
211
+ o .Valid (t , & cmdResult )
212
+ }
221
213
}
222
214
223
215
// Assertion specifies an assertion on the given CommandResult.
224
216
// Pass custom Assertion types to cover special cases.
225
217
type Assertion interface {
226
- Valid (r * CommandResult ) error
218
+ Valid (t * testing. T , r * CommandResult )
227
219
}
228
220
229
221
// Named is an optional extension of Assertion that provides a helpful label
@@ -240,12 +232,13 @@ type CommandResult struct {
240
232
}
241
233
242
234
type simpleFuncAssert struct {
243
- valid func (r * CommandResult ) error
235
+ valid func (t * testing. T , r * CommandResult )
244
236
name string
245
237
}
246
238
247
- func (s simpleFuncAssert ) Valid (r * CommandResult ) error {
248
- return s .valid (r )
239
+ func (s simpleFuncAssert ) Valid (t * testing.T , r * CommandResult ) {
240
+ slog .Helper ()
241
+ s .valid (t , r )
249
242
}
250
243
251
244
func (s simpleFuncAssert ) Name () string {
@@ -254,17 +247,16 @@ func (s simpleFuncAssert) Name() string {
254
247
255
248
// Success asserts that the command exited with an exit code of 0
256
249
func Success () Assertion {
250
+ slog .Helper ()
257
251
return ExitCodeIs (0 )
258
252
}
259
253
260
254
// Error asserts that the command exited with a nonzero exit code
261
255
func Error () Assertion {
262
256
return simpleFuncAssert {
263
- valid : func (r * CommandResult ) error {
264
- if r .ExitCode == 0 {
265
- return xerrors .Errorf ("expected nonzero exit code, got %v" , r .ExitCode )
266
- }
267
- return nil
257
+ valid : func (t * testing.T , r * CommandResult ) {
258
+ slog .Helper ()
259
+ assert .True (t , "exit code is nonzero" , r .ExitCode != 0 )
268
260
},
269
261
name : fmt .Sprintf ("error" ),
270
262
}
@@ -273,11 +265,9 @@ func Error() Assertion {
273
265
// ExitCodeIs asserts that the command exited with the given code
274
266
func ExitCodeIs (code int ) Assertion {
275
267
return simpleFuncAssert {
276
- valid : func (r * CommandResult ) error {
277
- if r .ExitCode != code {
278
- return xerrors .Errorf ("exit code of %v expected, got %v, (%s)" , code , r .ExitCode , string (r .Stderr ))
279
- }
280
- return nil
268
+ valid : func (t * testing.T , r * CommandResult ) {
269
+ slog .Helper ()
270
+ assert .Equal (t , "exit code is as expected" , code , r .ExitCode )
281
271
},
282
272
name : fmt .Sprintf ("exitcode" ),
283
273
}
@@ -286,8 +276,9 @@ func ExitCodeIs(code int) Assertion {
286
276
// StdoutEmpty asserts that the command did not write any data to Stdout
287
277
func StdoutEmpty () Assertion {
288
278
return simpleFuncAssert {
289
- valid : func (r * CommandResult ) error {
290
- return empty ("stdout" , r .Stdout )
279
+ valid : func (t * testing.T , r * CommandResult ) {
280
+ slog .Helper ()
281
+ empty (t , "stdout" , r .Stdout )
291
282
},
292
283
name : fmt .Sprintf ("stdout-empty" ),
293
284
}
@@ -297,9 +288,10 @@ func StdoutEmpty() Assertion {
297
288
// The pointer passed as "result" will be assigned to the command's *CommandResult
298
289
func GetResult (result * * CommandResult ) Assertion {
299
290
return simpleFuncAssert {
300
- valid : func (r * CommandResult ) error {
291
+ valid : func (t * testing.T , r * CommandResult ) {
292
+ slog .Helper ()
293
+ empty (t , "stdout" , r .Stdout )
301
294
* result = r
302
- return nil
303
295
},
304
296
name : "get-result" ,
305
297
}
@@ -308,8 +300,9 @@ func GetResult(result **CommandResult) Assertion {
308
300
// StderrEmpty asserts that the command did not write any data to Stderr
309
301
func StderrEmpty () Assertion {
310
302
return simpleFuncAssert {
311
- valid : func (r * CommandResult ) error {
312
- return empty ("stderr" , r .Stderr )
303
+ valid : func (t * testing.T , r * CommandResult ) {
304
+ slog .Helper ()
305
+ empty (t , "stderr" , r .Stderr )
313
306
},
314
307
name : fmt .Sprintf ("stderr-empty" ),
315
308
}
@@ -318,8 +311,9 @@ func StderrEmpty() Assertion {
318
311
// StdoutMatches asserts that Stdout contains a substring which matches the given regexp
319
312
func StdoutMatches (pattern string ) Assertion {
320
313
return simpleFuncAssert {
321
- valid : func (r * CommandResult ) error {
322
- return matches ("stdout" , pattern , r .Stdout )
314
+ valid : func (t * testing.T , r * CommandResult ) {
315
+ slog .Helper ()
316
+ matches (t , "stdout" , pattern , r .Stdout )
323
317
},
324
318
name : fmt .Sprintf ("stdout-matches" ),
325
319
}
@@ -328,8 +322,9 @@ func StdoutMatches(pattern string) Assertion {
328
322
// StderrMatches asserts that Stderr contains a substring which matches the given regexp
329
323
func StderrMatches (pattern string ) Assertion {
330
324
return simpleFuncAssert {
331
- valid : func (r * CommandResult ) error {
332
- return matches ("stderr" , pattern , r .Stderr )
325
+ valid : func (t * testing.T , r * CommandResult ) {
326
+ slog .Helper ()
327
+ matches (t , "stderr" , pattern , r .Stderr )
333
328
},
334
329
name : fmt .Sprintf ("stderr-matches" ),
335
330
}
@@ -338,45 +333,53 @@ func StderrMatches(pattern string) Assertion {
338
333
// CombinedMatches asserts that either Stdout or Stderr a substring which matches the given regexp
339
334
func CombinedMatches (pattern string ) Assertion {
340
335
return simpleFuncAssert {
341
- valid : func (r * CommandResult ) error {
342
- //stdoutValid := StdoutMatches(pattern).Valid(r)
343
- //stderrValid := StderrMatches(pattern).Valid(r)
344
- // TODO: combine errors
345
- return nil
336
+ valid : func (t * testing.T , r * CommandResult ) {
337
+ slog .Helper ()
338
+ StdoutMatches (pattern ).Valid (t , r )
339
+ StderrMatches (pattern ).Valid (t , r )
346
340
},
347
341
name : fmt .Sprintf ("combined-matches" ),
348
342
}
349
343
}
350
344
351
- func matches (name , pattern string , target []byte ) error {
345
+ func matches (t * testing.T , name , pattern string , target []byte ) {
346
+ slog .Helper ()
347
+
352
348
ok , err := regexp .Match (pattern , target )
353
349
if err != nil {
354
- return xerrors .Errorf ("failed to attempt regexp match: %w" , err )
350
+ slogtest .Fatal (t , "failed to attempt regexp match" , slog .Error (err ),
351
+ slog .F ("pattern" , pattern ),
352
+ slog .F ("target" , string (target )),
353
+ slog .F ("sink" , name ),
354
+ )
355
355
}
356
356
if ! ok {
357
- return xerrors .Errorf (
358
- "expected to find pattern (%s) in %s, no match found in (%v)" ,
359
- pattern , name , string (target ),
357
+ slogtest .Fatal (t , "expected to find pattern, no match found" ,
358
+ slog .F ("pattern" , pattern ),
359
+ slog .F ("target" , string (target )),
360
+ slog .F ("sink" , name ),
360
361
)
361
362
}
362
- return nil
363
363
}
364
364
365
- func empty (name string , a []byte ) error {
365
+ func empty (t * testing.T , name string , a []byte ) {
366
+ slog .Helper ()
366
367
if len (a ) > 0 {
367
- return xerrors . Errorf ( "expected %s to be empty, got (%s)" , name , string (a ))
368
+ slogtest . Fatal ( t , "expected " + name + " to be empty" , slog . F ( "got" , string (a ) ))
368
369
}
369
- return nil
370
370
}
371
371
372
372
// DurationLessThan asserts that the command completed in less than the given duration
373
373
func DurationLessThan (dur time.Duration ) Assertion {
374
374
return simpleFuncAssert {
375
- valid : func (r * CommandResult ) error {
375
+ valid : func (t * testing.T , r * CommandResult ) {
376
+ slog .Helper ()
376
377
if r .Duration > dur {
377
- return xerrors .Errorf ("expected duration less than %s, took %s" , dur .String (), r .Duration .String ())
378
+ slogtest .Fatal (t , "duration longer than expected" ,
379
+ slog .F ("expected_less_than" , dur .String ),
380
+ slog .F ("actual" , r .Duration .String ()),
381
+ )
378
382
}
379
- return nil
380
383
},
381
384
name : fmt .Sprintf ("duration-lessthan" ),
382
385
}
@@ -385,11 +388,14 @@ func DurationLessThan(dur time.Duration) Assertion {
385
388
// DurationGreaterThan asserts that the command completed in greater than the given duration
386
389
func DurationGreaterThan (dur time.Duration ) Assertion {
387
390
return simpleFuncAssert {
388
- valid : func (r * CommandResult ) error {
391
+ valid : func (t * testing.T , r * CommandResult ) {
392
+ slog .Helper ()
389
393
if r .Duration < dur {
390
- return xerrors .Errorf ("expected duration greater than %s, took %s" , dur .String (), r .Duration .String ())
394
+ slogtest .Fatal (t , "duration shorter than expected" ,
395
+ slog .F ("expected_greater_than" , dur .String ),
396
+ slog .F ("actual" , r .Duration .String ()),
397
+ )
391
398
}
392
- return nil
393
399
},
394
400
name : fmt .Sprintf ("duration-greaterthan" ),
395
401
}
0 commit comments