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

Function TestExecuteContext

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

Source from the content-addressed store, hash-verified

175}
176
177func TestExecuteContext(t *testing.T) {
178 ctx := context.TODO()
179
180 ctxRun := func(cmd *Command, args []string) {
181 if cmd.Context() != ctx {
182 t.Errorf("Command %q must have context when called with ExecuteContext", cmd.Use)
183 }
184 }
185
186 rootCmd := &Command{Use: "root", Run: ctxRun, PreRun: ctxRun}
187 childCmd := &Command{Use: "child", Run: ctxRun, PreRun: ctxRun}
188 granchildCmd := &Command{Use: "grandchild", Run: ctxRun, PreRun: ctxRun}
189
190 childCmd.AddCommand(granchildCmd)
191 rootCmd.AddCommand(childCmd)
192
193 if _, err := executeCommandWithContext(ctx, rootCmd, ""); err != nil {
194 t.Errorf("Root command must not fail: %+v", err)
195 }
196
197 if _, err := executeCommandWithContext(ctx, rootCmd, "child"); err != nil {
198 t.Errorf("Subcommand must not fail: %+v", err)
199 }
200
201 if _, err := executeCommandWithContext(ctx, rootCmd, "child", "grandchild"); err != nil {
202 t.Errorf("Command child must not fail: %+v", err)
203 }
204}
205
206func TestExecuteContextC(t *testing.T) {
207 ctx := context.TODO()

Callers

nothing calls this directly

Calls 3

AddCommandMethod · 0.95
ContextMethod · 0.80

Tested by

no test coverage detected