mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
Fix expected behavior
Signed-off-by: JaySon-Huang <tshent@qq.com>
This commit is contained in:
parent
7d8e3706d1
commit
a916286db1
1 changed files with 17 additions and 16 deletions
|
@ -2059,7 +2059,7 @@ func TestFParseErrWhitelistSiblingCommand(t *testing.T) {
|
|||
checkStringContains(t, output, "unknown flag: --unknown")
|
||||
}
|
||||
|
||||
func TestFCommandFindSubCommand(t *testing.T) {
|
||||
func TestFindSubCommand(t *testing.T) {
|
||||
root := &Command{
|
||||
Use: "root",
|
||||
Run: emptyRun,
|
||||
|
@ -2078,22 +2078,11 @@ func TestFCommandFindSubCommand(t *testing.T) {
|
|||
}
|
||||
child.Flags().BoolVar(&opt.a, "a", false, "a")
|
||||
child.Flags().BoolVar(&opt.b, "b", false, "b")
|
||||
flag := child.Flags().Lookup("a")
|
||||
if flag != nil {
|
||||
if flag.NoOptDefVal != "true" {
|
||||
t.Errorf("ffff %v", flag.NoOptDefVal)
|
||||
}
|
||||
}
|
||||
root.AddCommand(child)
|
||||
|
||||
argsToTest := [][]string{
|
||||
{"--a", "--b", "child"},
|
||||
{"child", "--a"},
|
||||
{"child", "--b"},
|
||||
{"--a=false", "child"},
|
||||
{"--a=true", "child"},
|
||||
{"--a", "child"},
|
||||
{"--b", "child"},
|
||||
}
|
||||
for _, args := range argsToTest {
|
||||
cmdFound, _, err := root.Find(args)
|
||||
|
@ -2104,9 +2093,24 @@ func TestFCommandFindSubCommand(t *testing.T) {
|
|||
t.Errorf("expected found 'child' command with args: %v", args)
|
||||
}
|
||||
}
|
||||
|
||||
argsToTestNotFound := [][]string{
|
||||
{"--a", "--b", "child"},
|
||||
{"--a", "child"},
|
||||
{"--b", "child"},
|
||||
}
|
||||
for _, args := range argsToTestNotFound {
|
||||
cmdFound, _, err := root.Find(args)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v with args: %v", err, args)
|
||||
}
|
||||
if cmdFound.Name() != "root" {
|
||||
t.Errorf("expected found 'root' command with args: %v", args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFFindSubCommand(t *testing.T) {
|
||||
func TestFParseUnknownFlagFromSubCommand(t *testing.T) {
|
||||
root := &Command{
|
||||
Use: "root",
|
||||
Run: emptyRun,
|
||||
|
@ -2127,21 +2131,18 @@ func TestFFindSubCommand(t *testing.T) {
|
|||
child.Flags().BoolVar(&opt.b, "b", false, "b")
|
||||
root.AddCommand(child)
|
||||
|
||||
// ok
|
||||
output, err := executeCommand(root, "--a", "child")
|
||||
if err == nil {
|
||||
t.Error("expected unknown flag error")
|
||||
}
|
||||
checkStringContains(t, output, "unknown flag: --a")
|
||||
|
||||
// ok
|
||||
output, err = executeCommand(root, "--b", "child")
|
||||
if err == nil {
|
||||
t.Error("expected unknown flag error")
|
||||
}
|
||||
checkStringContains(t, output, "unknown flag: --b")
|
||||
|
||||
// fail?
|
||||
output, err = executeCommand(root, "--a", "--b", "child")
|
||||
if err == nil {
|
||||
t.Error("expected unknown flag error")
|
||||
|
|
Loading…
Reference in a new issue