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

Function TestSetContext

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

Source from the content-addressed store, hash-verified

2601}
2602
2603func TestSetContext(t *testing.T) {
2604 type key struct{}
2605 val := "foobar"
2606 root := &Command{
2607 Use: "root",
2608 Run: func(cmd *Command, args []string) {
2609 key := cmd.Context().Value(key{})
2610 got, ok := key.(string)
2611 if !ok {
2612 t.Error("key not found in context")
2613 }
2614 if got != val {
2615 t.Errorf("Expected value: \n %v\nGot:\n %v\n", val, got)
2616 }
2617 },
2618 }
2619
2620 ctx := context.WithValue(context.Background(), key{}, val)
2621 root.SetContext(ctx)
2622 err := root.Execute()
2623 if err != nil {
2624 t.Error(err)
2625 }
2626}
2627
2628func TestSetContextPreRun(t *testing.T) {
2629 type key struct{}

Callers

nothing calls this directly

Calls 4

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

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…