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

Function TestWhenExitSubCommandWithCodeThenCommandQuitUnexpectedly

command_test.go:3154–3200  ·  command_test.go::TestWhenExitSubCommandWithCodeThenCommandQuitUnexpectedly
(t *testing.T)

Source from the content-addressed store, hash-verified

3152}
3153
3154func TestWhenExitSubCommandWithCodeThenCommandQuitUnexpectedly(t *testing.T) {
3155 testCode := 104
3156
3157 cmd := buildMinimalTestCommand()
3158 cmd.Commands = []*Command{
3159 {
3160 Name: "cmd",
3161 Commands: []*Command{
3162 {
3163 Name: "subcmd",
3164 Action: func(context.Context, *Command) error {
3165 return Exit("exit error", testCode)
3166 },
3167 },
3168 },
3169 },
3170 }
3171
3172 // set user function as ExitErrHandler
3173 exitCodeFromExitErrHandler := int(0)
3174 cmd.ExitErrHandler = func(_ context.Context, _ *Command, err error) {
3175 if exitErr, ok := err.(ExitCoder); ok {
3176 exitCodeFromExitErrHandler = exitErr.ExitCode()
3177 }
3178 }
3179
3180 // keep and restore original OsExiter
3181 origExiter := OsExiter
3182 t.Cleanup(func() { OsExiter = origExiter })
3183
3184 // set user function as OsExiter
3185 exitCodeFromOsExiter := int(0)
3186 OsExiter = func(exitCode int) {
3187 exitCodeFromOsExiter = exitCode
3188 }
3189
3190 r := require.New(t)
3191
3192 r.Error(cmd.Run(buildTestContext(t), []string{
3193 "myapp",
3194 "cmd",
3195 "subcmd",
3196 }))
3197
3198 r.Equal(0, exitCodeFromOsExiter)
3199 r.Equal(testCode, exitCodeFromExitErrHandler)
3200}
3201
3202func buildMinimalTestCommand() *Command {
3203 // reset the help flag because tests may have set it

Callers

nothing calls this directly

Calls 6

buildMinimalTestCommandFunction · 0.85
ExitFunction · 0.85
buildTestContextFunction · 0.85
ExitCodeMethod · 0.65
ErrorMethod · 0.45
RunMethod · 0.45

Tested by

no test coverage detected