Add Command.SetContext (#1551)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Increases flexibility in how Contexts can be used with Cobra.
authored by Joshua Carpeggiani · committed by GitHub · parent 5d066b7 · Browse files · 2 files·+109 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+109 added−0 removed
- other:+109 added−0 removed
Provenance
- Commit
f848943- Tree
86061be- Author
Joshua Carpeggiani <32605850+joshcarp@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
5d066b7
2 files changed+109 added−0 removed
modified:command_test.go+103 added
2056 unmodified lines2057 t.Error("expected unknown flag error")2058 }2059 checkStringContains(t, output, "unknown flag: --unknown")2060 }2061+2062+func TestSetContext(t *testing.T) {2063+ type key struct{}2064+ val := "foobar"2065+ root := &Command{2066+ Use: "root",2067+ Run: func(cmd *Command, args []string) {2068+ key := cmd.Context().Value(key{})2069+ got, ok := key.(string)2070+ if !ok {2071+ t.Error("key not found in context")2072+ }2073+ if got != val {2074+ t.Errorf("Expected value: \n %v\nGot:\n %v\n", val, got)2075+ }2076+ },2077+ }2078+2079+ ctx := context.WithValue(context.Background(), key{}, val)2080+ root.SetContext(ctx)2081+ err := root.Execute()2082+ if err != nil {2083+ t.Error(err)2084+ }2085+}2086+2087+func TestSetContextPreRun(t *testing.T) {2088+ type key struct{}2089+ val := "barr"2090+ root := &Command{2091+ Use: "root",2092+ PreRun: func(cmd *Command, args []string) {2093+ ctx := context.WithValue(cmd.Context(), key{}, val)2094+ cmd.SetContext(ctx)2095+ },2096+ Run: func(cmd *Command, args []string) {2097+ val := cmd.Context().Value(key{})2098+ got, ok := val.(string)2099+ if !ok {2100+ t.Error("key not found in context")2101+ }2102+ if got != val {2103+ t.Errorf("Expected value: \n %v\nGot:\n %v\n", val, got)2104+ }2105+ },2106+ }2107+ err := root.Execute()2108+ if err != nil {2109+ t.Error(err)2110+ }2111+}2112+2113+func TestSetContextPreRunOverwrite(t *testing.T) {2114+ type key struct{}2115+ val := "blah"2116+ root := &Command{2117+ Use: "root",2118+ Run: func(cmd *Command, args []string) {2119+ key := cmd.Context().Value(key{})2120+ _, ok := key.(string)2121+ if ok {2122+ t.Error("key found in context when not expected")2123+ }2124+ },2125+ }2126+ ctx := context.WithValue(context.Background(), key{}, val)2127+ root.SetContext(ctx)2128+ err := root.ExecuteContext(context.Background())2129+ if err != nil {2130+ t.Error(err)2131+ }2132+}2133+2134+func TestSetContextPersistentPreRun(t *testing.T) {2135+ type key struct{}2136+ val := "barbar"2137+ root := &Command{2138+ Use: "root",2139+ PersistentPreRun: func(cmd *Command, args []string) {2140+ ctx := context.WithValue(cmd.Context(), key{}, val)2141+ cmd.SetContext(ctx)2142+ },2143+ }2144+ child := &Command{2145+ Use: "child",2146+ Run: func(cmd *Command, args []string) {2147+ key := cmd.Context().Value(key{})2148+ got, ok := key.(string)2149+ if !ok {2150+ t.Error("key not found in context")2151+ }2152+ if got != val {2153+ t.Errorf("Expected value: \n %v\nGot:\n %v\n", val, got)2154+ }2155+ },2156+ }2157+ root.AddCommand(child)2158+ root.SetArgs([]string{"child"})2159+ err := root.Execute()2160+ if err != nil {2161+ t.Error(err)2162+ }2163+}modified:command.go+6 added
228 unmodified lines229 func (c *Command) Context() context.Context {230 return c.ctx231 }232 233+// SetContext sets context for the command. It is set to context.Background by default and will be overwritten by234+// Command.ExecuteContext or Command.ExecuteContextC235+func (c *Command) SetContext(ctx context.Context) {236+ c.ctx = ctx237+}238+239 // SetArgs sets arguments for the command. It is set to os.Args[1:] by default, if desired, can be overridden240 // particularly useful when testing.241 func (c *Command) SetArgs(a []string) {242 c.args = a1444 unmodified lines