diff --git a/cmd/interfaces.go b/cmd/interfaces.go index b6c1f196a..7c8704eee 100644 --- a/cmd/interfaces.go +++ b/cmd/interfaces.go @@ -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) } diff --git a/cmd/karmaExecuteTests.go b/cmd/karmaExecuteTests.go index 9d7bfb9ef..8bcac6d99 100644 --- a/cmd/karmaExecuteTests.go +++ b/cmd/karmaExecuteTests.go @@ -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 } diff --git a/cmd/piper_test.go b/cmd/piper_test.go index 8d94cae7e..02d177f01 100644 --- a/cmd/piper_test.go +++ b/cmd/piper_test.go @@ -13,8 +13,10 @@ import ( ) type execMockRunner struct { - dir []string - calls []execCall + dir []string + calls []execCall + stdout io.Writer + stderr io.Writer shouldFailWith error } @@ -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 } @@ -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 } @@ -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"` } diff --git a/pkg/command/command.go b/pkg/command/command.go index 0db3378c8..ed7776627 100644 --- a/pkg/command/command.go +++ b/pkg/command/command.go @@ -14,8 +14,8 @@ 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 @@ -23,13 +23,23 @@ 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) @@ -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...) diff --git a/pkg/command/command_test.go b/pkg/command/command_test.go index c23f034f4..0d80a0594 100644 --- a/pkg/command/command_test.go +++ b/pkg/command/command_test.go @@ -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) { @@ -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) { @@ -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") @@ -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"