PR updates
This commit is contained in:
parent
2fb0dc188f
commit
c8863d0b30
@ -29,12 +29,10 @@ type Context struct {
|
|||||||
func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
|
func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
|
||||||
c := &Context{App: app, flagSet: set, parentContext: parentCtx}
|
c := &Context{App: app, flagSet: set, parentContext: parentCtx}
|
||||||
if parentCtx != nil {
|
if parentCtx != nil {
|
||||||
if parentCtx.Context == nil {
|
|
||||||
parentCtx.Context = context.Background()
|
|
||||||
}
|
|
||||||
c.Context = parentCtx.Context
|
c.Context = parentCtx.Context
|
||||||
c.shellComplete = parentCtx.shellComplete
|
c.shellComplete = parentCtx.shellComplete
|
||||||
} else {
|
}
|
||||||
|
if c.Context == nil {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
go func() {
|
go func() {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
@ -264,17 +264,20 @@ func TestContext_lookupFlagSet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestContextPropagation tests that
|
func TestNonNilContext(t *testing.T) {
|
||||||
// *cli.Context always has a valid
|
|
||||||
// context.Context
|
|
||||||
func TestContextPropagation(t *testing.T) {
|
|
||||||
ctx := NewContext(nil, nil, nil)
|
ctx := NewContext(nil, nil, nil)
|
||||||
if ctx.Context == nil {
|
if ctx.Context == nil {
|
||||||
t.Fatal("expected a non nil context when no parent is present")
|
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 := NewContext(nil, nil, nil)
|
||||||
parent.Context = context.WithValue(context.Background(), "key", "val")
|
parent.Context = context.WithValue(context.Background(), "key", "val")
|
||||||
ctx = NewContext(nil, nil, parent)
|
ctx := NewContext(nil, nil, parent)
|
||||||
val := ctx.Value("key")
|
val := ctx.Value("key")
|
||||||
if val == nil {
|
if val == nil {
|
||||||
t.Fatal("expected a parent context to be inherited but got 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" {
|
if valstr != "val" {
|
||||||
t.Fatalf("expected the context value to be %q but got %q", "val", valstr)
|
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…
Reference in New Issue
Block a user