From 8b5e5539e1619706c65b733fae8e03f31f5d9a8b Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 1 Feb 2025 14:00:59 +0100 Subject: [PATCH] cli/command/volume: TestVolumeCreateWithName: minor fixes and improvements - assert unhandled error - use sub-tests - add test-case for conflicting options (both flag and name) - reset command-args to prevent test failing when running from pre-compiled test-binary - use a const and a slightly more unique name for the volume-name - discard stdout/stderr output Signed-off-by: Sebastiaan van Stijn --- cli/command/volume/create_test.go | 42 ++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/cli/command/volume/create_test.go b/cli/command/volume/create_test.go index de879e81e2..2ffe222a9c 100644 --- a/cli/command/volume/create_test.go +++ b/cli/command/volume/create_test.go @@ -57,7 +57,7 @@ func TestVolumeCreateErrors(t *testing.T) { } func TestVolumeCreateWithName(t *testing.T) { - name := "foo" + const name = "my-volume-name" cli := test.NewFakeCli(&fakeClient{ volumeCreateFunc: func(body volume.CreateOptions) (volume.Volume, error) { if body.Name != name { @@ -70,19 +70,37 @@ func TestVolumeCreateWithName(t *testing.T) { }) buf := cli.OutBuffer() + t.Run("using-flags", func(t *testing.T) { + cmd := newCreateCommand(cli) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + cmd.SetArgs([]string{}) + assert.Check(t, cmd.Flags().Set("name", name)) + assert.NilError(t, cmd.Execute()) + assert.Check(t, is.Equal(strings.TrimSpace(buf.String()), name)) + }) - // Test by flags - cmd := newCreateCommand(cli) - cmd.Flags().Set("name", name) - assert.NilError(t, cmd.Execute()) - assert.Check(t, is.Equal(name, strings.TrimSpace(buf.String()))) - - // Then by args buf.Reset() - cmd = newCreateCommand(cli) - cmd.SetArgs([]string{name}) - assert.NilError(t, cmd.Execute()) - assert.Check(t, is.Equal(name, strings.TrimSpace(buf.String()))) + t.Run("using-args", func(t *testing.T) { + cmd := newCreateCommand(cli) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + cmd.SetArgs([]string{name}) + assert.NilError(t, cmd.Execute()) + assert.Check(t, is.Equal(strings.TrimSpace(buf.String()), name)) + }) + + buf.Reset() + t.Run("using-both", func(t *testing.T) { + cmd := newCreateCommand(cli) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + cmd.SetArgs([]string{name}) + assert.Check(t, cmd.Flags().Set("name", name)) + err := cmd.Execute() + assert.Check(t, is.Error(err, `conflicting options: cannot specify a volume-name through both --name and as a positional arg`)) + assert.Check(t, is.Equal(strings.TrimSpace(buf.String()), "")) + }) } func TestVolumeCreateWithFlags(t *testing.T) {