Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit 53d1c0e

Browse files
author
Luis Plazas
committed
Add unit tests for CLI help commands.
1 parent 2357b87 commit 53d1c0e

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

tests/integration_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,127 @@ func TestMain(m *testing.M) {
284284
closer.Close()
285285
os.Exit(m.Run())
286286
}
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 analyze image [flags]",
309+
"-c, --cache-dir string cache directory base to create .container-diff (default is $HOME).",
310+
"-j, --json JSON Output defines if the diff should be returned in a human readable format (false) or a JSON (true).",
311+
"-w, --output string output file to write to (default writes to the screen).",
312+
"-t, --type multiValueFlag This flag sets the list of analyzer types to use.",
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 analyze image [flags]",
323+
"-c, --cache-dir string cache directory base to create .container-diff (default is $HOME).",
324+
"-j, --json JSON Output defines if the diff should be returned in a human readable format (false) or a JSON (true).",
325+
"-w, --output string output file to write to (default writes to the screen).",
326+
"-t, --type multiValueFlag This flag sets the list of analyzer types to use.",
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 Analyzes an image: container-diff image",
337+
"diff Compare two images: container-diff image1 image2",
338+
"--format string Format to output diff in.",
339+
"--skip-tls-verify-registry multiValueFlag Insecure registry ignoring TLS verify to push and pull. Set it repeatedly for multiple registries.",
340+
"-v, --verbosity string This flag controls the verbosity of container-diff. (default \"warning\")",
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 Analyzes an image: container-diff image",
351+
"diff Compare two images: container-diff image1 image2",
352+
"--format string Format to output diff in.",
353+
"--skip-tls-verify-registry multiValueFlag Insecure registry ignoring TLS verify to push and pull. Set it repeatedly for multiple registries.",
354+
"-v, --verbosity string This flag controls the verbosity of container-diff. (default \"warning\")",
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 cache directory base to create .container-diff (default is $HOME).",
366+
"-j, --json JSON Output defines if the diff should be returned in a human readable format (false) or a JSON (true).",
367+
"-w, --output string output file to write to (default writes to the screen).",
368+
"--skip-tls-verify-registry multiValueFlag Insecure registry ignoring TLS verify to push and pull. Set it repeatedly for multiple registries.",
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 cache directory base to create .container-diff (default is $HOME).",
379+
"-j, --json JSON Output defines if the diff should be returned in a human readable format (false) or a JSON (true).",
380+
"-w, --output string output file to write to (default writes to the screen).",
381+
"--skip-tls-verify-registry multiValueFlag Insecure registry ignoring TLS verify to push and pull. Set it repeatedly for multiple registries.",
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+
t.Fatalf("Error running command: %s. Stderr: %s", err, stderr)
397+
} else {
398+
actual = err.Error()
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\nExpected: %s\n\n Actual: %s\n\n, Stderr: %s", expectedLine, actual, stderr)
405+
}
406+
}
407+
408+
})
409+
}
410+
}

0 commit comments

Comments
 (0)