1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-09-16 09:26:22 +02:00

Prepare testing command/shell executions (#930)

* Move shell call related mocks to piper_test.go
This commit is contained in:
Marcus Holl
2019-10-30 09:52:41 +01:00
committed by Oliver Nocon
parent a5548ab443
commit 74dd263834
3 changed files with 47 additions and 27 deletions

View File

@@ -4,3 +4,8 @@ type execRunner interface {
RunExecutable(e string, p ...string) error RunExecutable(e string, p ...string) error
Dir(d string) Dir(d string)
} }
type shellRunner interface {
RunShell(s string, c string) error
Dir(d string)
}

View File

@@ -1,40 +1,16 @@
package cmd package cmd
import ( import (
"fmt"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
type mockRunner struct {
dir []string
calls []execCall
}
type execCall struct {
exec string
params []string
}
func (m *mockRunner) Dir(d string) {
m.dir = append(m.dir, d)
}
func (m *mockRunner) RunExecutable(e string, p ...string) error {
if e == "fail" {
return fmt.Errorf("error case")
}
exec := execCall{exec: e, params: p}
m.calls = append(m.calls, exec)
return nil
}
func TestRunKarma(t *testing.T) { func TestRunKarma(t *testing.T) {
t.Run("success case", func(t *testing.T) { t.Run("success case", func(t *testing.T) {
opts := karmaExecuteTestsOptions{ModulePath: "./test", InstallCommand: "npm install test", RunCommand: "npm run test"} opts := karmaExecuteTestsOptions{ModulePath: "./test", InstallCommand: "npm install test", RunCommand: "npm run test"}
e := mockRunner{} e := execMockRunner{}
err := runKarma(opts, &e) err := runKarma(opts, &e)
assert.NoError(t, err, "error occured but no error expected") assert.NoError(t, err, "error occured but no error expected")
@@ -50,7 +26,7 @@ func TestRunKarma(t *testing.T) {
t.Run("error case install command", func(t *testing.T) { t.Run("error case install command", func(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 := mockRunner{} e := execMockRunner{}
err := runKarma(opts, &e) err := runKarma(opts, &e)
assert.Error(t, err, "error expected but none occcured") assert.Error(t, err, "error expected but none occcured")
}) })
@@ -58,7 +34,7 @@ func TestRunKarma(t *testing.T) {
t.Run("error case run command", func(t *testing.T) { t.Run("error case run command", func(t *testing.T) {
opts := karmaExecuteTestsOptions{ModulePath: "./test", InstallCommand: "npm install test", RunCommand: "fail run test"} opts := karmaExecuteTestsOptions{ModulePath: "./test", InstallCommand: "npm install test", RunCommand: "fail run test"}
e := mockRunner{} e := execMockRunner{}
err := runKarma(opts, &e) err := runKarma(opts, &e)
assert.Error(t, err, "error expected but none occcured") assert.Error(t, err, "error expected but none occcured")
}) })

View File

@@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"strings" "strings"
@@ -12,6 +13,44 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
type execMockRunner struct {
dir []string
calls []execCall
}
type execCall struct {
exec string
params []string
}
type shellMockRunner struct {
dir string
calls []string
}
func (m *execMockRunner) Dir(d string) {
m.dir = append(m.dir, d)
}
func (m *execMockRunner) RunExecutable(e string, p ...string) error {
if e == "fail" {
return fmt.Errorf("error case")
}
exec := execCall{exec: e, params: p}
m.calls = append(m.calls, exec)
return nil
}
func(m *shellMockRunner) Dir(d string) {
m.dir = d
}
func(m *shellMockRunner) RunShell(s string, c string) error {
m.calls = append(m.calls, c)
return nil
}
type stepOptions struct { type stepOptions struct {
TestParam string `json:"testParam,omitempty"` TestParam string `json:"testParam,omitempty"`
} }