Skip to content

Commit

Permalink
Add stdout/stderr to the interfaces (shellRunner, execRunner) (#948)
Browse files Browse the repository at this point in the history
Otherwise we cannot set the streams on the level of the interfaces.
  • Loading branch information
marcusholl authored Nov 19, 2019
1 parent b6e8987 commit e6f9d54
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 15 deletions.
8 changes: 8 additions & 0 deletions cmd/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package cmd

import (
"io"
)

type execRunner interface {
RunExecutable(e string, p ...string) error
Dir(d string)
Stdout(out io.Writer)
Stderr(err io.Writer)
}

type shellRunner interface {
RunShell(s string, c string) error
Dir(d string)
Stdout(out io.Writer)
Stderr(err io.Writer)
}
4 changes: 2 additions & 2 deletions cmd/karmaExecuteTests.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ func karmaExecuteTests(myKarmaExecuteTestsOptions karmaExecuteTestsOptions) erro
c := command.Command{}
// reroute command output to loging framework
// also log stdout as Karma reports into it
c.Stdout = log.Entry().Writer()
c.Stderr = log.Entry().Writer()
c.Stdout(log.Entry().Writer())
c.Stderr(log.Entry().Writer())
runKarma(myKarmaExecuteTestsOptions, &c)
return nil
}
Expand Down
31 changes: 27 additions & 4 deletions cmd/piper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
)

type execMockRunner struct {
dir []string
calls []execCall
dir []string
calls []execCall
stdout io.Writer
stderr io.Writer
shouldFailWith error
}

Expand All @@ -24,8 +26,10 @@ type execCall struct {
}

type shellMockRunner struct {
dir string
calls []string
dir string
calls []string
stdout io.Writer
stderr io.Writer
shouldFailWith error
}

Expand All @@ -42,6 +46,16 @@ func (m *execMockRunner) RunExecutable(e string, p ...string) error {
return nil
}

func (m * execMockRunner) Stdout(out io.Writer) {
m.stdout = out
}


func (m * execMockRunner) Stderr(err io.Writer) {
m.stderr = err
}


func (m *shellMockRunner) Dir(d string) {
m.dir = d
}
Expand All @@ -56,6 +70,15 @@ func (m *shellMockRunner) RunShell(s string, c string) error {
return nil
}

func (m * shellMockRunner) Stdout(out io.Writer) {
m.stdout = out
}


func (m * shellMockRunner) Stderr(err io.Writer) {
m.stderr = err
}

type stepOptions struct {
TestParam string `json:"testParam,omitempty"`
}
Expand Down
18 changes: 14 additions & 4 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,32 @@ import (
// Command defines the information required for executing a call to any executable
type Command struct {
dir string
Stdout io.Writer
Stderr io.Writer
stdout io.Writer
stderr io.Writer
}

// Dir sets the working directory for the execution
func (c *Command) Dir(d string) {
c.dir = d
}

// Stdout ..
func (c *Command) Stdout(stdout io.Writer) {
c.stdout = stdout
}

// Stderr ..
func (c *Command) Stderr(stderr io.Writer) {
c.stderr = stderr
}

// ExecCommand defines how to execute os commands
var ExecCommand = exec.Command

// RunShell runs the specified command on the shell
func (c *Command) RunShell(shell, script string) error {

_out, _err := prepareOut(c.Stdout, c.Stderr)
_out, _err := prepareOut(c.stdout, c.stderr)

cmd := ExecCommand(shell)

Expand All @@ -47,7 +57,7 @@ func (c *Command) RunShell(shell, script string) error {
// RunExecutable runs the specified executable with parameters
func (c *Command) RunExecutable(executable string, params ...string) error {

_out, _err := prepareOut(c.Stdout, c.Stderr)
_out, _err := prepareOut(c.stdout, c.stderr)

cmd := ExecCommand(executable, params...)

Expand Down
10 changes: 5 additions & 5 deletions pkg/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestShellRun(t *testing.T) {
o := new(bytes.Buffer)
e := new(bytes.Buffer)

s := Command{Stdout: o, Stderr: e}
s := Command{stdout: o, stderr: e}
s.RunShell("/bin/bash", "myScript")

t.Run("success case", func(t *testing.T) {
Expand Down Expand Up @@ -54,7 +54,7 @@ func TestExecutableRun(t *testing.T) {
o := new(bytes.Buffer)
e := new(bytes.Buffer)

ex := Command{Stdout: o, Stderr: e}
ex := Command{stdout: o, stderr: e}
ex.RunExecutable("echo", []string{"foo bar", "baz"}...)

t.Run("success case", func(t *testing.T) {
Expand All @@ -78,7 +78,7 @@ func TestPrepareOut(t *testing.T) {

t.Run("os", func(t *testing.T) {
s := Command{}
_out, _err := prepareOut(s.Stdout, s.Stderr)
_out, _err := prepareOut(s.stdout, s.stderr)

if _out != os.Stdout {
t.Errorf("expected out to be os.Stdout")
Expand All @@ -92,8 +92,8 @@ func TestPrepareOut(t *testing.T) {
t.Run("custom", func(t *testing.T) {
o := bytes.NewBufferString("")
e := bytes.NewBufferString("")
s := Command{Stdout: o, Stderr: e}
_out, _err := prepareOut(s.Stdout, s.Stderr)
s := Command{stdout: o, stderr: e}
_out, _err := prepareOut(s.stdout, s.stderr)

expectOut := "Test out"
expectErr := "Test err"
Expand Down

0 comments on commit e6f9d54

Please sign in to comment.