Skip to content

Commit

Permalink
Command.Context() returns nil instead of context.Background()
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo committed Feb 25, 2022
1 parent e04ec72 commit f5f90a2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions command.go
Expand Up @@ -227,6 +227,9 @@ type Command struct {
// Context returns underlying command context. If command wasn't
// executed with ExecuteContext Context returns Background context.
func (c *Command) Context() context.Context {
if c.ctx == nil {
c.ctx = context.Background()
}
return c.ctx
}

Expand Down
7 changes: 7 additions & 0 deletions command_test.go
Expand Up @@ -2058,3 +2058,10 @@ func TestFParseErrWhitelistSiblingCommand(t *testing.T) {
}
checkStringContains(t, output, "unknown flag: --unknown")
}

func TestContext(t *testing.T) {
root := &Command{}
if root.Context() == nil {
t.Error("expected root.Context() != nil")
}
}

0 comments on commit f5f90a2

Please sign in to comment.