MCPcopy
hub / github.com/spf13/cobra / TestSetContextPreRun

Function TestSetContextPreRun

command_test.go:2628–2652  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2626}
2627
2628func TestSetContextPreRun(t *testing.T) {
2629 type key struct{}
2630 val := "barr"
2631 root := &Command{
2632 Use: "root",
2633 PreRun: func(cmd *Command, args []string) {
2634 ctx := context.WithValue(cmd.Context(), key{}, val)
2635 cmd.SetContext(ctx)
2636 },
2637 Run: func(cmd *Command, args []string) {
2638 val := cmd.Context().Value(key{})
2639 got, ok := val.(string)
2640 if !ok {
2641 t.Error("key not found in context")
2642 }
2643 if got != val {
2644 t.Errorf("Expected value: \n %v\nGot:\n %v\n", val, got)
2645 }
2646 },
2647 }
2648 err := root.Execute()
2649 if err != nil {
2650 t.Error(err)
2651 }
2652}
2653
2654func TestSetContextPreRunOverwrite(t *testing.T) {
2655 type key struct{}

Callers

nothing calls this directly

Calls 4

ExecuteMethod · 0.95
ContextMethod · 0.80
SetContextMethod · 0.80
ErrorMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…