From 3b38dc67be367589c1d58000413d8f66d4670c1c Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 13 Oct 2024 18:45:20 +0200 Subject: [PATCH] cli/command/container: set empty args in tests and discard output Prevent some tests from failing when running from a pre-compiled testbinary, and discard output to make the output less noisy. Signed-off-by: Sebastiaan van Stijn --- cli/command/container/list_test.go | 27 +++++++++++++++++++++++++-- cli/command/container/prune_test.go | 1 + 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/cli/command/container/list_test.go b/cli/command/container/list_test.go index 6d72fbcf7e..d6f7d4f11f 100644 --- a/cli/command/container/list_test.go +++ b/cli/command/container/list_test.go @@ -127,7 +127,6 @@ func TestContainerListBuildContainerListOptions(t *testing.T) { func TestContainerListErrors(t *testing.T) { testCases := []struct { - args []string flags map[string]string containerListFunc func(container.ListOptions) ([]container.Summary, error) expectedError string @@ -157,10 +156,10 @@ func TestContainerListErrors(t *testing.T) { containerListFunc: tc.containerListFunc, }), ) - cmd.SetArgs(tc.args) for key, value := range tc.flags { assert.Check(t, cmd.Flags().Set(key, value)) } + cmd.SetArgs([]string{}) cmd.SetOut(io.Discard) cmd.SetErr(io.Discard) assert.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -180,6 +179,9 @@ func TestContainerListWithoutFormat(t *testing.T) { }, }) cmd := newListCommand(cli) + cmd.SetArgs([]string{}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) assert.NilError(t, cmd.Execute()) golden.Assert(t, cli.OutBuffer().String(), "container-list-without-format.golden") } @@ -194,6 +196,9 @@ func TestContainerListNoTrunc(t *testing.T) { }, }) cmd := newListCommand(cli) + cmd.SetArgs([]string{}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) assert.Check(t, cmd.Flags().Set("no-trunc", "true")) assert.NilError(t, cmd.Execute()) golden.Assert(t, cli.OutBuffer().String(), "container-list-without-format-no-trunc.golden") @@ -210,6 +215,9 @@ func TestContainerListNamesMultipleTime(t *testing.T) { }, }) cmd := newListCommand(cli) + cmd.SetArgs([]string{}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) assert.Check(t, cmd.Flags().Set("format", "{{.Names}} {{.Names}}")) assert.NilError(t, cmd.Execute()) golden.Assert(t, cli.OutBuffer().String(), "container-list-format-name-name.golden") @@ -226,6 +234,9 @@ func TestContainerListFormatTemplateWithArg(t *testing.T) { }, }) cmd := newListCommand(cli) + cmd.SetArgs([]string{}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) assert.Check(t, cmd.Flags().Set("format", `{{.Names}} {{.Label "some.label"}}`)) assert.NilError(t, cmd.Execute()) golden.Assert(t, cli.OutBuffer().String(), "container-list-format-with-arg.golden") @@ -275,6 +286,9 @@ func TestContainerListFormatSizeSetsOption(t *testing.T) { }, }) cmd := newListCommand(cli) + cmd.SetArgs([]string{}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) assert.Check(t, cmd.Flags().Set("format", tc.format)) if tc.sizeFlag != "" { assert.Check(t, cmd.Flags().Set("size", tc.sizeFlag)) @@ -297,6 +311,9 @@ func TestContainerListWithConfigFormat(t *testing.T) { PsFormat: "{{ .Names }} {{ .Image }} {{ .Labels }} {{ .Size}}", }) cmd := newListCommand(cli) + cmd.SetArgs([]string{}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) assert.NilError(t, cmd.Execute()) golden.Assert(t, cli.OutBuffer().String(), "container-list-with-config-format.golden") } @@ -314,6 +331,9 @@ func TestContainerListWithFormat(t *testing.T) { t.Run("with format", func(t *testing.T) { cli.OutBuffer().Reset() cmd := newListCommand(cli) + cmd.SetArgs([]string{}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) assert.Check(t, cmd.Flags().Set("format", "{{ .Names }} {{ .Image }} {{ .Labels }}")) assert.NilError(t, cmd.Execute()) golden.Assert(t, cli.OutBuffer().String(), "container-list-with-format.golden") @@ -322,6 +342,9 @@ func TestContainerListWithFormat(t *testing.T) { t.Run("with format and quiet", func(t *testing.T) { cli.OutBuffer().Reset() cmd := newListCommand(cli) + cmd.SetArgs([]string{}) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) assert.Check(t, cmd.Flags().Set("format", "{{ .Names }} {{ .Image }} {{ .Labels }}")) assert.Check(t, cmd.Flags().Set("quiet", "true")) assert.NilError(t, cmd.Execute()) diff --git a/cli/command/container/prune_test.go b/cli/command/container/prune_test.go index bf2e793d07..1ded0f2882 100644 --- a/cli/command/container/prune_test.go +++ b/cli/command/container/prune_test.go @@ -21,6 +21,7 @@ func TestContainerPrunePromptTermination(t *testing.T) { }, }) cmd := NewPruneCommand(cli) + cmd.SetArgs([]string{}) cmd.SetOut(io.Discard) cmd.SetErr(io.Discard) test.TerminatePrompt(ctx, t, cmd, cli)