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

Function TestCustomDefaultShellCompDirective

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

Source from the content-addressed store, hash-verified

4017}
4018
4019func TestCustomDefaultShellCompDirective(t *testing.T) {
4020 rootCmd := &Command{Use: "root", Run: emptyRun}
4021 rootCmd.PersistentFlags().String("string", "", "test string flag")
4022 // use ShellCompDirectiveNoFileComp instead of the default, which is ShellCompDirectiveDefault.
4023 rootCmd.CompletionOptions.SetDefaultShellCompDirective(ShellCompDirectiveNoFileComp)
4024
4025 // child1 inherits the custom DefaultShellCompDirective.
4026 childCmd1 := &Command{Use: "child1", Run: emptyRun}
4027 // child2 resets the custom DefaultShellCompDirective to the default value.
4028 childCmd2 := &Command{Use: "child2", Run: emptyRun}
4029 childCmd2.CompletionOptions.SetDefaultShellCompDirective(ShellCompDirectiveDefault)
4030
4031 rootCmd.AddCommand(childCmd1, childCmd2)
4032
4033 testCases := []struct {
4034 desc string
4035 args []string
4036 expectedDirective string
4037 }{
4038 {
4039 "flag completion on root command with custom DefaultShellCompDirective",
4040 []string{"--string", ""},
4041 "ShellCompDirectiveNoFileComp",
4042 },
4043 {
4044 "flag completion on subcommand with inherited custom DefaultShellCompDirective",
4045 []string{"child1", "--string", ""},
4046 "ShellCompDirectiveNoFileComp",
4047 },
4048 {
4049 "flag completion on subcommand with reset DefaultShellCompDirective",
4050 []string{"child2", "--string", ""},
4051 "ShellCompDirectiveDefault",
4052 },
4053 }
4054
4055 for _, tc := range testCases {
4056 t.Run(tc.desc, func(t *testing.T) {
4057 args := []string{ShellCompNoDescRequestCmd}
4058 args = append(args, tc.args...)
4059
4060 output, err := executeCommand(rootCmd, args...)
4061
4062 if err != nil {
4063 t.Errorf("Unexpected error: %v", err)
4064 }
4065
4066 outputWords := strings.Split(strings.TrimSpace(output), " ")
4067 directive := outputWords[len(outputWords)-1]
4068
4069 if directive != tc.expectedDirective {
4070 t.Errorf("expected: %q, got: %q", tc.expectedDirective, directive)
4071 }
4072 })
4073 }
4074}

Callers

nothing calls this directly

Calls 5

PersistentFlagsMethod · 0.95
AddCommandMethod · 0.95
executeCommandFunction · 0.85
StringMethod · 0.80

Tested by

no test coverage detected