diff --git a/cmd/skopeo/copy_test.go b/cmd/skopeo/copy_test.go new file mode 100644 index 000000000..68248dc88 --- /dev/null +++ b/cmd/skopeo/copy_test.go @@ -0,0 +1,18 @@ +package main + +import "testing" + +func TestCopy(t *testing.T) { + // Invalid command-line arguments + for _, args := range [][]string{ + {}, + {"a1"}, + {"a1", "a2", "a3"}, + } { + out, err := runSkopeo(append([]string{"--insecure-policy", "copy"}, args...)...) + assertTestFailed(t, out, err, "Exactly two arguments expected") + } + + // FIXME: Much more test coverage + // Actual feature tests exist in integration and systemtest +} diff --git a/cmd/skopeo/list_tags_test.go b/cmd/skopeo/list_tags_test.go index 2b3cc2d5b..b1707f731 100644 --- a/cmd/skopeo/list_tags_test.go +++ b/cmd/skopeo/list_tags_test.go @@ -54,3 +54,17 @@ func TestDockerRepositoryReferenceParserDrift(t *testing.T) { } } } + +func TestListTags(t *testing.T) { + // Invalid command-line arguments + for _, args := range [][]string{ + {}, + {"a1", "a2"}, + } { + out, err := runSkopeo(append([]string{"list-tags"}, args...)...) + assertTestFailed(t, out, err, "Exactly one non-option argument expected") + } + + // FIXME: Much more test coverage + // Actual feature tests exist in systemtest +} diff --git a/cmd/skopeo/sync_test.go b/cmd/skopeo/sync_test.go index 734abfedd..17ba4c2f6 100644 --- a/cmd/skopeo/sync_test.go +++ b/cmd/skopeo/sync_test.go @@ -44,3 +44,18 @@ func TestTLSVerifyConfig(t *testing.T) { err := yaml.Unmarshal([]byte(`tls-verify: "not a valid bool"`), &config) assert.Error(t, err) } + +func TestSync(t *testing.T) { + // Invalid command-line arguments + for _, args := range [][]string{ + {}, + {"a1"}, + {"a1", "a2", "a3"}, + } { + out, err := runSkopeo(append([]string{"sync"}, args...)...) + assertTestFailed(t, out, err, "Exactly two arguments expected") + } + + // FIXME: Much more test coverage + // Actual feature tests exist in integration and systemtest +} diff --git a/cmd/skopeo/utils.go b/cmd/skopeo/utils.go index 75a2fa823..fbf4e6283 100644 --- a/cmd/skopeo/utils.go +++ b/cmd/skopeo/utils.go @@ -26,7 +26,7 @@ import ( "golang.org/x/term" ) -// errorShouldDisplayUsage is a subtype of error used by command handlers to indicate that cli.ShowSubcommandHelp should be called. +// errorShouldDisplayUsage is a subtype of error used by command handlers to indicate that the command’s help should be included. type errorShouldDisplayUsage struct { error } @@ -62,7 +62,8 @@ func commandAction(handler func(args []string, stdout io.Writer) error) func(cmd err := handler(args, c.OutOrStdout()) var shouldDisplayUsage errorShouldDisplayUsage if errors.As(err, &shouldDisplayUsage) { - return c.Help() + c.SetOut(c.ErrOrStderr()) // This mutates c, but we are failing anyway. + _ = c.Help() // Even if this failed, we prefer to report the original error } return err }