(t *testing.T)
| 1655 | } |
| 1656 | |
| 1657 | func TestCommand_BeforeFuncPersistentFlag(t *testing.T) { |
| 1658 | counts := &opCounts{} |
| 1659 | beforeError := fmt.Errorf("fail") |
| 1660 | var err error |
| 1661 | |
| 1662 | cmd := &Command{ |
| 1663 | Before: func(_ context.Context, cmd *Command) (context.Context, error) { |
| 1664 | counts.Before++ |
| 1665 | s := cmd.String("opt") |
| 1666 | if s != "value" { |
| 1667 | return nil, beforeError |
| 1668 | } |
| 1669 | return nil, nil |
| 1670 | }, |
| 1671 | Commands: []*Command{ |
| 1672 | { |
| 1673 | Name: "sub", |
| 1674 | Action: func(context.Context, *Command) error { |
| 1675 | counts.SubCommand++ |
| 1676 | return nil |
| 1677 | }, |
| 1678 | }, |
| 1679 | }, |
| 1680 | Flags: []Flag{ |
| 1681 | &StringFlag{Name: "opt"}, |
| 1682 | }, |
| 1683 | Writer: io.Discard, |
| 1684 | } |
| 1685 | |
| 1686 | // Check that --opt value is available in root command Before hook, |
| 1687 | // even when it was set on the subcommand. |
| 1688 | err = cmd.Run(buildTestContext(t), []string{"command", "sub", "--opt", "value"}) |
| 1689 | require.NoError(t, err) |
| 1690 | assert.Equal(t, 1, counts.Before, "Before() not executed when expected") |
| 1691 | assert.Equal(t, 1, counts.SubCommand, "Subcommand not executed when expected") |
| 1692 | } |
| 1693 | |
| 1694 | func TestCommand_BeforeAfterFuncShellCompletion(t *testing.T) { |
| 1695 | t.Skip("TODO: is '--generate-shell-completion' (flag) still supported?") |
nothing calls this directly
no test coverage detected