On Wed, 1 Jun 2016 23:56:43 +0300
Janne Snabb <
sn...@epipe.com> wrote:
> You should check if the type of err is *exec.ExitError and in that
> case consider it just as non-zero exit value from the process.
>
> The following snippet from
>
https://golang.org/src/os/exec/exec_test.go#L123 clarifies it:
>
> func TestExitStatus(t *testing.T) {
> // Test that exit values are returned correctly
> cmd := helperCommand(t, "exit", "42")
> err := cmd.Run()
> want := "exit status 42"
>
> if werr, ok := err.(*exec.ExitError); ok {
> if s := werr.Error(); s != want {
> t.Errorf("from exit 42 got exit %q, want %q",
> s, want) }
> } else {
> t.Fatalf("expected *exec.ExitError from exit 42; got %
> T: %v", err, err) }
> }
I'd warn the OP to take this snippet with a hefty grain of salt: while
it's perfectly reasonable for the test suite of an stdlib package to
test the output of os/exec.ExitError.Error(), 3rd-party code must not
rely on the output of the Error() methods of error values of any type to
be predictable and stable -- at least until there's absolutely no
other way to get onto error's details.
That is, the OP should consider writing a helper function to test the
error returned by os/exec.Cmd.Run() et al to check whether the exit
status is non-zero and discard the error value if so.
An example:
----8<----
package main
import (
"os/exec"
"testing"
)
func maybeIgnore(err error) error {
if _, ok := err.(*exec.ExitError); ok {
return nil
}
return err
}
func TestExitCode(t *testing.T) {
cmd := exec.Command("/bin/sh", "-c", "exit 42")
err := maybeIgnore(cmd.Run())
if err != nil {
t.Error("Expected nil error, got: %#v", err)
}
cmd = exec.Command("./does not exist, really")
err = maybeIgnore(cmd.Run())
if err == nil {
t.Error("Expected non-nil error, got nil")
}
}
----8<----
(Save as "whatever_test.go" and run `go test`.)