Create unit test illustrating unknown flag bug (#1854)

Created a unit test that tests the unknown flag
error message when the unknown flag is located
in different arg positions.
This commit is contained in:
Brian Pursley 2022-11-14 21:46:57 -05:00 committed by GitHub
parent 6b0bd3076c
commit ad6db7f8f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 0 deletions

View File

@ -2692,3 +2692,46 @@ func TestFind(t *testing.T) {
})
}
}
func TestUnknownFlagShouldReturnSameErrorRegardlessOfArgPosition(t *testing.T) {
testCases := [][]string{
//{"--unknown", "--namespace", "foo", "child", "--bar"}, // FIXME: This test case fails, returning the error `unknown command "foo" for "root"` instead of the expected error `unknown flag: --unknown`
{"--namespace", "foo", "--unknown", "child", "--bar"},
{"--namespace", "foo", "child", "--unknown", "--bar"},
{"--namespace", "foo", "child", "--bar", "--unknown"},
{"--unknown", "--namespace=foo", "child", "--bar"},
{"--namespace=foo", "--unknown", "child", "--bar"},
{"--namespace=foo", "child", "--unknown", "--bar"},
{"--namespace=foo", "child", "--bar", "--unknown"},
{"--unknown", "--namespace=foo", "child", "--bar=true"},
{"--namespace=foo", "--unknown", "child", "--bar=true"},
{"--namespace=foo", "child", "--unknown", "--bar=true"},
{"--namespace=foo", "child", "--bar=true", "--unknown"},
}
root := &Command{
Use: "root",
Run: emptyRun,
}
root.PersistentFlags().String("namespace", "", "a string flag")
c := &Command{
Use: "child",
Run: emptyRun,
}
c.Flags().Bool("bar", false, "a boolean flag")
root.AddCommand(c)
for _, tc := range testCases {
t.Run(strings.Join(tc, " "), func(t *testing.T) {
output, err := executeCommand(root, tc...)
if err == nil {
t.Error("expected unknown flag error")
}
checkStringContains(t, output, "unknown flag: --unknown")
})
}
}