mirror of
https://github.com/spf13/cobra
synced 2025-05-05 21:07:24 +00:00
flag normalization tests
This commit is contained in:
parent
0897e7eb57
commit
ac6d398796
1 changed files with 65 additions and 0 deletions
|
@ -1167,11 +1167,18 @@ func TestGlobalNormFuncPropagation(t *testing.T) {
|
|||
}
|
||||
|
||||
rootCmd := initialize()
|
||||
rootCmd.AddCommand(cmdEcho)
|
||||
|
||||
rootCmd.SetGlobalNormalizationFunc(normFunc)
|
||||
if reflect.ValueOf(normFunc).Pointer() != reflect.ValueOf(rootCmd.GlobalNormalizationFunc()).Pointer() {
|
||||
t.Error("rootCmd seems to have a wrong normalization function")
|
||||
}
|
||||
|
||||
// Also check it propagates retroactively
|
||||
if reflect.ValueOf(normFunc).Pointer() != reflect.ValueOf(cmdEcho.GlobalNormalizationFunc()).Pointer() {
|
||||
t.Error("cmdEcho should have had the normalization function of rootCmd")
|
||||
}
|
||||
|
||||
// First add the cmdEchoSub to cmdPrint
|
||||
cmdPrint.AddCommand(cmdEchoSub)
|
||||
if cmdPrint.GlobalNormalizationFunc() != nil && cmdEchoSub.GlobalNormalizationFunc() != nil {
|
||||
|
@ -1186,6 +1193,64 @@ func TestGlobalNormFuncPropagation(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestNormPassedOnLocal(t *testing.T) {
|
||||
n := func(f *pflag.FlagSet, name string) pflag.NormalizedName {
|
||||
return pflag.NormalizedName(strings.ToUpper(name))
|
||||
}
|
||||
|
||||
cmd := &Command{}
|
||||
flagVal := false
|
||||
|
||||
cmd.Flags().BoolVar(&flagVal, "flagname", true, "this is a dummy flag")
|
||||
cmd.SetGlobalNormalizationFunc(n)
|
||||
if cmd.LocalFlags().Lookup("flagname") != cmd.LocalFlags().Lookup("FLAGNAME") {
|
||||
t.Error("Normalization function should be passed on to Local flag set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormPassedOnInherited(t *testing.T) {
|
||||
n := func(f *pflag.FlagSet, name string) pflag.NormalizedName {
|
||||
return pflag.NormalizedName(strings.ToUpper(name))
|
||||
}
|
||||
|
||||
cmd, childBefore, childAfter := &Command{}, &Command{}, &Command{}
|
||||
flagVal := false
|
||||
cmd.AddCommand(childBefore)
|
||||
|
||||
cmd.PersistentFlags().BoolVar(&flagVal, "flagname", true, "this is a dummy flag")
|
||||
cmd.SetGlobalNormalizationFunc(n)
|
||||
|
||||
cmd.AddCommand(childAfter)
|
||||
|
||||
if f := childBefore.InheritedFlags(); f.Lookup("flagname") == nil || f.Lookup("flagname") != f.Lookup("FLAGNAME") {
|
||||
t.Error("Normalization function should be passed on to inherited flag set in command added before flag")
|
||||
}
|
||||
if f := childAfter.InheritedFlags(); f.Lookup("flagname") == nil || f.Lookup("flagname") != f.Lookup("FLAGNAME") {
|
||||
t.Error("Normalization function should be passed on to inherited flag set in command added after flag")
|
||||
}
|
||||
}
|
||||
|
||||
// Related to https://github.com/spf13/cobra/issues/521.
|
||||
func TestNormConsistent(t *testing.T) {
|
||||
n := func(f *pflag.FlagSet, name string) pflag.NormalizedName {
|
||||
return pflag.NormalizedName(strings.ToUpper(name))
|
||||
}
|
||||
|
||||
cmd := &Command{}
|
||||
flagVal := false
|
||||
|
||||
cmd.Flags().BoolVar(&flagVal, "flagname", true, "this is a dummy flag")
|
||||
// Build local flag set
|
||||
cmd.LocalFlags()
|
||||
|
||||
cmd.SetGlobalNormalizationFunc(n)
|
||||
cmd.SetGlobalNormalizationFunc(nil)
|
||||
|
||||
if cmd.LocalFlags().Lookup("flagname") == cmd.LocalFlags().Lookup("FLAGNAME") {
|
||||
t.Error("Normalizing flag names should not result in duplicate flags")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlagOnPflagCommandLine(t *testing.T) {
|
||||
flagName := "flagOnCommandLine"
|
||||
pflag.String(flagName, "", "about my flag")
|
||||
|
|
Loading…
Add table
Reference in a new issue