From 98e64f4507588a67659d0f9d9e762c0ac4496e0e Mon Sep 17 00:00:00 2001 From: marwan-at-work Date: Tue, 6 Aug 2019 14:04:51 -0400 Subject: [PATCH] add propagation tests --- app_test.go | 5 +---- context_test.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app_test.go b/app_test.go index 796f665..dfb3a88 100644 --- a/app_test.go +++ b/app_test.go @@ -236,7 +236,7 @@ func ExampleApp_Run_shellComplete() { os.Args = []string{"greet", fmt.Sprintf("--%s", genCompName())} app := &App{ - Name: "greet", + Name: "greet", EnableShellCompletion: true, Commands: []*Command{ { @@ -503,7 +503,6 @@ func TestApp_Float64Flag(t *testing.T) { } func TestApp_ParseSliceFlags(t *testing.T) { - var parsedOption, firstArg string var parsedIntSlice []int var parsedStringSlice []string @@ -518,8 +517,6 @@ func TestApp_ParseSliceFlags(t *testing.T) { Action: func(c *Context) error { parsedIntSlice = c.IntSlice("p") parsedStringSlice = c.StringSlice("ip") - parsedOption = c.String("option") - firstArg = c.Args().First() return nil }, }, diff --git a/context_test.go b/context_test.go index 0509488..948af57 100644 --- a/context_test.go +++ b/context_test.go @@ -1,6 +1,7 @@ package cli import ( + "context" "flag" "sort" "testing" @@ -262,3 +263,24 @@ func TestContext_lookupFlagSet(t *testing.T) { t.Fail() } } + +// TestContextPropagation tests that +// *cli.Context always has a valid +// context.Context +func TestContextPropagation(t *testing.T) { + ctx := NewContext(nil, nil, nil) + if ctx.Context == nil { + t.Fatal("expected a non nil context when no parent is present") + } + parent := NewContext(nil, nil, nil) + parent.Context = context.WithValue(context.Background(), "key", "val") + ctx = NewContext(nil, nil, parent) + val := ctx.Value("key") + if val == nil { + t.Fatal("expected a parent context to be inherited but got nil") + } + valstr, _ := val.(string) + if valstr != "val" { + t.Fatalf("expected the context value to be %q but got %q", "val", valstr) + } +}