MCPcopy
hub / github.com/urfave/cli / TestCommand_BeforeFuncPersistentFlag

Function TestCommand_BeforeFuncPersistentFlag

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

Source from the content-addressed store, hash-verified

1655}
1656
1657func 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
1694func TestCommand_BeforeAfterFuncShellCompletion(t *testing.T) {
1695 t.Skip("TODO: is '--generate-shell-completion' (flag) still supported?")

Callers

nothing calls this directly

Calls 3

StringMethod · 0.95
RunMethod · 0.95
buildTestContextFunction · 0.85

Tested by

no test coverage detected