2019-10-25 14:58:59 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-11-07 10:02:11 +02:00
|
|
|
"errors"
|
2019-10-25 14:58:59 +02:00
|
|
|
"testing"
|
|
|
|
|
2019-11-04 15:43:33 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
2019-10-25 14:58:59 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRunKarma(t *testing.T) {
|
|
|
|
t.Run("success case", func(t *testing.T) {
|
|
|
|
opts := karmaExecuteTestsOptions{ModulePath: "./test", InstallCommand: "npm install test", RunCommand: "npm run test"}
|
|
|
|
|
2019-10-30 10:52:41 +02:00
|
|
|
e := execMockRunner{}
|
2019-11-04 15:43:33 +02:00
|
|
|
runKarma(opts, &e)
|
2019-10-25 14:58:59 +02:00
|
|
|
|
|
|
|
assert.Equal(t, e.dir[0], "./test", "install command dir incorrect")
|
|
|
|
assert.Equal(t, e.calls[0], execCall{exec: "npm", params: []string{"install", "test"}}, "install command/params incorrect")
|
|
|
|
|
|
|
|
assert.Equal(t, e.dir[1], "./test", "run command dir incorrect")
|
|
|
|
assert.Equal(t, e.calls[1], execCall{exec: "npm", params: []string{"run", "test"}}, "run command/params incorrect")
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error case install command", func(t *testing.T) {
|
2019-11-04 15:43:33 +02:00
|
|
|
var hasFailed bool
|
|
|
|
log.Entry().Logger.ExitFunc = func(int) { hasFailed = true }
|
|
|
|
|
2019-10-25 14:58:59 +02:00
|
|
|
opts := karmaExecuteTestsOptions{ModulePath: "./test", InstallCommand: "fail install test", RunCommand: "npm run test"}
|
|
|
|
|
2019-11-07 10:02:11 +02:00
|
|
|
e := execMockRunner{shouldFailWith: errors.New("error case")}
|
2019-11-04 15:43:33 +02:00
|
|
|
runKarma(opts, &e)
|
|
|
|
assert.True(t, hasFailed, "expected command to exit with fatal")
|
2019-10-25 14:58:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("error case run command", func(t *testing.T) {
|
2019-11-04 15:43:33 +02:00
|
|
|
var hasFailed bool
|
|
|
|
log.Entry().Logger.ExitFunc = func(int) { hasFailed = true }
|
|
|
|
|
2019-11-07 10:02:11 +02:00
|
|
|
opts := karmaExecuteTestsOptions{ModulePath: "./test", InstallCommand: "npm install test", RunCommand: "npm run test"}
|
2019-10-25 14:58:59 +02:00
|
|
|
|
2019-11-07 10:02:11 +02:00
|
|
|
e := execMockRunner{shouldFailWith: errors.New("error case")}
|
2019-11-04 15:43:33 +02:00
|
|
|
runKarma(opts, &e)
|
|
|
|
assert.True(t, hasFailed, "expected command to exit with fatal")
|
2019-10-25 14:58:59 +02:00
|
|
|
})
|
|
|
|
}
|