(t *testing.T)
| 448 | } |
| 449 | |
| 450 | func TestCompletionRunsBeforeChain(t *testing.T) { |
| 451 | type contextKey struct{} |
| 452 | |
| 453 | out := &bytes.Buffer{} |
| 454 | cmd := &Command{ |
| 455 | EnableShellCompletion: true, |
| 456 | Writer: out, |
| 457 | Before: func(ctx context.Context, cmd *Command) (context.Context, error) { |
| 458 | return context.WithValue(ctx, contextKey{}, "ready"), nil |
| 459 | }, |
| 460 | Commands: []*Command{ |
| 461 | { |
| 462 | Name: "index", |
| 463 | Commands: []*Command{ |
| 464 | { |
| 465 | Name: "show", |
| 466 | ShellComplete: func(ctx context.Context, cmd *Command) { |
| 467 | fmt.Fprintln(cmd.Root().Writer, ctx.Value(contextKey{})) |
| 468 | }, |
| 469 | Action: func(ctx context.Context, cmd *Command) error { return nil }, |
| 470 | }, |
| 471 | }, |
| 472 | }, |
| 473 | }, |
| 474 | } |
| 475 | |
| 476 | r := require.New(t) |
| 477 | r.NoError(cmd.Run(buildTestContext(t), []string{"foo", "index", "show", completionFlag})) |
| 478 | r.Equal("ready\n", out.String()) |
| 479 | } |
| 480 | |
| 481 | func TestCompletionReturnsBeforeError(t *testing.T) { |
| 482 | beforeErr := errors.New("load config") |
nothing calls this directly
no test coverage detected