2017-05-08 13:36:04 -04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2022-02-25 07:04:43 -05:00
|
|
|
"io"
|
2017-05-08 13:36:04 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-05-08 13:36:04 -04:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-05-08 13:36:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigRemoveErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
configRemoveFunc func(string) error
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{},
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "requires at least 1 argument.",
|
2017-05-08 13:36:04 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"foo"},
|
|
|
|
configRemoveFunc: func(name string) error {
|
|
|
|
return errors.Errorf("error removing config")
|
|
|
|
},
|
|
|
|
expectedError: "error removing config",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newConfigRemoveCommand(
|
2017-08-16 11:54:23 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2017-05-08 13:36:04 -04:00
|
|
|
configRemoveFunc: tc.configRemoveFunc,
|
2017-08-16 11:54:23 -04:00
|
|
|
}),
|
2017-05-08 13:36:04 -04:00
|
|
|
)
|
|
|
|
cmd.SetArgs(tc.args)
|
2022-02-25 07:04:43 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigRemoveWithName(t *testing.T) {
|
|
|
|
names := []string{"foo", "bar"}
|
|
|
|
var removedConfigs []string
|
2017-08-16 11:54:23 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-05-08 13:36:04 -04:00
|
|
|
configRemoveFunc: func(name string) error {
|
|
|
|
removedConfigs = append(removedConfigs, name)
|
|
|
|
return nil
|
|
|
|
},
|
2017-08-16 11:54:23 -04:00
|
|
|
})
|
2017-05-08 13:36:04 -04:00
|
|
|
cmd := newConfigRemoveCommand(cli)
|
|
|
|
cmd.SetArgs(names)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(names, strings.Split(strings.TrimSpace(cli.OutBuffer().String()), "\n")))
|
|
|
|
assert.Check(t, is.DeepEqual(names, removedConfigs))
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigRemoveContinueAfterError(t *testing.T) {
|
|
|
|
names := []string{"foo", "bar"}
|
|
|
|
var removedConfigs []string
|
|
|
|
|
2017-08-16 11:54:23 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-05-08 13:36:04 -04:00
|
|
|
configRemoveFunc: func(name string) error {
|
|
|
|
removedConfigs = append(removedConfigs, name)
|
|
|
|
if name == "foo" {
|
|
|
|
return errors.Errorf("error removing config: %s", name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2017-08-16 11:54:23 -04:00
|
|
|
})
|
2017-05-08 13:36:04 -04:00
|
|
|
|
|
|
|
cmd := newConfigRemoveCommand(cli)
|
|
|
|
cmd.SetArgs(names)
|
2022-02-25 07:04:43 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, cmd.Execute(), "error removing config: foo")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(names, removedConfigs))
|
2017-05-08 13:36:04 -04:00
|
|
|
}
|