-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: Use structs for CLI-validation errors returned by Cobra. #2266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eth-p
wants to merge
6
commits into
spf13:main
Choose a base branch
from
eth-p:error-structs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+668
−29
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8dd7f63
refactor: Split up findSuggestions function
eth-p 4aae22c
feat: Use error structs for errors returned in arg validation
eth-p 7cc4f93
feat: Use struct error for errors returned in flag validation
eth-p a010bc6
feat: Add getters to error structs
eth-p d3a8d96
test: Add tests for error structs
eth-p 4f45711
doc: Add user guide section on error structs
eth-p File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,7 +15,9 @@ | |||||||||||||||||||||||||||||||||||||||||
| package cobra | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||
| "errors" | ||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||
| "reflect" | ||||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||||
| "testing" | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -32,6 +34,14 @@ func getCommand(args PositionalArgs, withValid bool) *Command { | |||||||||||||||||||||||||||||||||||||||||
| return c | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func getCommandName(c *Command) string { | ||||||||||||||||||||||||||||||||||||||||||
| if c == nil { | ||||||||||||||||||||||||||||||||||||||||||
| return "<nil>" | ||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||
| return c.Name() | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func expectSuccess(output string, err error, t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
| if output != "" { | ||||||||||||||||||||||||||||||||||||||||||
| t.Errorf("Unexpected output: %v", output) | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -41,6 +51,31 @@ func expectSuccess(output string, err error, t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func expectErrorAs(err error, target error, t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
| if err == nil { | ||||||||||||||||||||||||||||||||||||||||||
| t.Fatalf("Expected error, got nil") | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| targetType := reflect.TypeOf(target) | ||||||||||||||||||||||||||||||||||||||||||
| targetPtr := reflect.New(targetType).Interface() // *SomeError | ||||||||||||||||||||||||||||||||||||||||||
| if !errors.As(err, targetPtr) { | ||||||||||||||||||||||||||||||||||||||||||
| t.Fatalf("Expected error to be %T, got %T", target, err) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+54
to
+64
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not using simply using this?
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, actually, yeah. Good point. I'll fix it in the next set of changes. |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func expectErrorHasCommand(err error, cmd *Command, t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
| getCommand, ok := err.(interface{ GetCommand() *Command }) | ||||||||||||||||||||||||||||||||||||||||||
| if !ok { | ||||||||||||||||||||||||||||||||||||||||||
| t.Fatalf("Expected error to have GetCommand method, but did not") | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| got := getCommand.GetCommand() | ||||||||||||||||||||||||||||||||||||||||||
| if cmd != got { | ||||||||||||||||||||||||||||||||||||||||||
| t.Errorf("Expected err.GetCommand to return %v, got %v", | ||||||||||||||||||||||||||||||||||||||||||
| getCommandName(cmd), getCommandName(got)) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func validOnlyWithInvalidArgs(err error, t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
| if err == nil { | ||||||||||||||||||||||||||||||||||||||||||
| t.Fatal("Expected an error") | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -139,6 +174,13 @@ func TestNoArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||
| validOnlyWithInvalidArgs(err, t) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func TestNoArgs_ReturnsUnknownSubcommandError(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
| c := getCommand(NoArgs, false) | ||||||||||||||||||||||||||||||||||||||||||
| _, err := executeCommand(c, "a") | ||||||||||||||||||||||||||||||||||||||||||
| expectErrorAs(err, UnknownSubcommandError{}, t) | ||||||||||||||||||||||||||||||||||||||||||
| expectErrorHasCommand(err, c, t) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // OnlyValidArgs | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func TestOnlyValidArgs(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -153,6 +195,13 @@ func TestOnlyValidArgs_WithInvalidArgs(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||
| validOnlyWithInvalidArgs(err, t) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func TestOnlyValidArgs_ReturnsInvalidArgValueError(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
| c := getCommand(OnlyValidArgs, true) | ||||||||||||||||||||||||||||||||||||||||||
| _, err := executeCommand(c, "a") | ||||||||||||||||||||||||||||||||||||||||||
| expectErrorAs(err, InvalidArgValueError{}, t) | ||||||||||||||||||||||||||||||||||||||||||
| expectErrorHasCommand(err, c, t) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ArbitraryArgs | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func TestArbitraryArgs(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -229,6 +278,13 @@ func TestMinimumNArgs_WithLessArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||
| validOnlyWithInvalidArgs(err, t) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func TestMinimumNArgs_ReturnsInvalidArgCountError(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
| c := getCommand(MinimumNArgs(2), true) | ||||||||||||||||||||||||||||||||||||||||||
| _, err := executeCommand(c, "a") | ||||||||||||||||||||||||||||||||||||||||||
| expectErrorAs(err, InvalidArgCountError{}, t) | ||||||||||||||||||||||||||||||||||||||||||
| expectErrorHasCommand(err, c, t) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // MaximumNArgs | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func TestMaximumNArgs(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -279,6 +335,13 @@ func TestMaximumNArgs_WithMoreArgs_WithValidOnly_WithInvalidArgs(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||
| validOnlyWithInvalidArgs(err, t) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func TestMaximumNArgs_ReturnsInvalidArgCountError(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
| c := getCommand(MaximumNArgs(2), true) | ||||||||||||||||||||||||||||||||||||||||||
| _, err := executeCommand(c, "a", "b", "c") | ||||||||||||||||||||||||||||||||||||||||||
| expectErrorAs(err, InvalidArgCountError{}, t) | ||||||||||||||||||||||||||||||||||||||||||
| expectErrorHasCommand(err, c, t) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ExactArgs | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func TestExactArgs(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -329,6 +392,13 @@ func TestExactArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) | |||||||||||||||||||||||||||||||||||||||||
| validOnlyWithInvalidArgs(err, t) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func TestExactArgs_ReturnsInvalidArgCountError(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
| c := getCommand(ExactArgs(2), true) | ||||||||||||||||||||||||||||||||||||||||||
| _, err := executeCommand(c, "a") | ||||||||||||||||||||||||||||||||||||||||||
| expectErrorAs(err, InvalidArgCountError{}, t) | ||||||||||||||||||||||||||||||||||||||||||
| expectErrorHasCommand(err, c, t) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // RangeArgs | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func TestRangeArgs(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -379,6 +449,13 @@ func TestRangeArgs_WithInvalidCount_WithValidOnly_WithInvalidArgs(t *testing.T) | |||||||||||||||||||||||||||||||||||||||||
| validOnlyWithInvalidArgs(err, t) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func TestRangeArgs_ReturnsInvalidArgCountError(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
| c := getCommand(RangeArgs(2, 4), true) | ||||||||||||||||||||||||||||||||||||||||||
| _, err := executeCommand(c, "a") | ||||||||||||||||||||||||||||||||||||||||||
| expectErrorAs(err, InvalidArgCountError{}, t) | ||||||||||||||||||||||||||||||||||||||||||
| expectErrorHasCommand(err, c, t) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Takes(No)Args | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| func TestRootTakesNoArgs(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.