(t *testing.T)
| 2923 | } |
| 2924 | |
| 2925 | func 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 | } |
nothing calls this directly
no test coverage detected