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

Function TestPersistentRequiredFlags

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

Source from the content-addressed store, hash-verified

869}
870
871func TestPersistentRequiredFlags(t *testing.T) {
872 parent := &Command{Use: "parent", Run: emptyRun}
873 parent.PersistentFlags().String("foo1", "", "")
874 assertNoErr(t, parent.MarkPersistentFlagRequired("foo1"))
875 parent.PersistentFlags().String("foo2", "", "")
876 assertNoErr(t, parent.MarkPersistentFlagRequired("foo2"))
877 parent.Flags().String("foo3", "", "")
878
879 child := &Command{Use: "child", Run: emptyRun}
880 child.Flags().String("bar1", "", "")
881 assertNoErr(t, child.MarkFlagRequired("bar1"))
882 child.Flags().String("bar2", "", "")
883 assertNoErr(t, child.MarkFlagRequired("bar2"))
884 child.Flags().String("bar3", "", "")
885
886 parent.AddCommand(child)
887
888 expected := fmt.Sprintf("required flag(s) %q, %q, %q, %q not set", "bar1", "bar2", "foo1", "foo2")
889
890 _, err := executeCommand(parent, "child")
891 if err.Error() != expected {
892 t.Errorf("Expected %q, got %q", expected, err.Error())
893 }
894}
895
896func TestPersistentRequiredFlagsWithDisableFlagParsing(t *testing.T) {
897 // Make sure a required persistent flag does not break

Callers

nothing calls this directly

Calls 9

PersistentFlagsMethod · 0.95
FlagsMethod · 0.95
MarkFlagRequiredMethod · 0.95
AddCommandMethod · 0.95
executeCommandFunction · 0.85
StringMethod · 0.80
ErrorMethod · 0.80
assertNoErrFunction · 0.70

Tested by

no test coverage detected