Skip to content

Commit 73ff81b

Browse files
committed
cli/command/system: TestEventsFormat: set cmd.Args to prevent test-failures
When running the tests with options set, such as `-update` for updating "golden" files, this test would pick up test arguments because no arguments were set to invoke the command; go test . -update Error: unknown shorthand flag: 'u' in -update Usage: events [OPTIONS] [flags] Flags: -f, --filter filter Filter output based on conditions provided --format string Format output using a custom template: 'json': Print in JSON format 'TEMPLATE': Print output using the given Go template. Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates -h, --help help for events --since string Show all events created since timestamp --until string Stream events until this timestamp --- FAIL: TestEventsFormat (0.00s) --- FAIL: TestEventsFormat/default (0.00s) events_test.go:75: assertion failed: error is not nil: unknown shorthand flag: 'u' in -update --- FAIL: TestEventsFormat/json (0.00s) events_test.go:75: assertion failed: error is not nil: unknown shorthand flag: 'u' in -update --- FAIL: TestEventsFormat/json_template (0.00s) events_test.go:75: assertion failed: error is not nil: unknown shorthand flag: 'u' in -update --- FAIL: TestEventsFormat/json_action (0.00s) events_test.go:75: assertion failed: error is not nil: unknown shorthand flag: 'u' in -update This patch: - changes the test to use command-arguments instead of manually setting the flag options; this also adds test-coverage for parsing actual command arguments. - discards stdout/stderr of the command to prevent noise in test output Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 91d097e commit 73ff81b

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

cli/command/system/events_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,24 @@ func TestEventsFormat(t *testing.T) {
3333
})
3434
}
3535
tests := []struct {
36-
name, format string
36+
name string
37+
args []string
3738
}{
3839
{
3940
name: "default",
41+
args: []string{},
4042
},
4143
{
42-
name: "json",
43-
format: "json",
44+
name: "json",
45+
args: []string{"--format", "json"},
4446
},
4547
{
46-
name: "json template",
47-
format: "{{ json . }}",
48+
name: "json template",
49+
args: []string{"--format", "{{ json . }}"},
4850
},
4951
{
50-
name: "json action",
51-
format: "{{ json .Action }}",
52+
name: "json action",
53+
args: []string{"--format", "{{ json .Action }}"},
5254
},
5355
}
5456

@@ -69,9 +71,9 @@ func TestEventsFormat(t *testing.T) {
6971
return messages, errs
7072
}})
7173
cmd := NewEventsCommand(cli)
72-
if tc.format != "" {
73-
cmd.Flags().Set("format", tc.format)
74-
}
74+
cmd.SetArgs(tc.args)
75+
cmd.SetOut(io.Discard)
76+
cmd.SetErr(io.Discard)
7577
assert.Check(t, cmd.Execute())
7678
out := cli.OutBuffer().String()
7779
assert.Check(t, golden.String(out, fmt.Sprintf("docker-events-%s.golden", strings.ReplaceAll(tc.name, " ", "-"))))

0 commit comments

Comments
 (0)