(t *testing.T)
| 2626 | } |
| 2627 | |
| 2628 | func 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 | |
| 2654 | func TestSetContextPreRunOverwrite(t *testing.T) { |
| 2655 | type key struct{} |
nothing calls this directly
no test coverage detected
searching dependent graphs…