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

Function TestExecuteContextC

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

Source from the content-addressed store, hash-verified

204}
205
206func TestExecuteContextC(t *testing.T) {
207 ctx := context.TODO()
208
209 ctxRun := func(cmd *Command, args []string) {
210 if cmd.Context() != ctx {
211 t.Errorf("Command %q must have context when called with ExecuteContext", cmd.Use)
212 }
213 }
214
215 rootCmd := &Command{Use: "root", Run: ctxRun, PreRun: ctxRun}
216 childCmd := &Command{Use: "child", Run: ctxRun, PreRun: ctxRun}
217 granchildCmd := &Command{Use: "grandchild", Run: ctxRun, PreRun: ctxRun}
218
219 childCmd.AddCommand(granchildCmd)
220 rootCmd.AddCommand(childCmd)
221
222 if _, _, err := executeCommandWithContextC(ctx, rootCmd, ""); err != nil {
223 t.Errorf("Root command must not fail: %+v", err)
224 }
225
226 if _, _, err := executeCommandWithContextC(ctx, rootCmd, "child"); err != nil {
227 t.Errorf("Subcommand must not fail: %+v", err)
228 }
229
230 if _, _, err := executeCommandWithContextC(ctx, rootCmd, "child", "grandchild"); err != nil {
231 t.Errorf("Command child must not fail: %+v", err)
232 }
233}
234
235func TestExecute_NoContext(t *testing.T) {
236 run := func(cmd *Command, args []string) {

Callers

nothing calls this directly

Calls 3

AddCommandMethod · 0.95
ContextMethod · 0.80

Tested by

no test coverage detected