@@ -284,3 +284,127 @@ func TestMain(m *testing.M) {
284
284
closer .Close ()
285
285
os .Exit (m .Run ())
286
286
}
287
+
288
+ func TestConsoleOutput (t * testing.T ) {
289
+ runner := ContainerDiffRunner {
290
+ t : t ,
291
+ binaryPath : "../out/container-diff" ,
292
+ }
293
+
294
+ tests := []struct {
295
+ description string
296
+ subCommand string
297
+ extraFlag string
298
+ expectedOutput []string
299
+ producesError bool
300
+ }{
301
+ {
302
+ description : "analysis --help" ,
303
+ subCommand : "analyze" ,
304
+ extraFlag : "--help" ,
305
+ expectedOutput : []string {
306
+ "Analyzes an image using the specifed analyzers as indicated via --type flag(s)." ,
307
+ "For details on how to specify images, run: container-diff help" ,
308
+ "container-diff" ,
309
+ "-c, --cache-dir string" ,
310
+ "-j, --json" ,
311
+ "-w, --output string" ,
312
+ "-t, --type multiValueFlag" ,
313
+ },
314
+ },
315
+ {
316
+ description : "analysis help" ,
317
+ subCommand : "analyze" ,
318
+ extraFlag : "help" ,
319
+ expectedOutput : []string {
320
+ "Analyzes an image using the specifed analyzers as indicated via --type flag(s)." ,
321
+ "For details on how to specify images, run: container-diff help" ,
322
+ "container-diff" ,
323
+ "-c, --cache-dir string" ,
324
+ "-j, --json" ,
325
+ "-w, --output string" ,
326
+ "-t, --type multiValueFlag" ,
327
+ },
328
+ },
329
+ {
330
+ description : "container-diff --help" ,
331
+ subCommand : "--help" ,
332
+ extraFlag : "" ,
333
+ expectedOutput : []string {
334
+ "container-diff is a CLI tool for analyzing and comparing container images." ,
335
+ "Images can be specified from either a local Docker daemon, or from a remote registry." ,
336
+ "analyze" ,
337
+ "diff" ,
338
+ "--format string" ,
339
+ "--skip-tls-verify-registry multiValueFlag" ,
340
+ "-v, --verbosity string" ,
341
+ },
342
+ },
343
+ {
344
+ description : "container-diff help" ,
345
+ subCommand : "help" ,
346
+ extraFlag : "" ,
347
+ expectedOutput : []string {
348
+ "container-diff is a CLI tool for analyzing and comparing container images." ,
349
+ "Images can be specified from either a local Docker daemon, or from a remote registry." ,
350
+ "analyze" ,
351
+ "diff" ,
352
+ "--format string" ,
353
+ "--skip-tls-verify-registry multiValueFlag" ,
354
+ "-v, --verbosity string" ,
355
+ },
356
+ },
357
+ {
358
+ description : "container-diff diff --help" ,
359
+ subCommand : "diff" ,
360
+ extraFlag : "--help" ,
361
+ expectedOutput : []string {
362
+ "Compares two images using the specifed analyzers as indicated via --type flag(s)." ,
363
+ "For details on how to specify images, run: container-diff help" ,
364
+ "container-diff diff image1 image2 [flags]" ,
365
+ "-c, --cache-dir string" ,
366
+ "-j, --json" ,
367
+ "-w, --output string" ,
368
+ "--skip-tls-verify-registry multiValueFlag" ,
369
+ },
370
+ },
371
+ {
372
+ description : "container-diff diff --help" ,
373
+ subCommand : "diff" ,
374
+ extraFlag : "help" ,
375
+ expectedOutput : []string {
376
+ "Error: 'diff' requires two images as arguments: container-diff diff [image1] [image2]" ,
377
+ "container-diff diff image1 image2 [flags]" ,
378
+ "-c, --cache-dir string" ,
379
+ "-j, --json" ,
380
+ "-w, --output string" ,
381
+ "--skip-tls-verify-registry multiValueFlag" ,
382
+ },
383
+ producesError : true ,
384
+ },
385
+ }
386
+ for _ , test := range tests {
387
+ t .Run (test .description , func (t * testing.T ) {
388
+ t .Parallel ()
389
+ args := []string {test .subCommand }
390
+ if test .extraFlag != "" {
391
+ args = append (args , test .extraFlag )
392
+ }
393
+ actual , stderr , err := runner .Run (args ... )
394
+ if err != nil {
395
+ if test .producesError {
396
+ actual = err .Error ()
397
+ } else {
398
+ t .Fatalf ("Error running command: %s. Stderr: %s" , err , stderr )
399
+ }
400
+ }
401
+ actual = strings .TrimSpace (actual )
402
+ for _ , expectedLine := range test .expectedOutput {
403
+ if ! strings .Contains (actual , expectedLine ) {
404
+ t .Errorf ("Error actual output does not contain expected line. \n \n Expected: %s\n \n Actual: %s\n \n , Stderr: %s" , expectedLine , actual , stderr )
405
+ }
406
+ }
407
+
408
+ })
409
+ }
410
+ }
0 commit comments