Fix expected behavior

Signed-off-by: JaySon-Huang <tshent@qq.com>
This commit is contained in:
JaySon-Huang 2021-12-03 02:52:49 +08:00
parent 7d8e3706d1
commit a916286db1
No known key found for this signature in database
GPG key ID: 370C14C8CDC7A5ED

View file

@ -2059,7 +2059,7 @@ func TestFParseErrWhitelistSiblingCommand(t *testing.T) {
checkStringContains(t, output, "unknown flag: --unknown") checkStringContains(t, output, "unknown flag: --unknown")
} }
func TestFCommandFindSubCommand(t *testing.T) { func TestFindSubCommand(t *testing.T) {
root := &Command{ root := &Command{
Use: "root", Use: "root",
Run: emptyRun, Run: emptyRun,
@ -2078,22 +2078,11 @@ func TestFCommandFindSubCommand(t *testing.T) {
} }
child.Flags().BoolVar(&opt.a, "a", false, "a") child.Flags().BoolVar(&opt.a, "a", false, "a")
child.Flags().BoolVar(&opt.b, "b", false, "b") 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) root.AddCommand(child)
argsToTest := [][]string{ argsToTest := [][]string{
{"--a", "--b", "child"},
{"child", "--a"}, {"child", "--a"},
{"child", "--b"}, {"child", "--b"},
{"--a=false", "child"},
{"--a=true", "child"},
{"--a", "child"},
{"--b", "child"},
} }
for _, args := range argsToTest { for _, args := range argsToTest {
cmdFound, _, err := root.Find(args) cmdFound, _, err := root.Find(args)
@ -2104,9 +2093,24 @@ func TestFCommandFindSubCommand(t *testing.T) {
t.Errorf("expected found 'child' command with args: %v", args) 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{ root := &Command{
Use: "root", Use: "root",
Run: emptyRun, Run: emptyRun,
@ -2127,21 +2131,18 @@ func TestFFindSubCommand(t *testing.T) {
child.Flags().BoolVar(&opt.b, "b", false, "b") child.Flags().BoolVar(&opt.b, "b", false, "b")
root.AddCommand(child) root.AddCommand(child)
// ok
output, err := executeCommand(root, "--a", "child") output, err := executeCommand(root, "--a", "child")
if err == nil { if err == nil {
t.Error("expected unknown flag error") t.Error("expected unknown flag error")
} }
checkStringContains(t, output, "unknown flag: --a") checkStringContains(t, output, "unknown flag: --a")
// ok
output, err = executeCommand(root, "--b", "child") output, err = executeCommand(root, "--b", "child")
if err == nil { if err == nil {
t.Error("expected unknown flag error") t.Error("expected unknown flag error")
} }
checkStringContains(t, output, "unknown flag: --b") checkStringContains(t, output, "unknown flag: --b")
// fail?
output, err = executeCommand(root, "--a", "--b", "child") output, err = executeCommand(root, "--a", "--b", "child")
if err == nil { if err == nil {
t.Error("expected unknown flag error") t.Error("expected unknown flag error")