Commit 35cb2511 authored by Mattia Bertorello's avatar Mattia Bertorello

Use a closure to avoid the defer called after the executeWithArgs method

parent 939029ef
......@@ -71,38 +71,40 @@ func executeWithArgs(t *testing.T, args ...string) (int, []byte) {
var output []byte
var exitCode int
fmt.Printf("RUNNING: %s\n", args)
redirect := &stdOutRedirect{}
redirect.Open(t)
defer func() {
output = redirect.GetOutput()
redirect.Close()
fmt.Print(string(output))
fmt.Println()
}()
// Mock the os.Exit function, so that we can use the
// error result for the test and prevent the test from exiting
fakeExitFired := false
fakeExit := func(code int) {
exitCode = code
fakeExitFired = true
// use panic to exit and jump to deferred recover
panic(fmt.Errorf("os.Exit(%d)", code))
}
patch := monkey.Patch(os.Exit, fakeExit)
defer patch.Unpatch()
defer func() {
if fakeExitFired {
recover()
// This closure is here because we won'that the defer are execute at the end of the "executeWithArgs" method
func() {
redirect := &stdOutRedirect{}
redirect.Open(t)
defer func() {
output = redirect.GetOutput()
redirect.Close()
fmt.Print(string(output))
fmt.Println()
}()
// Mock the os.Exit function, so that we can use the
// error result for the test and prevent the test from exiting
fakeExitFired := false
fakeExit := func(code int) {
exitCode = code
fakeExitFired = true
// use panic to exit and jump to deferred recover
panic(fmt.Errorf("os.Exit(%d)", code))
}
}()
patch := monkey.Patch(os.Exit, fakeExit)
defer patch.Unpatch()
defer func() {
if fakeExitFired {
recover()
}
}()
// Execute the CLI command, in this process
cmd := root.Init()
cmd.SetArgs(args)
cmd.Execute()
// Execute the CLI command, in this process
cmd := root.Init()
cmd.SetArgs(args)
cmd.Execute()
}()
return exitCode, output
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment