From c8863d0b304ec3a200d6a3a0e12e3e59fcbfdd94 Mon Sep 17 00:00:00 2001 From: marwan-at-work Date: Tue, 6 Aug 2019 14:20:21 -0400 Subject: [PATCH] PR updates --- context.go | 6 ++---- context_test.go | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/context.go b/context.go index 1e5414e..f1a01b4 100644 --- a/context.go +++ b/context.go @@ -29,12 +29,10 @@ type Context struct { func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context { c := &Context{App: app, flagSet: set, parentContext: parentCtx} if parentCtx != nil { - if parentCtx.Context == nil { - parentCtx.Context = context.Background() - } c.Context = parentCtx.Context c.shellComplete = parentCtx.shellComplete - } else { + } + if c.Context == nil { ctx, cancel := context.WithCancel(context.Background()) go func() { defer cancel() diff --git a/context_test.go b/context_test.go index 948af57..7333ae0 100644 --- a/context_test.go +++ b/context_test.go @@ -264,17 +264,20 @@ func TestContext_lookupFlagSet(t *testing.T) { } } -// TestContextPropagation tests that -// *cli.Context always has a valid -// context.Context -func TestContextPropagation(t *testing.T) { +func TestNonNilContext(t *testing.T) { ctx := NewContext(nil, nil, nil) if ctx.Context == nil { t.Fatal("expected a non nil context when no parent is present") } +} + +// TestContextPropagation tests that +// *cli.Context always has a valid +// context.Context +func TestContextPropagation(t *testing.T) { parent := NewContext(nil, nil, nil) parent.Context = context.WithValue(context.Background(), "key", "val") - ctx = NewContext(nil, nil, parent) + ctx := NewContext(nil, nil, parent) val := ctx.Value("key") if val == nil { t.Fatal("expected a parent context to be inherited but got nil") @@ -283,4 +286,10 @@ func TestContextPropagation(t *testing.T) { if valstr != "val" { t.Fatalf("expected the context value to be %q but got %q", "val", valstr) } + parent = NewContext(nil, nil, nil) + parent.Context = nil + ctx = NewContext(nil, nil, parent) + if ctx.Context == nil { + t.Fatal("expected context to not be nil even if the parent's context is nil") + } }