From d848cfcdf81e747c057c1f8ab7631f29d12a13e7 Mon Sep 17 00:00:00 2001 From: Fraser Waters <fraser@pulumi.com> Date: Fri, 21 Feb 2025 08:33:59 +0000 Subject: [PATCH] Check specific context --- command_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/command_test.go b/command_test.go index 0c7aae74..175f2ffd 100644 --- a/command_test.go +++ b/command_test.go @@ -2925,6 +2925,9 @@ func TestUnknownFlagShouldReturnSameErrorRegardlessOfArgPosition(t *testing.T) { func TestHelpFuncExecuted(t *testing.T) { helpText := "Long description" + // Create a context that will be unique, not just the background context + executionCtx, _ := context.WithCancel(context.Background()) + child := &Command{Use: "child", Run: emptyRun} child.SetHelpFunc(func(cmd *Command, args []string) { _, err := cmd.OutOrStdout().Write([]byte(helpText)) @@ -2933,15 +2936,15 @@ func TestHelpFuncExecuted(t *testing.T) { } // Test for https://github.com/spf13/cobra/issues/2240 - if cmd.Context() == nil { - t.Error("Context is nil") + if cmd.Context() != executionCtx { + t.Error("Context doesn't equal the execution context") } }) rootCmd := &Command{Use: "root", Run: emptyRun} rootCmd.AddCommand(child) - output, err := executeCommand(rootCmd, "help", "child") + output, err := executeCommandWithContext(executionCtx, rootCmd, "help", "child") if err != nil { t.Errorf("Unexpected error: %v", err) }