github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/process/exit_code_test.go (about) 1 package process 2 3 import ( 4 "os/exec" 5 "runtime" 6 "testing" 7 ) 8 9 // TestIsPOSIXShellInvalidCommand tests that the IsPOSIXShellInvalidCommand 10 // function correctly identifiers an "invalid command" error from a POSIX shell. 11 func TestIsPOSIXShellInvalidCommand(t *testing.T) { 12 // If we're not running in a POSIX environment, then skip this test. I think 13 // that we also have to skip this test in POSIX environments on Windows 14 // (which might be detectable with, e.g., the go-isatty package), because Go 15 // won't be able to find shell paths (e.g. "/bin/sh") due to how it resolves 16 // executable paths. 17 if runtime.GOOS == "windows" { 18 t.Skip() 19 } 20 21 // Attempt to run a command that doesn't exist and verify that it has the 22 // correct error classification. Note that we have to run this inside a 23 // shell, otherwise other errors will crop up before the shell's error. 24 command := exec.Command("/bin/sh", "-c", "/dev/null") 25 if err := command.Run(); err == nil { 26 t.Fatal("expected non-nil error when running invalid command") 27 } else if !IsPOSIXShellInvalidCommand(command.ProcessState) { 28 t.Error("expected POSIX invalid command classification") 29 } 30 } 31 32 // TestIsPOSIXShellCommandNotFound tests that the IsPOSIXShellCommandNotFound 33 // function correctly identifiers a "command not found" error from a POSIX 34 // shell. 35 func TestIsPOSIXShellCommandNotFound(t *testing.T) { 36 // If we're not running in a POSIX environment, then skip this test. I think 37 // that we also have to skip this test in POSIX environments on Windows 38 // (which might be detectable with, e.g., the go-isatty package), because Go 39 // won't be able to find shell paths (e.g. "/bin/sh") due to how it resolves 40 // executable paths. 41 if runtime.GOOS == "windows" { 42 t.Skip() 43 } 44 45 // Attempt to run a command that doesn't exist and verify that it has the 46 // correct error classification. Note that we have to run this inside a 47 // shell, otherwise other errors will crop up before the shell's error. 48 command := exec.Command("/bin/sh", "-c", "mutagen-test-not-exist") 49 if err := command.Run(); err == nil { 50 t.Fatal("expected non-nil error when running non-existent command") 51 } else if !IsPOSIXShellCommandNotFound(command.ProcessState) { 52 t.Error("expected POSIX command not found classification") 53 } 54 }