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

Function TestPersistentRequiredFlagsWithDisableFlagParsing

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

Source from the content-addressed store, hash-verified

894}
895
896func TestPersistentRequiredFlagsWithDisableFlagParsing(t *testing.T) {
897 // Make sure a required persistent flag does not break
898 // commands that disable flag parsing
899
900 parent := &Command{Use: "parent", Run: emptyRun}
901 parent.PersistentFlags().Bool("foo", false, "")
902 flag := parent.PersistentFlags().Lookup("foo")
903 assertNoErr(t, parent.MarkPersistentFlagRequired("foo"))
904
905 child := &Command{Use: "child", Run: emptyRun}
906 child.DisableFlagParsing = true
907
908 parent.AddCommand(child)
909
910 if _, err := executeCommand(parent, "--foo", "child"); err != nil {
911 t.Errorf("Unexpected error: %v", err)
912 }
913
914 // Reset the flag or else it will remember the state from the previous command
915 flag.Changed = false
916 if _, err := executeCommand(parent, "child", "--foo"); err != nil {
917 t.Errorf("Unexpected error: %v", err)
918 }
919
920 // Reset the flag or else it will remember the state from the previous command
921 flag.Changed = false
922 if _, err := executeCommand(parent, "child"); err != nil {
923 t.Errorf("Unexpected error: %v", err)
924 }
925}
926
927func TestInitHelpFlagMergesFlags(t *testing.T) {
928 usage := "custom flag"

Callers

nothing calls this directly

Calls 5

PersistentFlagsMethod · 0.95
AddCommandMethod · 0.95
executeCommandFunction · 0.85
assertNoErrFunction · 0.70

Tested by

no test coverage detected