1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +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:
Marcus Holl 2019-11-07 09:02:11 +01:00 committed by GitHub
parent 7c5a8a73bc
commit f4aa5fc377
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package cmd
import (
"errors"
"testing"
"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"}
e := execMockRunner{}
e := execMockRunner{shouldFailWith: errors.New("error case")}
runKarma(opts, &e)
assert.True(t, hasFailed, "expected command to exit with fatal")
})
@ -37,9 +38,9 @@ func TestRunKarma(t *testing.T) {
var hasFailed bool
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)
assert.True(t, hasFailed, "expected command to exit with fatal")
})

View File

@ -1,7 +1,6 @@
package cmd
import (
"fmt"
"io"
"io/ioutil"
"strings"
@ -16,6 +15,7 @@ import (
type execMockRunner struct {
dir []string
calls []execCall
shouldFailWith error
}
type execCall struct {
@ -26,6 +26,7 @@ type execCall struct {
type shellMockRunner struct {
dir string
calls []string
shouldFailWith error
}
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 {
if e == "fail" {
return fmt.Errorf("error case")
if m.shouldFailWith != nil {
return m.shouldFailWith
}
exec := execCall{exec: e, params: p}
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 {
if m.shouldFailWith != nil {
return m.shouldFailWith
}
m.calls = append(m.calls, c)
return nil
}