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

Function TestRequiredFlagNameCompletionInGo

completions_test.go:844–1036  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

842}
843
844func TestRequiredFlagNameCompletionInGo(t *testing.T) {
845 rootCmd := &Command{
846 Use: "root",
847 ValidArgs: []string{"realArg"},
848 Run: emptyRun,
849 }
850 childCmd := &Command{
851 Use: "childCmd",
852 ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
853 return []string{"subArg"}, ShellCompDirectiveNoFileComp
854 },
855 Run: emptyRun,
856 }
857 rootCmd.AddCommand(childCmd)
858
859 rootCmd.Flags().IntP("requiredFlag", "r", -1, "required flag")
860 assertNoErr(t, rootCmd.MarkFlagRequired("requiredFlag"))
861 requiredFlag := rootCmd.Flags().Lookup("requiredFlag")
862
863 rootCmd.PersistentFlags().IntP("requiredPersistent", "p", -1, "required persistent")
864 assertNoErr(t, rootCmd.MarkPersistentFlagRequired("requiredPersistent"))
865 requiredPersistent := rootCmd.PersistentFlags().Lookup("requiredPersistent")
866
867 rootCmd.Flags().StringP("release", "R", "", "Release name")
868
869 childCmd.Flags().BoolP("subRequired", "s", false, "sub required flag")
870 assertNoErr(t, childCmd.MarkFlagRequired("subRequired"))
871 childCmd.Flags().BoolP("subNotRequired", "n", false, "sub not required flag")
872
873 // Test that a required flag is suggested even without the - prefix
874 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")
875 if err != nil {
876 t.Errorf("Unexpected error: %v", err)
877 }
878
879 expected := strings.Join([]string{
880 "childCmd",
881 "completion",
882 "help",
883 "--requiredFlag",
884 "-r",
885 "--requiredPersistent",
886 "-p",
887 "realArg",
888 ":4",
889 "Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")
890
891 if output != expected {
892 t.Errorf("expected: %q, got: %q", expected, output)
893 }
894
895 // Test that a required flag is suggested without other flags when using the '-' prefix
896 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-")
897 if err != nil {
898 t.Errorf("Unexpected error: %v", err)
899 }
900
901 expected = strings.Join([]string{

Callers

nothing calls this directly

Calls 7

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

Tested by

no test coverage detected