Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type RunOptions struct {
// The options parameter may be nil, in which case default values are used. See [RunOptions] for
// more details.
func Run(ctx context.Context, root *Command, options *RunOptions) error {
if ctx == nil {
ctx = context.Background()
}
if root == nil {
return errors.New("root command is nil")
}
Expand Down
19 changes: 1 addition & 18 deletions run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"bytes"
"context"
"errors"
"flag"
"strings"
"testing"
Expand Down Expand Up @@ -98,23 +97,7 @@ func TestRun(t *testing.T) {
require.Contains(t, err.Error(), `unknown command "verzion". Did you mean one of these?`)
require.Contains(t, err.Error(), ` version`)
})
t.Run("run with nil context", func(t *testing.T) {
t.Parallel()
root := &Command{
Name: "test",
Exec: func(ctx context.Context, s *State) error {
if ctx == nil {
return errors.New("context is nil")
}
return nil
},
}
err := Parse(root, nil)
require.NoError(t, err)
err = Run(nil, root, nil) //nolint:staticcheck // intentionally testing nil context
require.Error(t, err)
require.Contains(t, err.Error(), "context is nil")
})

t.Run("command that panics during execution", func(t *testing.T) {
t.Parallel()
root := &Command{
Expand Down