mirror of
https://github.com/spf13/cobra
synced 2025-05-07 22:07:23 +00:00
Create TestSetContext() for testing passing context between hooks
This commit is contained in:
parent
a081152dc5
commit
d0e2899afb
1 changed files with 32 additions and 0 deletions
|
@ -148,6 +148,38 @@ func TestSubcommandExecuteC(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSetContext(t *testing.T) {
|
||||||
|
ctx := context.TODO()
|
||||||
|
|
||||||
|
aKey := "akey"
|
||||||
|
aVal := "aval"
|
||||||
|
|
||||||
|
parentRun := func(cmd *Command, args []string) {
|
||||||
|
ctx := context.WithValue(cmd.Context(), aKey, aVal)
|
||||||
|
cmd.SetContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
childRun := func(cmd *Command, args []string) {
|
||||||
|
if val := cmd.Context().Value(aKey); val != aVal {
|
||||||
|
t.Errorf(`Context attribute not found in child command. Expected: "%+v". Have: "%+v"`, aVal, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rootCmd := &Command{Use: "root", Run: parentRun, PersistentPreRun: parentRun}
|
||||||
|
childCmd := &Command{Use: "child", Run: childRun}
|
||||||
|
|
||||||
|
rootCmd.AddCommand(childCmd)
|
||||||
|
|
||||||
|
if _, err := executeCommandWithContext(ctx, rootCmd, ""); err != nil {
|
||||||
|
t.Errorf("Root command must not fail: %+v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := executeCommandWithContext(ctx, rootCmd, "child"); err != nil {
|
||||||
|
t.Errorf("Subcommand must not fail: %+v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestExecuteContext(t *testing.T) {
|
func TestExecuteContext(t *testing.T) {
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue