MCPcopy
hub / github.com/spf13/cobra / TestChildFlagShadowsParentPersistentFlag

Function TestChildFlagShadowsParentPersistentFlag

command_test.go:789–816  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

787}
788
789func TestChildFlagShadowsParentPersistentFlag(t *testing.T) {
790 parent := &Command{Use: "parent", Run: emptyRun}
791 child := &Command{Use: "child", Run: emptyRun}
792
793 parent.PersistentFlags().Bool("boolf", false, "")
794 parent.PersistentFlags().Int("intf", -1, "")
795 child.Flags().String("strf", "", "")
796 child.Flags().Int("intf", -1, "")
797
798 parent.AddCommand(child)
799
800 childInherited := child.InheritedFlags()
801 childLocal := child.LocalFlags()
802
803 if childLocal.Lookup("strf") == nil {
804 t.Error(`LocalFlags expected to contain "strf", got "nil"`)
805 }
806 if childInherited.Lookup("boolf") == nil {
807 t.Error(`InheritedFlags expected to contain "boolf", got "nil"`)
808 }
809
810 if childInherited.Lookup("intf") != nil {
811 t.Errorf(`InheritedFlags should not contain shadowed flag "intf"`)
812 }
813 if childLocal.Lookup("intf") == nil {
814 t.Error(`LocalFlags expected to contain "intf", got "nil"`)
815 }
816}
817
818func TestPersistentFlagsOnChild(t *testing.T) {
819 var childCmdArgs []string

Callers

nothing calls this directly

Calls 7

PersistentFlagsMethod · 0.95
FlagsMethod · 0.95
AddCommandMethod · 0.95
InheritedFlagsMethod · 0.95
LocalFlagsMethod · 0.95
StringMethod · 0.80
ErrorMethod · 0.80

Tested by

no test coverage detected