Skip to content

Commit

Permalink
Merge pull request #2469 from mtrmac/error-with-usage
Browse files Browse the repository at this point in the history
Fix handling of errorShouldDisplayUsage
  • Loading branch information
cgwalters authored Dec 3, 2024
2 parents 4863e05 + 27baed9 commit e288827
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
18 changes: 18 additions & 0 deletions cmd/skopeo/copy_test.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions cmd/skopeo/list_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions cmd/skopeo/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 3 additions & 2 deletions cmd/skopeo/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit e288827

Please sign in to comment.