Skip to content

Commit 8b5e553

Browse files
committed
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 <github@gone.nl>
1 parent 2e26600 commit 8b5e553

1 file changed

Lines changed: 29 additions & 11 deletions

File tree

cli/command/volume/create_test.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestVolumeCreateErrors(t *testing.T) {
5757
}
5858

5959
func TestVolumeCreateWithName(t *testing.T) {
60-
name := "foo"
60+
const name = "my-volume-name"
6161
cli := test.NewFakeCli(&fakeClient{
6262
volumeCreateFunc: func(body volume.CreateOptions) (volume.Volume, error) {
6363
if body.Name != name {
@@ -70,19 +70,37 @@ func TestVolumeCreateWithName(t *testing.T) {
7070
})
7171

7272
buf := cli.OutBuffer()
73+
t.Run("using-flags", func(t *testing.T) {
74+
cmd := newCreateCommand(cli)
75+
cmd.SetOut(io.Discard)
76+
cmd.SetErr(io.Discard)
77+
cmd.SetArgs([]string{})
78+
assert.Check(t, cmd.Flags().Set("name", name))
79+
assert.NilError(t, cmd.Execute())
80+
assert.Check(t, is.Equal(strings.TrimSpace(buf.String()), name))
81+
})
7382

74-
// Test by flags
75-
cmd := newCreateCommand(cli)
76-
cmd.Flags().Set("name", name)
77-
assert.NilError(t, cmd.Execute())
78-
assert.Check(t, is.Equal(name, strings.TrimSpace(buf.String())))
83+
buf.Reset()
84+
t.Run("using-args", func(t *testing.T) {
85+
cmd := newCreateCommand(cli)
86+
cmd.SetOut(io.Discard)
87+
cmd.SetErr(io.Discard)
88+
cmd.SetArgs([]string{name})
89+
assert.NilError(t, cmd.Execute())
90+
assert.Check(t, is.Equal(strings.TrimSpace(buf.String()), name))
91+
})
7992

80-
// Then by args
8193
buf.Reset()
82-
cmd = newCreateCommand(cli)
83-
cmd.SetArgs([]string{name})
84-
assert.NilError(t, cmd.Execute())
85-
assert.Check(t, is.Equal(name, strings.TrimSpace(buf.String())))
94+
t.Run("using-both", func(t *testing.T) {
95+
cmd := newCreateCommand(cli)
96+
cmd.SetOut(io.Discard)
97+
cmd.SetErr(io.Discard)
98+
cmd.SetArgs([]string{name})
99+
assert.Check(t, cmd.Flags().Set("name", name))
100+
err := cmd.Execute()
101+
assert.Check(t, is.Error(err, `conflicting options: cannot specify a volume-name through both --name and as a positional arg`))
102+
assert.Check(t, is.Equal(strings.TrimSpace(buf.String()), ""))
103+
})
86104
}
87105

88106
func TestVolumeCreateWithFlags(t *testing.T) {

0 commit comments

Comments
 (0)