Command.Context() returns nil instead of context.Background()

This commit is contained in:
andreaangiolillo 2022-02-25 15:46:17 +00:00
parent e04ec72550
commit f5f90a22c6
No known key found for this signature in database
GPG key ID: 4AFC548DF62C1DE1
2 changed files with 10 additions and 0 deletions

View file

@ -227,6 +227,9 @@ type Command struct {
// Context returns underlying command context. If command wasn't // Context returns underlying command context. If command wasn't
// executed with ExecuteContext Context returns Background context. // executed with ExecuteContext Context returns Background context.
func (c *Command) Context() context.Context { func (c *Command) Context() context.Context {
if c.ctx == nil {
c.ctx = context.Background()
}
return c.ctx return c.ctx
} }

View file

@ -2058,3 +2058,10 @@ func TestFParseErrWhitelistSiblingCommand(t *testing.T) {
} }
checkStringContains(t, output, "unknown flag: --unknown") checkStringContains(t, output, "unknown flag: --unknown")
} }
func TestContext(t *testing.T) {
root := &Command{}
if root.Context() == nil {
t.Error("expected root.Context() != nil")
}
}