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

Function TestHelpFuncExecuted

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

Source from the content-addressed store, hash-verified

2923}
2924
2925func TestHelpFuncExecuted(t *testing.T) {
2926 helpText := "Long description"
2927
2928 // Create a context that will be unique, not just the background context
2929 //nolint:staticcheck // We can safely use a basic type as key in tests.
2930 executionCtx := context.WithValue(context.Background(), "testKey", "123")
2931
2932 child := &Command{Use: "child", Run: emptyRun}
2933 child.SetHelpFunc(func(cmd *Command, args []string) {
2934 _, err := cmd.OutOrStdout().Write([]byte(helpText))
2935 if err != nil {
2936 t.Error(err)
2937 }
2938
2939 // Test for https://github.com/spf13/cobra/issues/2240
2940 if cmd.Context() != executionCtx {
2941 t.Error("Context doesn't equal the execution context")
2942 }
2943 })
2944
2945 rootCmd := &Command{Use: "root", Run: emptyRun}
2946 rootCmd.AddCommand(child)
2947
2948 output, err := executeCommandWithContext(executionCtx, rootCmd, "help", "child")
2949 if err != nil {
2950 t.Errorf("Unexpected error: %v", err)
2951 }
2952
2953 checkStringContains(t, output, helpText)
2954}

Callers

nothing calls this directly

Calls 7

SetHelpFuncMethod · 0.95
AddCommandMethod · 0.95
OutOrStdoutMethod · 0.80
ErrorMethod · 0.80
ContextMethod · 0.80
checkStringContainsFunction · 0.70

Tested by

no test coverage detected