-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Fix shorthand combination edge case #2189
Open
inicula
wants to merge
4
commits into
spf13:main
Choose a base branch
from
inicula:fix-shorthand-combination-bug
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
|
@@ -693,6 +693,30 @@ func TestStripFlags(t *testing.T) { | |
[]string{"-p", "bar"}, | ||
[]string{"bar"}, | ||
}, | ||
{ | ||
[]string{"-s", "value", "bar"}, | ||
[]string{"bar"}, | ||
}, | ||
{ | ||
[]string{"-s=value", "bar"}, | ||
[]string{"bar"}, | ||
}, | ||
{ | ||
[]string{"-svalue", "bar"}, | ||
[]string{"bar"}, | ||
}, | ||
{ | ||
[]string{"-ps", "value", "bar"}, | ||
[]string{"bar"}, | ||
}, | ||
{ | ||
[]string{"-ps=value", "bar"}, | ||
[]string{"bar"}, | ||
}, | ||
{ | ||
[]string{"-psvalue", "bar"}, | ||
[]string{"bar"}, | ||
}, | ||
} | ||
|
||
c := &Command{Use: "c", Run: emptyRun} | ||
|
@@ -702,7 +726,7 @@ func TestStripFlags(t *testing.T) { | |
c.Flags().BoolP("bool", "b", false, "") | ||
|
||
for i, test := range tests { | ||
got := stripFlags(test.input, c) | ||
got, _ := stripFlags(test.input, c) | ||
if !reflect.DeepEqual(test.output, got) { | ||
t.Errorf("(%v) Expected: %v, got: %v", i, test.output, got) | ||
} | ||
|
@@ -2215,6 +2239,7 @@ func TestUseDeprecatedFlags(t *testing.T) { | |
checkStringContains(t, output, "This flag is deprecated") | ||
} | ||
|
||
//nolint:goconst,nolintlint // Disable check for string literal occurrences | ||
func TestTraverseWithParentFlags(t *testing.T) { | ||
rootCmd := &Command{Use: "root", TraverseChildren: true} | ||
rootCmd.Flags().String("str", "", "") | ||
|
@@ -2229,12 +2254,70 @@ func TestTraverseWithParentFlags(t *testing.T) { | |
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if len(args) != 1 && args[0] != "--add" { | ||
if len(args) != 1 || args[0] != "--int" { | ||
t.Errorf("Wrong args: %v", args) | ||
} | ||
if c.Name() != childCmd.Name() { | ||
t.Errorf("Expected command: %q, got: %q", childCmd.Name(), c.Name()) | ||
} | ||
} | ||
|
||
//nolint:goconst,nolintlint // Disable check for string literal occurrences | ||
func TestTraverseWithShorthandCombinationInParentFlags(t *testing.T) { | ||
rootCmd := &Command{Use: "root", TraverseChildren: true} | ||
stringVal := rootCmd.Flags().StringP("str", "s", "", "") | ||
boolVal := rootCmd.Flags().BoolP("bool", "b", false, "") | ||
|
||
childCmd := &Command{Use: "child"} | ||
childCmd.Flags().Int("int", -1, "") | ||
|
||
rootCmd.AddCommand(childCmd) | ||
|
||
c, args, err := rootCmd.Traverse([]string{"-bs", "ok", "child", "--int"}) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if len(args) != 1 || args[0] != "--int" { | ||
t.Errorf("Wrong args: %v", args) | ||
} | ||
if c.Name() != childCmd.Name() { | ||
t.Errorf("Expected command: %q, got: %q", childCmd.Name(), c.Name()) | ||
} | ||
if *stringVal != "ok" { | ||
t.Errorf("Expected -s to be set to: %s, got: %s", "ok", *stringVal) | ||
} | ||
if !*boolVal { | ||
t.Errorf("Expected -b to be set") | ||
} | ||
} | ||
|
||
//nolint:goconst,nolintlint // Disable check for string literal occurrences | ||
func TestTraverseWithArgumentIdenticalToCommandName(t *testing.T) { | ||
rootCmd := &Command{Use: "root", TraverseChildren: true} | ||
stringVal := rootCmd.Flags().StringP("str", "s", "", "") | ||
boolVal := rootCmd.Flags().BoolP("bool", "b", false, "") | ||
|
||
childCmd := &Command{Use: "child"} | ||
childCmd.Flags().Int("int", -1, "") | ||
|
||
rootCmd.AddCommand(childCmd) | ||
|
||
c, args, err := rootCmd.Traverse([]string{"-bs", "child", "child", "--int"}) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if len(args) != 1 || args[0] != "--int" { | ||
t.Errorf("Wrong args: %v", args) | ||
} | ||
if c.Name() != childCmd.Name() { | ||
t.Errorf("Expected command: %q, got: %q", childCmd.Name(), c.Name()) | ||
} | ||
if *stringVal != "child" { | ||
t.Errorf("Expected -s to be set to: %s, got: %s", "child", *stringVal) | ||
} | ||
if !*boolVal { | ||
t.Errorf("Expected -b to be set") | ||
} | ||
} | ||
|
||
func TestTraverseNoParentFlags(t *testing.T) { | ||
|
@@ -2288,7 +2371,7 @@ func TestTraverseWithBadChildFlag(t *testing.T) { | |
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if len(args) != 1 && args[0] != "--str" { | ||
if len(args) != 1 || args[0] != "--str" { | ||
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. See above. |
||
t.Errorf("Wrong args: %v", args) | ||
} | ||
if c.Name() != childCmd.Name() { | ||
|
@@ -2688,11 +2771,13 @@ func TestHelpflagCommandExecutedWithoutVersionSet(t *testing.T) { | |
|
||
func TestFind(t *testing.T) { | ||
var foo, bar string | ||
var persist bool | ||
root := &Command{ | ||
Use: "root", | ||
} | ||
root.PersistentFlags().StringVarP(&foo, "foo", "f", "", "") | ||
root.PersistentFlags().StringVarP(&bar, "bar", "b", "something", "") | ||
root.PersistentFlags().BoolVarP(&persist, "persist", "p", false, "") | ||
|
||
child := &Command{ | ||
Use: "child", | ||
|
@@ -2755,6 +2840,38 @@ func TestFind(t *testing.T) { | |
[]string{"--foo", "child", "--bar", "something", "child"}, | ||
[]string{"--foo", "child", "--bar", "something"}, | ||
}, | ||
{ | ||
[]string{"-f", "value", "child"}, | ||
[]string{"-f", "value"}, | ||
}, | ||
{ | ||
[]string{"-f=value", "child"}, | ||
[]string{"-f=value"}, | ||
}, | ||
{ | ||
[]string{"-fvalue", "child"}, | ||
[]string{"-fvalue"}, | ||
}, | ||
{ | ||
[]string{"-pf", "value", "child"}, | ||
[]string{"-pf", "value"}, | ||
}, | ||
{ | ||
[]string{"-pf=value", "child"}, | ||
[]string{"-pf=value"}, | ||
}, | ||
{ | ||
[]string{"-pfvalue", "child"}, | ||
[]string{"-pfvalue"}, | ||
}, | ||
{ | ||
[]string{"-pf", "child", "child"}, | ||
[]string{"-pf", "child"}, | ||
}, | ||
{ | ||
[]string{"-pf", "child", "-pb", "something", "child"}, | ||
[]string{"-pf", "child", "-pb", "something"}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
|
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.
The original condition seems wrong.
--add
wasn't mentioned anywhere else in the repo and from how I understand the test,||
makes sense here, not&&
.