@@ -71,38 +71,40 @@ func executeWithArgs(t *testing.T, args ...string) (int, []byte) {
7171 var output []byte
7272 var exitCode int
7373 fmt .Printf ("RUNNING: %s\n " , args )
74-
75- redirect := & stdOutRedirect {}
76- redirect .Open (t )
77- defer func () {
78- output = redirect .GetOutput ()
79- redirect .Close ()
80- fmt .Print (string (output ))
81- fmt .Println ()
82- }()
83-
84- // Mock the os.Exit function, so that we can use the
85- // error result for the test and prevent the test from exiting
86- fakeExitFired := false
87- fakeExit := func (code int ) {
88- exitCode = code
89- fakeExitFired = true
90-
91- // use panic to exit and jump to deferred recover
92- panic (fmt .Errorf ("os.Exit(%d)" , code ))
93- }
94- patch := monkey .Patch (os .Exit , fakeExit )
95- defer patch .Unpatch ()
96- defer func () {
97- if fakeExitFired {
98- recover ()
74+ // This closure is here because we won'that the defer are execute at the end of the "executeWithArgs" method
75+ func () {
76+ redirect := & stdOutRedirect {}
77+ redirect .Open (t )
78+ defer func () {
79+ output = redirect .GetOutput ()
80+ redirect .Close ()
81+ fmt .Print (string (output ))
82+ fmt .Println ()
83+ }()
84+
85+ // Mock the os.Exit function, so that we can use the
86+ // error result for the test and prevent the test from exiting
87+ fakeExitFired := false
88+ fakeExit := func (code int ) {
89+ exitCode = code
90+ fakeExitFired = true
91+
92+ // use panic to exit and jump to deferred recover
93+ panic (fmt .Errorf ("os.Exit(%d)" , code ))
9994 }
100- }()
95+ patch := monkey .Patch (os .Exit , fakeExit )
96+ defer patch .Unpatch ()
97+ defer func () {
98+ if fakeExitFired {
99+ recover ()
100+ }
101+ }()
101102
102- // Execute the CLI command, in this process
103- cmd := root .Init ()
104- cmd .SetArgs (args )
105- cmd .Execute ()
103+ // Execute the CLI command, in this process
104+ cmd := root .Init ()
105+ cmd .SetArgs (args )
106+ cmd .Execute ()
107+ }()
106108
107109 return exitCode , output
108110}
0 commit comments