2023-05-03 18:02:11 +02:00
|
|
|
//go:build unit
|
|
|
|
// +build unit
|
|
|
|
|
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"
|
|
|
|
|
2021-04-15 13:58:23 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
|
|
|
|
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) {
|
2021-04-15 13:58:23 +02:00
|
|
|
opts := karmaExecuteTestsOptions{Modules: []string{"./test"}, InstallCommand: "npm install test", RunCommand: "npm run test"}
|
2019-10-25 14:58:59 +02:00
|
|
|
|
2020-02-27 13:11:22 +02:00
|
|
|
e := mock.ExecMockRunner{}
|
2019-11-04 15:43:33 +02:00
|
|
|
runKarma(opts, &e)
|
2019-10-25 14:58:59 +02:00
|
|
|
|
2020-02-27 13:11:22 +02:00
|
|
|
assert.Equal(t, e.Dir[0], "./test", "install command dir incorrect")
|
|
|
|
assert.Equal(t, e.Calls[0], mock.ExecCall{Exec: "npm", Params: []string{"install", "test"}}, "install command/params incorrect")
|
2019-10-25 14:58:59 +02:00
|
|
|
|
2020-02-27 13:11:22 +02:00
|
|
|
assert.Equal(t, e.Dir[1], "./test", "run command dir incorrect")
|
|
|
|
assert.Equal(t, e.Calls[1], mock.ExecCall{Exec: "npm", Params: []string{"run", "test"}}, "run command/params incorrect")
|
2019-10-25 14:58:59 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2023-05-04 09:38:23 +02:00
|
|
|
t.Run("success case - verbose logging", func(t *testing.T) {
|
|
|
|
GeneralConfig.Verbose = true
|
|
|
|
defer func() { GeneralConfig.Verbose = false }()
|
|
|
|
|
|
|
|
opts := karmaExecuteTestsOptions{Modules: []string{"./test"}, InstallCommand: "npm install test", RunCommand: "npm run test"}
|
|
|
|
|
|
|
|
e := mock.ExecMockRunner{}
|
|
|
|
runKarma(opts, &e)
|
|
|
|
|
|
|
|
assert.Equal(t, "./test", e.Dir[1], "run command dir incorrect")
|
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"run", "test", "--", "--log-level", "DEBUG"}}, e.Calls[1], "run command/params incorrect")
|
|
|
|
})
|
|
|
|
|
2019-10-25 14:58:59 +02:00
|
|
|
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 }
|
|
|
|
|
2021-04-15 13:58:23 +02:00
|
|
|
opts := karmaExecuteTestsOptions{Modules: []string{"./test"}, InstallCommand: "fail install test", RunCommand: "npm run test"}
|
2019-10-25 14:58:59 +02:00
|
|
|
|
2020-02-27 13:11:22 +02:00
|
|
|
e := mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{"fail install test": 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 }
|
|
|
|
|
2021-04-15 13:58:23 +02:00
|
|
|
opts := karmaExecuteTestsOptions{Modules: []string{"./test"}, InstallCommand: "npm install test", RunCommand: "npm run test"}
|
2019-10-25 14:58:59 +02:00
|
|
|
|
2020-02-27 13:11:22 +02:00
|
|
|
e := mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{"npm install test": 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
|
|
|
})
|
|
|
|
}
|