mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-01-04 04:07:16 +02:00
Add stdout/stderr to the interfaces (shellRunner, execRunner) (#948)
Otherwise we cannot set the streams on the level of the interfaces.
This commit is contained in:
parent
b6e8987a31
commit
e6f9d541ca
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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"`
|
||||
}
|
||||
|
@ -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...)
|
||||
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user