Skip to content

Commit 7444a39

Browse files
authored
fix: default nil context to context.Background in Run (#9)
1 parent 85eb0e7 commit 7444a39

File tree

2 files changed

+4
-18
lines changed

2 files changed

+4
-18
lines changed

run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ type RunOptions struct {
2828
// The options parameter may be nil, in which case default values are used. See [RunOptions] for
2929
// more details.
3030
func Run(ctx context.Context, root *Command, options *RunOptions) error {
31+
if ctx == nil {
32+
ctx = context.Background()
33+
}
3134
if root == nil {
3235
return errors.New("root command is nil")
3336
}

run_test.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cli
33
import (
44
"bytes"
55
"context"
6-
"errors"
76
"flag"
87
"strings"
98
"testing"
@@ -98,23 +97,7 @@ func TestRun(t *testing.T) {
9897
require.Contains(t, err.Error(), `unknown command "verzion". Did you mean one of these?`)
9998
require.Contains(t, err.Error(), ` version`)
10099
})
101-
t.Run("run with nil context", func(t *testing.T) {
102-
t.Parallel()
103-
root := &Command{
104-
Name: "test",
105-
Exec: func(ctx context.Context, s *State) error {
106-
if ctx == nil {
107-
return errors.New("context is nil")
108-
}
109-
return nil
110-
},
111-
}
112-
err := Parse(root, nil)
113-
require.NoError(t, err)
114-
err = Run(nil, root, nil) //nolint:staticcheck // intentionally testing nil context
115-
require.Error(t, err)
116-
require.Contains(t, err.Error(), "context is nil")
117-
})
100+
118101
t.Run("command that panics during execution", func(t *testing.T) {
119102
t.Parallel()
120103
root := &Command{

0 commit comments

Comments
 (0)