(t *testing.T)
| 2408 | } |
| 2409 | |
| 2410 | func TestCommand_HelpFlagTakesPrecedenceOverParseErrors(t *testing.T) { |
| 2411 | t.Run("root command with --help and bad flag", func(t *testing.T) { |
| 2412 | r := require.New(t) |
| 2413 | var buf bytes.Buffer |
| 2414 | var errBuf bytes.Buffer |
| 2415 | |
| 2416 | cmd := &Command{ |
| 2417 | Writer: &buf, |
| 2418 | ErrWriter: &errBuf, |
| 2419 | Action: func(ctx context.Context, cmd *Command) error { |
| 2420 | return nil |
| 2421 | }, |
| 2422 | } |
| 2423 | |
| 2424 | err := cmd.Run(buildTestContext(t), []string{"command", "--help", "--undefined"}) |
| 2425 | r.NoError(err) |
| 2426 | r.Contains(buf.String(), "NAME:") |
| 2427 | r.Contains(buf.String(), "command") |
| 2428 | r.NotContains(errBuf.String(), "Incorrect Usage") |
| 2429 | }) |
| 2430 | |
| 2431 | t.Run("root command with -h and bad flag", func(t *testing.T) { |
| 2432 | r := require.New(t) |
| 2433 | var buf bytes.Buffer |
| 2434 | var errBuf bytes.Buffer |
| 2435 | |
| 2436 | cmd := &Command{ |
| 2437 | Writer: &buf, |
| 2438 | ErrWriter: &errBuf, |
| 2439 | Action: func(ctx context.Context, cmd *Command) error { |
| 2440 | return nil |
| 2441 | }, |
| 2442 | } |
| 2443 | |
| 2444 | err := cmd.Run(buildTestContext(t), []string{"command", "-h", "--undefined"}) |
| 2445 | r.NoError(err) |
| 2446 | r.Contains(buf.String(), "NAME:") |
| 2447 | r.Contains(buf.String(), "command") |
| 2448 | r.NotContains(errBuf.String(), "Incorrect Usage") |
| 2449 | }) |
| 2450 | |
| 2451 | t.Run("subcommand with --help and bad flag", func(t *testing.T) { |
| 2452 | r := require.New(t) |
| 2453 | var buf bytes.Buffer |
| 2454 | var errBuf bytes.Buffer |
| 2455 | |
| 2456 | cmd := &Command{ |
| 2457 | Writer: &buf, |
| 2458 | ErrWriter: &errBuf, |
| 2459 | Action: func(ctx context.Context, cmd *Command) error { |
| 2460 | return nil |
| 2461 | }, |
| 2462 | Commands: []*Command{ |
| 2463 | { |
| 2464 | Name: "foo", |
| 2465 | Action: func(ctx context.Context, cmd *Command) error { |
| 2466 | return nil |
| 2467 | }, |
nothing calls this directly
no test coverage detected