diff --git a/command_test.go b/command_test.go index 263ad82cde..c7cb3e254d 100644 --- a/command_test.go +++ b/command_test.go @@ -906,6 +906,34 @@ func TestCommand_RunDefaultCommand(t *testing.T) { } } +func TestCommand_FlagNamesIncludesParentFlags(t *testing.T) { + var got []string + parent := &Command{ + Name: "parent", + Flags: []Flag{&StringFlag{Name: "parent-flag"}}, + Commands: []*Command{ + { + Name: "child", + Flags: []Flag{&StringFlag{Name: "child-flag"}}, + Action: func(_ context.Context, c *Command) error { + got = c.FlagNames() + return nil + }, + }, + }, + } + + err := parent.Run(buildTestContext(t), []string{"parent", "--parent-flag", "p", "child", "--child-flag", "c"}) + assert.NoError(t, err) + assert.Equal(t, []string{"parent-flag", "child-flag"}, got) +} + +func TestCommand_CountUnknownFlag(t *testing.T) { + cmd := &Command{Name: "app"} + + assert.Equal(t, 0, cmd.Count("missing")) +} + var defaultCommandSubCommandTests = []struct { cmdName string subCmd string diff --git a/errors_test.go b/errors_test.go index 91cf801a36..29e60fb4c2 100644 --- a/errors_test.go +++ b/errors_test.go @@ -122,6 +122,25 @@ func captureExit(err error) (int, bool, string) { // // The test is not parallel because captureExit replaces the package level // OsExiter and ErrWriter, like the rest of this file. +type formattedExitError struct { + msg string + code int +} + +func (e formattedExitError) Error() string { return e.msg } +func (e formattedExitError) ExitCode() int { return e.code } +func (e formattedExitError) Format(s fmt.State, _ rune) { + _, _ = fmt.Fprintf(s, "formatted(%s)", e.msg) +} + +func TestHandleExitCoder_DirectErrorFormatter(t *testing.T) { + code, called, out := captureExit(formattedExitError{msg: "boom", code: 7}) + + assert.True(t, called, "OsExiter was not called for a direct ErrorFormatter") + assert.Equal(t, 7, code) + assert.Equal(t, "formatted(boom)\n", out) +} + func TestHandleExitCoder_WrappedExitCoder(t *testing.T) { t.Run("direct", func(t *testing.T) { code, called, out := captureExit(fmt.Errorf("deploy failed: %w", Exit("galactic perimeter breach", 9)))