Handle executable not found in exec
This commit is contained in:
		| @@ -21,6 +21,9 @@ import ( | |||||||
| 	"syscall" | 	"syscall" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | // ErrExecutableNotFound is returned if the executable is not found. | ||||||
|  | var ErrExecutableNotFound = osexec.ErrNotFound | ||||||
|  |  | ||||||
| // Interface is an interface that presents a subset of the os/exec API.  Use this | // Interface is an interface that presents a subset of the os/exec API.  Use this | ||||||
| // when you want to inject fakeable/mockable exec behavior. | // when you want to inject fakeable/mockable exec behavior. | ||||||
| type Interface interface { | type Interface interface { | ||||||
| @@ -81,14 +84,18 @@ func (cmd *cmdWrapper) SetDir(dir string) { | |||||||
| func (cmd *cmdWrapper) CombinedOutput() ([]byte, error) { | func (cmd *cmdWrapper) CombinedOutput() ([]byte, error) { | ||||||
| 	out, err := (*osexec.Cmd)(cmd).CombinedOutput() | 	out, err := (*osexec.Cmd)(cmd).CombinedOutput() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		ee, ok := err.(*osexec.ExitError) | 		if ee, ok := err.(*osexec.ExitError); ok { | ||||||
| 		if !ok { |  | ||||||
| 			return out, err |  | ||||||
| 		} |  | ||||||
| 			// Force a compile fail if exitErrorWrapper can't convert to ExitError. | 			// Force a compile fail if exitErrorWrapper can't convert to ExitError. | ||||||
| 			var x ExitError = &exitErrorWrapper{ee} | 			var x ExitError = &exitErrorWrapper{ee} | ||||||
| 			return out, x | 			return out, x | ||||||
| 		} | 		} | ||||||
|  | 		if ee, ok := err.(*osexec.Error); ok { | ||||||
|  | 			if ee.Err == osexec.ErrNotFound { | ||||||
|  | 				return out, ErrExecutableNotFound | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		return out, err | ||||||
|  | 	} | ||||||
| 	return out, nil | 	return out, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -92,3 +92,12 @@ func TestLookPath(t *testing.T) { | |||||||
| 		t.Errorf("unexpected result for LookPath: got %s, expected %s", sh, shExpected) | 		t.Errorf("unexpected result for LookPath: got %s, expected %s", sh, shExpected) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestExecutableNotFound(t *testing.T) { | ||||||
|  | 	exec := New() | ||||||
|  | 	cmd := exec.Command("fake_executable_name") | ||||||
|  | 	_, err := cmd.CombinedOutput() | ||||||
|  | 	if err != ErrExecutableNotFound { | ||||||
|  | 		t.Errorf("Expected error ErrExecutableNotFound but got %v", err) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Sami Wagiaalla
					Sami Wagiaalla