The validation functions to test for the number of passed arguments did not
pluralize `argument(s)`, and used `argument(s)` in all cases.
This patch adds a simple `pluralize()` helper to improve this.
Before this change, `argument(s)` was used in all cases:
$ docker container ls foobar
"docker container ls" accepts no argument(s).
$ docker network create one two
"docker network create" requires exactly 1 argument(s).
$ docker network connect
"docker network connect" requires exactly 2 argument(s).
$ docker volume create one two
"docker volume create" requires at most 1 argument(s).
After this change, `argument(s)` is properly singularized or plurarized:
$ docker container ls foobar
"docker container ls" accepts no arguments.
$ docker network create one two
"docker network create" requires exactly 1 argument.
$ docker network connect
"docker network connect" requires exactly 2 arguments.
$ docker volume create one two
"docker volume create" requires at most 1 argument.
Test cases were updated accordingly.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
106 lines
2.9 KiB
Go
106 lines
2.9 KiB
Go
package image
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/docker/cli/cli/internal/test"
|
|
"github.com/docker/docker/pkg/testutil"
|
|
"github.com/pkg/errors"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestNewSaveCommandErrors(t *testing.T) {
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
isTerminal bool
|
|
expectedError string
|
|
imageSaveFunc func(images []string) (io.ReadCloser, error)
|
|
}{
|
|
{
|
|
name: "wrong args",
|
|
args: []string{},
|
|
expectedError: "requires at least 1 argument.",
|
|
},
|
|
{
|
|
name: "output to terminal",
|
|
args: []string{"output", "file", "arg1"},
|
|
isTerminal: true,
|
|
expectedError: "cowardly refusing to save to a terminal. Use the -o flag or redirect",
|
|
},
|
|
{
|
|
name: "ImageSave fail",
|
|
args: []string{"arg1"},
|
|
isTerminal: false,
|
|
expectedError: "error saving image",
|
|
imageSaveFunc: func(images []string) (io.ReadCloser, error) {
|
|
return ioutil.NopCloser(strings.NewReader("")), errors.Errorf("error saving image")
|
|
},
|
|
},
|
|
{
|
|
name: "output directory does not exist",
|
|
args: []string{"-o", "fakedir/out.tar", "arg1"},
|
|
expectedError: "failed to save image: unable to validate output path: directory \"fakedir\" does not exist",
|
|
},
|
|
}
|
|
for _, tc := range testCases {
|
|
cli := test.NewFakeCli(&fakeClient{imageSaveFunc: tc.imageSaveFunc})
|
|
cli.Out().SetIsTerminal(tc.isTerminal)
|
|
cmd := NewSaveCommand(cli)
|
|
cmd.SetOutput(ioutil.Discard)
|
|
cmd.SetArgs(tc.args)
|
|
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
|
}
|
|
}
|
|
|
|
func TestNewSaveCommandSuccess(t *testing.T) {
|
|
testCases := []struct {
|
|
args []string
|
|
isTerminal bool
|
|
imageSaveFunc func(images []string) (io.ReadCloser, error)
|
|
deferredFunc func()
|
|
}{
|
|
{
|
|
args: []string{"-o", "save_tmp_file", "arg1"},
|
|
isTerminal: true,
|
|
imageSaveFunc: func(images []string) (io.ReadCloser, error) {
|
|
require.Len(t, images, 1)
|
|
assert.Equal(t, "arg1", images[0])
|
|
return ioutil.NopCloser(strings.NewReader("")), nil
|
|
},
|
|
deferredFunc: func() {
|
|
os.Remove("save_tmp_file")
|
|
},
|
|
},
|
|
{
|
|
args: []string{"arg1", "arg2"},
|
|
isTerminal: false,
|
|
imageSaveFunc: func(images []string) (io.ReadCloser, error) {
|
|
require.Len(t, images, 2)
|
|
assert.Equal(t, "arg1", images[0])
|
|
assert.Equal(t, "arg2", images[1])
|
|
return ioutil.NopCloser(strings.NewReader("")), nil
|
|
},
|
|
},
|
|
}
|
|
for _, tc := range testCases {
|
|
cmd := NewSaveCommand(test.NewFakeCliWithOutput(&fakeClient{
|
|
imageSaveFunc: func(images []string) (io.ReadCloser, error) {
|
|
return ioutil.NopCloser(strings.NewReader("")), nil
|
|
},
|
|
}, new(bytes.Buffer)))
|
|
cmd.SetOutput(ioutil.Discard)
|
|
cmd.SetArgs(tc.args)
|
|
assert.NoError(t, cmd.Execute())
|
|
if tc.deferredFunc != nil {
|
|
tc.deferredFunc()
|
|
}
|
|
}
|
|
}
|