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

Function TestActiveHelpAlone

active_help_test.go:29–82  ·  active_help_test.go::TestActiveHelpAlone
(t *testing.T)

Source from the content-addressed store, hash-verified

27)
28
29func TestActiveHelpAlone(t *testing.T) {
30 rootCmd := &Command{
31 Use: "root",
32 Run: emptyRun,
33 }
34
35 activeHelpFunc := func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
36 comps := AppendActiveHelp(nil, activeHelpMessage)
37 return comps, ShellCompDirectiveDefault
38 }
39
40 // Test that activeHelp can be added to a root command
41 rootCmd.ValidArgsFunction = activeHelpFunc
42
43 output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")
44 if err != nil {
45 t.Errorf("Unexpected error: %v", err)
46 }
47
48 expected := strings.Join([]string{
49 fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),
50 ":0",
51 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
52
53 if output != expected {
54 t.Errorf("expected: %q, got: %q", expected, output)
55 }
56
57 rootCmd.ValidArgsFunction = nil
58
59 // Test that activeHelp can be added to a child command
60 childCmd := &Command{
61 Use: "thechild",
62 Short: "The child command",
63 Run: emptyRun,
64 }
65 rootCmd.AddCommand(childCmd)
66
67 childCmd.ValidArgsFunction = activeHelpFunc
68
69 output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")
70 if err != nil {
71 t.Errorf("Unexpected error: %v", err)
72 }
73
74 expected = strings.Join([]string{
75 fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),
76 ":0",
77 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")
78
79 if output != expected {
80 t.Errorf("expected: %q, got: %q", expected, output)
81 }
82}
83
84func TestActiveHelpWithComps(t *testing.T) {
85 rootCmd := &Command{

Callers

nothing calls this directly

Calls 3

AddCommandMethod · 0.95
AppendActiveHelpFunction · 0.85
executeCommandFunction · 0.85

Tested by

no test coverage detected