Skip to content

Commit f4fab2c

Browse files
authored
Merge pull request #5488 from thaJeztah/align_errs
align "conflicting options" errors for consistency
2 parents 3907414 + bd96bda commit f4fab2c

8 files changed

Lines changed: 66 additions & 10 deletions

File tree

cli/command/cli.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...CLIOption)
272272
debug.Enable()
273273
}
274274
if opts.Context != "" && len(opts.Hosts) > 0 {
275-
return errors.New("conflicting options: either specify --host or --context, not both")
275+
return errors.New("conflicting options: cannot specify both --host and --context")
276276
}
277277

278278
cli.options = opts
@@ -299,7 +299,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...CLIOption)
299299
// NewAPIClientFromFlags creates a new APIClient from command line flags
300300
func NewAPIClientFromFlags(opts *cliflags.ClientOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
301301
if opts.Context != "" && len(opts.Hosts) > 0 {
302-
return nil, errors.New("conflicting options: either specify --host or --context, not both")
302+
return nil, errors.New("conflicting options: cannot specify both --host and --context")
303303
}
304304

305305
storeConfig := DefaultContextStoreConfig()

cli/command/container/create_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,35 @@ func TestCreateContainerImagePullPolicyInvalid(t *testing.T) {
195195
}
196196
}
197197

198+
func TestCreateContainerValidateFlags(t *testing.T) {
199+
for _, tc := range []struct {
200+
name string
201+
args []string
202+
expectedErr string
203+
}{
204+
{
205+
name: "with invalid --attach value",
206+
args: []string{"--attach", "STDINFO", "myimage"},
207+
expectedErr: `invalid argument "STDINFO" for "-a, --attach" flag: valid streams are STDIN, STDOUT and STDERR`,
208+
},
209+
} {
210+
tc := tc
211+
t.Run(tc.name, func(t *testing.T) {
212+
cmd := NewCreateCommand(test.NewFakeCli(&fakeClient{}))
213+
cmd.SetOut(io.Discard)
214+
cmd.SetErr(io.Discard)
215+
cmd.SetArgs(tc.args)
216+
217+
err := cmd.Execute()
218+
if tc.expectedErr != "" {
219+
assert.Check(t, is.ErrorContains(err, tc.expectedErr))
220+
} else {
221+
assert.Check(t, is.Nil(err))
222+
}
223+
})
224+
}
225+
}
226+
198227
func TestNewCreateCommandWithContentTrustErrors(t *testing.T) {
199228
testCases := []struct {
200229
name string

cli/command/container/opts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
704704
}
705705

706706
if copts.autoRemove && !hostConfig.RestartPolicy.IsNone() {
707-
return nil, errors.Errorf("Conflicting options: --restart and --rm")
707+
return nil, errors.Errorf("conflicting options: cannot specify both --restart and --rm")
708708
}
709709

710710
// only set this value if the user provided the flag, else it should default to nil

cli/command/container/opts_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,11 +817,9 @@ func TestParseRestartPolicy(t *testing.T) {
817817
}
818818

819819
func TestParseRestartPolicyAutoRemove(t *testing.T) {
820-
expected := "Conflicting options: --restart and --rm"
821820
_, _, _, err := parseRun([]string{"--rm", "--restart=always", "img", "cmd"}) //nolint:dogsled
822-
if err == nil || err.Error() != expected {
823-
t.Fatalf("Expected error %v, but got none", expected)
824-
}
821+
const expected = "conflicting options: cannot specify both --restart and --rm"
822+
assert.Check(t, is.Error(err, expected))
825823
}
826824

827825
func TestParseHealth(t *testing.T) {

cli/command/container/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func runContainer(ctx context.Context, dockerCli command.Cli, runOpts *runOption
131131
}
132132
} else {
133133
if copts.attach.Len() != 0 {
134-
return errors.New("Conflicting options: -a and -d")
134+
return errors.New("conflicting options: cannot specify both --attach and --detach")
135135
}
136136

137137
config.AttachStdin = false

cli/command/container/run_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,35 @@ import (
2323
is "gotest.tools/v3/assert/cmp"
2424
)
2525

26+
func TestRunValidateFlags(t *testing.T) {
27+
for _, tc := range []struct {
28+
name string
29+
args []string
30+
expectedErr string
31+
}{
32+
{
33+
name: "with conflicting --attach, --detach",
34+
args: []string{"--attach", "stdin", "--detach", "myimage"},
35+
expectedErr: "conflicting options: cannot specify both --attach and --detach",
36+
},
37+
} {
38+
tc := tc
39+
t.Run(tc.name, func(t *testing.T) {
40+
cmd := NewRunCommand(test.NewFakeCli(&fakeClient{}))
41+
cmd.SetOut(io.Discard)
42+
cmd.SetErr(io.Discard)
43+
cmd.SetArgs(tc.args)
44+
45+
err := cmd.Execute()
46+
if tc.expectedErr != "" {
47+
assert.Check(t, is.ErrorContains(err, tc.expectedErr))
48+
} else {
49+
assert.Check(t, is.Nil(err))
50+
}
51+
})
52+
}
53+
}
54+
2655
func TestRunLabel(t *testing.T) {
2756
fakeCLI := test.NewFakeCli(&fakeClient{
2857
createContainerFunc: func(_ *container.Config, _ *container.HostConfig, _ *network.NetworkingConfig, _ *specs.Platform, _ string) (container.CreateResponse, error) {

cli/command/volume/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
5252
RunE: func(cmd *cobra.Command, args []string) error {
5353
if len(args) == 1 {
5454
if options.name != "" {
55-
return errors.Errorf("conflicting options: either specify --name or provide positional arg, not both")
55+
return errors.Errorf("conflicting options: cannot specify a volume-name through both --name and as a positional arg")
5656
}
5757
options.name = args[0]
5858
}

cli/command/volume/create_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestVolumeCreateErrors(t *testing.T) {
2626
flags: map[string]string{
2727
"name": "volumeName",
2828
},
29-
expectedError: "conflicting options: either specify --name or provide positional arg, not both",
29+
expectedError: "conflicting options: cannot specify a volume-name through both --name and as a positional arg",
3030
},
3131
{
3232
args: []string{"too", "many"},

0 commit comments

Comments
 (0)