Skip to content
This repository has been archived by the owner on Mar 11, 2020. It is now read-only.

Commit

Permalink
remove *exec.Cmd as it is already covered by Runner interface + tidy …
Browse files Browse the repository at this point in the history
…up test
  • Loading branch information
imantung committed Jul 28, 2019
1 parent 86162eb commit 2065c62
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
1 change: 1 addition & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
func TestErrors(t *testing.T) {
var errors runn.Errors
errors.Add(fmt.Errorf("error1"))
errors.Add(nil)
errors.Add(fmt.Errorf("error2"))
errors.Add(fmt.Errorf("error3"))

Expand Down
20 changes: 0 additions & 20 deletions executor.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package runn

import (
"bytes"
"fmt"
"os/exec"
)

// Executor do the code statement execution
type Executor struct {
StopWhenError bool
Expand All @@ -31,20 +25,6 @@ func (e Executor) Execute(stmts ...interface{}) (err error) {
}
errs.Add(runErr)
}
case *exec.Cmd:
execCmd := stmt.(*exec.Cmd)
buf := bytes.Buffer{}
execCmd.Stdout = &buf
execCmd.Stderr = &buf

runErr := execCmd.Run()
if err != nil {
runErr = fmt.Errorf("%s: %s", err.Error(), buf.String())
if e.StopWhenError {
return runErr
}
errs.Add(runErr)
}
}
}
if len(errs) > 0 {
Expand Down
24 changes: 18 additions & 6 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type RunnerImplementationWithError struct{}

func (i RunnerImplementationWithError) Run() error { return errors.New("some-error") }
func (i RunnerImplementationWithError) Run() error { return errors.New("some-runner-error") }

type RunnerImplementationNoError struct{}

Expand All @@ -27,12 +27,12 @@ func TestExecutor_All(t *testing.T) {
{
false,
[]interface{}{
RunnerImplementationWithError{},
errors.New("error1"),
RunnerImplementationWithError{},
errors.New("error2"),
exec.Command("wrong-command", "bad-argument"),
},
errors.New("some-error; error1; error2; exec: \"wrong-command\": executable file not found in $PATH"),
errors.New("error1; some-runner-error; error2; exec: \"wrong-command\": executable file not found in $PATH"),
},
{
true,
Expand All @@ -43,15 +43,27 @@ func TestExecutor_All(t *testing.T) {
errors.New("error1"),
},
{
false,
[]interface{}{},
nil,
true,
[]interface{}{
RunnerImplementationWithError{},
errors.New("unreachable-error"),
},
errors.New("some-runner-error"),
},
{
true,
[]interface{}{
exec.Command("wrong-command", "bad-argument"),
errors.New("unreachable-error"),
},
errors.New("exec: \"wrong-command\": executable file not found in $PATH"),
},
{
false,
[]interface{}{},
nil,
},

{
false,
[]interface{}{
Expand Down
1 change: 1 addition & 0 deletions runner.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package runn

// Runner contain run function
type Runner interface {
Run() error
}

0 comments on commit 2065c62

Please sign in to comment.