You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-11-06 09:09:19 +02:00
Let the mocks fail if error is provided from a test (#940)
There was some command parsing with failure in case it started with fail. That is IMO less transparent. Now we prepare more explicit with a failure from outside. This enables us to prepare an error like we expect it in the free wild.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/SAP/jenkins-library/pkg/log"
|
"github.com/SAP/jenkins-library/pkg/log"
|
||||||
@@ -28,7 +29,7 @@ func TestRunKarma(t *testing.T) {
|
|||||||
|
|
||||||
opts := karmaExecuteTestsOptions{ModulePath: "./test", InstallCommand: "fail install test", RunCommand: "npm run test"}
|
opts := karmaExecuteTestsOptions{ModulePath: "./test", InstallCommand: "fail install test", RunCommand: "npm run test"}
|
||||||
|
|
||||||
e := execMockRunner{}
|
e := execMockRunner{shouldFailWith: errors.New("error case")}
|
||||||
runKarma(opts, &e)
|
runKarma(opts, &e)
|
||||||
assert.True(t, hasFailed, "expected command to exit with fatal")
|
assert.True(t, hasFailed, "expected command to exit with fatal")
|
||||||
})
|
})
|
||||||
@@ -37,9 +38,9 @@ func TestRunKarma(t *testing.T) {
|
|||||||
var hasFailed bool
|
var hasFailed bool
|
||||||
log.Entry().Logger.ExitFunc = func(int) { hasFailed = true }
|
log.Entry().Logger.ExitFunc = func(int) { hasFailed = true }
|
||||||
|
|
||||||
opts := karmaExecuteTestsOptions{ModulePath: "./test", InstallCommand: "npm install test", RunCommand: "fail run test"}
|
opts := karmaExecuteTestsOptions{ModulePath: "./test", InstallCommand: "npm install test", RunCommand: "npm run test"}
|
||||||
|
|
||||||
e := execMockRunner{}
|
e := execMockRunner{shouldFailWith: errors.New("error case")}
|
||||||
runKarma(opts, &e)
|
runKarma(opts, &e)
|
||||||
assert.True(t, hasFailed, "expected command to exit with fatal")
|
assert.True(t, hasFailed, "expected command to exit with fatal")
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -16,6 +15,7 @@ import (
|
|||||||
type execMockRunner struct {
|
type execMockRunner struct {
|
||||||
dir []string
|
dir []string
|
||||||
calls []execCall
|
calls []execCall
|
||||||
|
shouldFailWith error
|
||||||
}
|
}
|
||||||
|
|
||||||
type execCall struct {
|
type execCall struct {
|
||||||
@@ -26,6 +26,7 @@ type execCall struct {
|
|||||||
type shellMockRunner struct {
|
type shellMockRunner struct {
|
||||||
dir string
|
dir string
|
||||||
calls []string
|
calls []string
|
||||||
|
shouldFailWith error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *execMockRunner) Dir(d string) {
|
func (m *execMockRunner) Dir(d string) {
|
||||||
@@ -33,8 +34,8 @@ func (m *execMockRunner) Dir(d string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *execMockRunner) RunExecutable(e string, p ...string) error {
|
func (m *execMockRunner) RunExecutable(e string, p ...string) error {
|
||||||
if e == "fail" {
|
if m.shouldFailWith != nil {
|
||||||
return fmt.Errorf("error case")
|
return m.shouldFailWith
|
||||||
}
|
}
|
||||||
exec := execCall{exec: e, params: p}
|
exec := execCall{exec: e, params: p}
|
||||||
m.calls = append(m.calls, exec)
|
m.calls = append(m.calls, exec)
|
||||||
@@ -46,6 +47,11 @@ func (m *shellMockRunner) Dir(d string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *shellMockRunner) RunShell(s string, c string) error {
|
func (m *shellMockRunner) RunShell(s string, c string) error {
|
||||||
|
|
||||||
|
if m.shouldFailWith != nil {
|
||||||
|
return m.shouldFailWith
|
||||||
|
}
|
||||||
|
|
||||||
m.calls = append(m.calls, c)
|
m.calls = append(m.calls, c)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user