From f5f90a22c67b709700247b63cf4e1b94bc598da0 Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Fri, 25 Feb 2022 15:46:17 +0000 Subject: [PATCH] Command.Context() returns nil instead of context.Background() --- command.go | 3 +++ command_test.go | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/command.go b/command.go index 2cc18891..ee34f392 100644 --- a/command.go +++ b/command.go @@ -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 } diff --git a/command_test.go b/command_test.go index ca90e67c..7878c297 100644 --- a/command_test.go +++ b/command_test.go @@ -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") + } +}