Skip to content

Commit e6f9d54

Browse files
authored
Add stdout/stderr to the interfaces (shellRunner, execRunner) (#948)
Otherwise we cannot set the streams on the level of the interfaces.
1 parent b6e8987 commit e6f9d54

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

cmd/interfaces.go

+8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package cmd
22

3+
import (
4+
"io"
5+
)
6+
37
type execRunner interface {
48
RunExecutable(e string, p ...string) error
59
Dir(d string)
10+
Stdout(out io.Writer)
11+
Stderr(err io.Writer)
612
}
713

814
type shellRunner interface {
915
RunShell(s string, c string) error
1016
Dir(d string)
17+
Stdout(out io.Writer)
18+
Stderr(err io.Writer)
1119
}

cmd/karmaExecuteTests.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ func karmaExecuteTests(myKarmaExecuteTestsOptions karmaExecuteTestsOptions) erro
1111
c := command.Command{}
1212
// reroute command output to loging framework
1313
// also log stdout as Karma reports into it
14-
c.Stdout = log.Entry().Writer()
15-
c.Stderr = log.Entry().Writer()
14+
c.Stdout(log.Entry().Writer())
15+
c.Stderr(log.Entry().Writer())
1616
runKarma(myKarmaExecuteTestsOptions, &c)
1717
return nil
1818
}

cmd/piper_test.go

+27-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import (
1313
)
1414

1515
type execMockRunner struct {
16-
dir []string
17-
calls []execCall
16+
dir []string
17+
calls []execCall
18+
stdout io.Writer
19+
stderr io.Writer
1820
shouldFailWith error
1921
}
2022

@@ -24,8 +26,10 @@ type execCall struct {
2426
}
2527

2628
type shellMockRunner struct {
27-
dir string
28-
calls []string
29+
dir string
30+
calls []string
31+
stdout io.Writer
32+
stderr io.Writer
2933
shouldFailWith error
3034
}
3135

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

49+
func (m * execMockRunner) Stdout(out io.Writer) {
50+
m.stdout = out
51+
}
52+
53+
54+
func (m * execMockRunner) Stderr(err io.Writer) {
55+
m.stderr = err
56+
}
57+
58+
4559
func (m *shellMockRunner) Dir(d string) {
4660
m.dir = d
4761
}
@@ -56,6 +70,15 @@ func (m *shellMockRunner) RunShell(s string, c string) error {
5670
return nil
5771
}
5872

73+
func (m * shellMockRunner) Stdout(out io.Writer) {
74+
m.stdout = out
75+
}
76+
77+
78+
func (m * shellMockRunner) Stderr(err io.Writer) {
79+
m.stderr = err
80+
}
81+
5982
type stepOptions struct {
6083
TestParam string `json:"testParam,omitempty"`
6184
}

pkg/command/command.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,32 @@ import (
1414
// Command defines the information required for executing a call to any executable
1515
type Command struct {
1616
dir string
17-
Stdout io.Writer
18-
Stderr io.Writer
17+
stdout io.Writer
18+
stderr io.Writer
1919
}
2020

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

26+
// Stdout ..
27+
func (c *Command) Stdout(stdout io.Writer) {
28+
c.stdout = stdout
29+
}
30+
31+
// Stderr ..
32+
func (c *Command) Stderr(stderr io.Writer) {
33+
c.stderr = stderr
34+
}
35+
2636
// ExecCommand defines how to execute os commands
2737
var ExecCommand = exec.Command
2838

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

32-
_out, _err := prepareOut(c.Stdout, c.Stderr)
42+
_out, _err := prepareOut(c.stdout, c.stderr)
3343

3444
cmd := ExecCommand(shell)
3545

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

50-
_out, _err := prepareOut(c.Stdout, c.Stderr)
60+
_out, _err := prepareOut(c.stdout, c.stderr)
5161

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

pkg/command/command_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestShellRun(t *testing.T) {
2626
o := new(bytes.Buffer)
2727
e := new(bytes.Buffer)
2828

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

3232
t.Run("success case", func(t *testing.T) {
@@ -54,7 +54,7 @@ func TestExecutableRun(t *testing.T) {
5454
o := new(bytes.Buffer)
5555
e := new(bytes.Buffer)
5656

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

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

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

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

9898
expectOut := "Test out"
9999
expectErr := "Test err"

0 commit comments

Comments
 (0)