forked from containers/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/run, root: Exit with exit code of invoked command
When a command is executed with toolbox run and it returns a non-zero exit code, it is just ignored if that exit code is not handled. This prevents users to identify errors when executing commands in toolbox. With this fix, the exit codes of the invoked command are propagated and returned by 'toolbox run'. This includes even exit codes returned by Podman on error. containers#867
- Loading branch information
1 parent
2af0b30
commit 750db4b
Showing
6 changed files
with
196 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright © 2022 Ondřej Míchal | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func getExitError(err error, rc int) error { | ||
return &exitError{rc, err} | ||
} | ||
|
||
func TestExitError(t *testing.T) { | ||
t.Run("correct error interface implementation", func(t *testing.T) { | ||
var err error = &exitError{0, nil} | ||
assert.Implements(t, (*error)(nil), err) | ||
}) | ||
|
||
testCases := []struct { | ||
name string | ||
err error | ||
rc int | ||
}{ | ||
{ | ||
"errmsg empty; return code 0; casting from Error", | ||
nil, | ||
0, | ||
}, | ||
{ | ||
"errmsg empty; return code > 0; casting from Error", | ||
nil, | ||
42, | ||
}, | ||
{ | ||
"errmsg full; return code 0; casting from Error", | ||
errors.New("this is an error message"), | ||
0, | ||
}, | ||
{ | ||
"errmsg full; return code > 0; casting from Error", | ||
errors.New("this is an error message"), | ||
42, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
err := getExitError(tc.err, tc.rc) | ||
var errExit *exitError | ||
|
||
assert.ErrorAs(t, err, &errExit) | ||
assert.Equal(t, tc.rc, errExit.Code) | ||
if tc.err != nil { | ||
assert.Equal(t, tc.err.Error(), errExit.Error()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters