cli/command/images: set cmd.Args to prevent test-failures

When running tests from my IDE, it compiles the tests before running,
then executes the compiled binary to run the tests. Cobra doesn't like that,
because in that situation os.Args is taken as argument for the command that's
executed. The command that's tested now sees the `test-` flags as arguments
(`-test.v -test.run ..`), which causes various tests to fail ("Command XYZ
does not accept arguments").

    # compile the tests:
    go test -c -o foo.test

    # execute the test:
    ./foo.test -test.v -test.run TestFoo
    === RUN   TestFoo
    Error: "foo" accepts no arguments.

Set arguments to an empty slice to make sure it doesn't inherit arguments
from the test-binary.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-10-08 20:26:15 +02:00
parent b64f265d0f
commit 839dbbcf27
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 4 additions and 0 deletions

View File

@ -28,11 +28,13 @@ func TestNewLoadCommandErrors(t *testing.T) {
}, },
{ {
name: "input-to-terminal", name: "input-to-terminal",
args: []string{},
isTerminalIn: true, isTerminalIn: true,
expectedError: "requested load from stdin, but stdin is empty", expectedError: "requested load from stdin, but stdin is empty",
}, },
{ {
name: "pull-error", name: "pull-error",
args: []string{},
expectedError: "something went wrong", expectedError: "something went wrong",
imageLoadFunc: func(input io.Reader, options image.LoadOptions) (image.LoadResponse, error) { imageLoadFunc: func(input io.Reader, options image.LoadOptions) (image.LoadResponse, error) {
return image.LoadResponse{}, errors.Errorf("something went wrong") return image.LoadResponse{}, errors.Errorf("something went wrong")
@ -71,12 +73,14 @@ func TestNewLoadCommandSuccess(t *testing.T) {
}{ }{
{ {
name: "simple", name: "simple",
args: []string{},
imageLoadFunc: func(input io.Reader, options image.LoadOptions) (image.LoadResponse, error) { imageLoadFunc: func(input io.Reader, options image.LoadOptions) (image.LoadResponse, error) {
return image.LoadResponse{Body: io.NopCloser(strings.NewReader("Success"))}, nil return image.LoadResponse{Body: io.NopCloser(strings.NewReader("Success"))}, nil
}, },
}, },
{ {
name: "json", name: "json",
args: []string{},
imageLoadFunc: func(input io.Reader, options image.LoadOptions) (image.LoadResponse, error) { imageLoadFunc: func(input io.Reader, options image.LoadOptions) (image.LoadResponse, error) {
json := "{\"ID\": \"1\"}" json := "{\"ID\": \"1\"}"
return image.LoadResponse{ return image.LoadResponse{