Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle executable not found in exec #16950

Merged
merged 1 commit into from Nov 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions pkg/util/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"syscall"
)

// ErrExecutableNotFound is returned if the executable is not found.
var ErrExecutableNotFound = osexec.ErrNotFound
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first this bugged me but now I see it does make the code easier to read (osexec.ErrNotFound isn't very descriptive). 👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

osexec.ErrNotFound isn't very descriptive

Yeah. In addition this follows this package's general notion of abstracting away os/exec. Currently users of this package don't have to import os/exec at all.


// Interface is an interface that presents a subset of the os/exec API. Use this
// when you want to inject fakeable/mockable exec behavior.
type Interface interface {
Expand Down Expand Up @@ -81,13 +84,17 @@ func (cmd *cmdWrapper) SetDir(dir string) {
func (cmd *cmdWrapper) CombinedOutput() ([]byte, error) {
out, err := (*osexec.Cmd)(cmd).CombinedOutput()
if err != nil {
ee, ok := err.(*osexec.ExitError)
if !ok {
return out, err
if ee, ok := err.(*osexec.ExitError); ok {
// Force a compile fail if exitErrorWrapper can't convert to ExitError.
var x ExitError = &exitErrorWrapper{ee}
return out, x
}
if ee, ok := err.(*osexec.Error); ok {
if ee.Err == osexec.ErrNotFound {
return out, ErrExecutableNotFound
}
}
// Force a compile fail if exitErrorWrapper can't convert to ExitError.
var x ExitError = &exitErrorWrapper{ee}
return out, x
return out, err
}
return out, nil
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,12 @@ func TestLookPath(t *testing.T) {
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)
}
}