align "conflicting options" errors for consistency

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-10-01 11:44:59 +02:00
parent 3907414549
commit bd96bdaf1b
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
8 changed files with 66 additions and 10 deletions

View File

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

View File

@ -195,6 +195,35 @@ func TestCreateContainerImagePullPolicyInvalid(t *testing.T) {
} }
} }
func TestCreateContainerValidateFlags(t *testing.T) {
for _, tc := range []struct {
name string
args []string
expectedErr string
}{
{
name: "with invalid --attach value",
args: []string{"--attach", "STDINFO", "myimage"},
expectedErr: `invalid argument "STDINFO" for "-a, --attach" flag: valid streams are STDIN, STDOUT and STDERR`,
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
cmd := NewCreateCommand(test.NewFakeCli(&fakeClient{}))
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
cmd.SetArgs(tc.args)
err := cmd.Execute()
if tc.expectedErr != "" {
assert.Check(t, is.ErrorContains(err, tc.expectedErr))
} else {
assert.Check(t, is.Nil(err))
}
})
}
}
func TestNewCreateCommandWithContentTrustErrors(t *testing.T) { func TestNewCreateCommandWithContentTrustErrors(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string

View File

@ -704,7 +704,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
} }
if copts.autoRemove && !hostConfig.RestartPolicy.IsNone() { if copts.autoRemove && !hostConfig.RestartPolicy.IsNone() {
return nil, errors.Errorf("Conflicting options: --restart and --rm") return nil, errors.Errorf("conflicting options: cannot specify both --restart and --rm")
} }
// only set this value if the user provided the flag, else it should default to nil // only set this value if the user provided the flag, else it should default to nil

View File

@ -817,11 +817,9 @@ func TestParseRestartPolicy(t *testing.T) {
} }
func TestParseRestartPolicyAutoRemove(t *testing.T) { func TestParseRestartPolicyAutoRemove(t *testing.T) {
expected := "Conflicting options: --restart and --rm"
_, _, _, err := parseRun([]string{"--rm", "--restart=always", "img", "cmd"}) //nolint:dogsled _, _, _, err := parseRun([]string{"--rm", "--restart=always", "img", "cmd"}) //nolint:dogsled
if err == nil || err.Error() != expected { const expected = "conflicting options: cannot specify both --restart and --rm"
t.Fatalf("Expected error %v, but got none", expected) assert.Check(t, is.Error(err, expected))
}
} }
func TestParseHealth(t *testing.T) { func TestParseHealth(t *testing.T) {

View File

@ -131,7 +131,7 @@ func runContainer(ctx context.Context, dockerCli command.Cli, runOpts *runOption
} }
} else { } else {
if copts.attach.Len() != 0 { if copts.attach.Len() != 0 {
return errors.New("Conflicting options: -a and -d") return errors.New("conflicting options: cannot specify both --attach and --detach")
} }
config.AttachStdin = false config.AttachStdin = false

View File

@ -23,6 +23,35 @@ import (
is "gotest.tools/v3/assert/cmp" is "gotest.tools/v3/assert/cmp"
) )
func TestRunValidateFlags(t *testing.T) {
for _, tc := range []struct {
name string
args []string
expectedErr string
}{
{
name: "with conflicting --attach, --detach",
args: []string{"--attach", "stdin", "--detach", "myimage"},
expectedErr: "conflicting options: cannot specify both --attach and --detach",
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
cmd := NewRunCommand(test.NewFakeCli(&fakeClient{}))
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
cmd.SetArgs(tc.args)
err := cmd.Execute()
if tc.expectedErr != "" {
assert.Check(t, is.ErrorContains(err, tc.expectedErr))
} else {
assert.Check(t, is.Nil(err))
}
})
}
}
func TestRunLabel(t *testing.T) { func TestRunLabel(t *testing.T) {
fakeCLI := test.NewFakeCli(&fakeClient{ fakeCLI := test.NewFakeCli(&fakeClient{
createContainerFunc: func(_ *container.Config, _ *container.HostConfig, _ *network.NetworkingConfig, _ *specs.Platform, _ string) (container.CreateResponse, error) { createContainerFunc: func(_ *container.Config, _ *container.HostConfig, _ *network.NetworkingConfig, _ *specs.Platform, _ string) (container.CreateResponse, error) {

View File

@ -52,7 +52,7 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 { if len(args) == 1 {
if options.name != "" { if options.name != "" {
return errors.Errorf("conflicting options: either specify --name or provide positional arg, not both") return errors.Errorf("conflicting options: cannot specify a volume-name through both --name and as a positional arg")
} }
options.name = args[0] options.name = args[0]
} }

View File

@ -26,7 +26,7 @@ func TestVolumeCreateErrors(t *testing.T) {
flags: map[string]string{ flags: map[string]string{
"name": "volumeName", "name": "volumeName",
}, },
expectedError: "conflicting options: either specify --name or provide positional arg, not both", expectedError: "conflicting options: cannot specify a volume-name through both --name and as a positional arg",
}, },
{ {
args: []string{"too", "many"}, args: []string{"too", "many"},