main
marwan-at-work 5 years ago
parent 2fb0dc188f
commit c8863d0b30

@ -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()

@ -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")
}
}

Loading…
Cancel
Save