2023-05-03 18:02:11 +02:00
|
|
|
//go:build unit
|
|
|
|
// +build unit
|
|
|
|
|
2020-04-24 18:29:30 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-03-04 11:16:59 +02:00
|
|
|
"testing"
|
|
|
|
|
2020-04-24 18:29:30 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
2020-06-18 17:30:17 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/npm"
|
2020-04-24 18:29:30 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-01-04 13:46:14 +02:00
|
|
|
// NpmMockUtilsBundle for mocking
|
|
|
|
type NpmMockUtilsBundle struct {
|
2020-06-18 17:30:17 +02:00
|
|
|
*mock.FilesMock
|
|
|
|
execRunner *mock.ExecMockRunner
|
2020-04-24 18:29:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 10:12:28 +02:00
|
|
|
// GetExecRunner return the execRunner mock
|
2021-01-04 13:46:14 +02:00
|
|
|
func (u *NpmMockUtilsBundle) GetExecRunner() npm.ExecRunner {
|
2020-06-18 17:30:17 +02:00
|
|
|
return u.execRunner
|
2020-04-24 18:29:30 +02:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:46:14 +02:00
|
|
|
// newNpmMockUtilsBundle creates an instance of NpmMockUtilsBundle
|
|
|
|
func newNpmMockUtilsBundle() NpmMockUtilsBundle {
|
|
|
|
utils := NpmMockUtilsBundle{FilesMock: &mock.FilesMock{}, execRunner: &mock.ExecMockRunner{}}
|
2020-06-18 17:30:17 +02:00
|
|
|
return utils
|
2020-04-24 18:29:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-18 17:30:17 +02:00
|
|
|
func TestNpmExecuteScripts(t *testing.T) {
|
2021-11-16 16:02:57 +02:00
|
|
|
cpe := npmExecuteScriptsCommonPipelineEnvironment{}
|
|
|
|
|
2020-11-04 17:20:26 +02:00
|
|
|
t.Run("Call with packagesList", func(t *testing.T) {
|
2022-02-07 15:46:03 +02:00
|
|
|
config := npmExecuteScriptsOptions{Install: true, RunScripts: []string{"ci-build", "ci-test"}, BuildDescriptorList: []string{"package.json", "src/package.json"}}
|
2021-01-04 13:46:14 +02:00
|
|
|
utils := npm.NewNpmMockUtilsBundle()
|
2020-11-04 17:20:26 +02:00
|
|
|
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
|
2021-01-04 13:46:14 +02:00
|
|
|
npmExecutor := npm.NpmExecutorMock{Utils: utils, Config: npm.NpmConfig{Install: config.Install, RunScripts: config.RunScripts, PackagesList: config.BuildDescriptorList}}
|
2021-11-16 16:02:57 +02:00
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config, &cpe)
|
2020-11-04 17:20:26 +02:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2020-07-16 17:16:55 +02:00
|
|
|
t.Run("Call with excludeList", func(t *testing.T) {
|
|
|
|
config := npmExecuteScriptsOptions{Install: true, RunScripts: []string{"ci-build", "ci-test"}, BuildDescriptorExcludeList: []string{"**/path/**"}}
|
2021-01-04 13:46:14 +02:00
|
|
|
utils := npm.NewNpmMockUtilsBundle()
|
2020-07-16 17:16:55 +02:00
|
|
|
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
|
2021-01-04 13:46:14 +02:00
|
|
|
npmExecutor := npm.NpmExecutorMock{Utils: utils, Config: npm.NpmConfig{Install: config.Install, RunScripts: config.RunScripts, ExcludeList: config.BuildDescriptorExcludeList}}
|
2021-11-16 16:02:57 +02:00
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config, &cpe)
|
2020-07-16 17:16:55 +02:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2020-07-09 14:57:41 +02:00
|
|
|
t.Run("Call with scriptOptions", func(t *testing.T) {
|
|
|
|
config := npmExecuteScriptsOptions{Install: true, RunScripts: []string{"ci-build", "ci-test"}, ScriptOptions: []string{"--run"}}
|
2021-01-04 13:46:14 +02:00
|
|
|
utils := npm.NewNpmMockUtilsBundle()
|
2020-07-09 14:57:41 +02:00
|
|
|
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
|
2021-01-04 13:46:14 +02:00
|
|
|
npmExecutor := npm.NpmExecutorMock{Utils: utils, Config: npm.NpmConfig{Install: config.Install, RunScripts: config.RunScripts, ScriptOptions: config.ScriptOptions}}
|
2021-11-16 16:02:57 +02:00
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config, &cpe)
|
2020-07-09 14:57:41 +02:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2020-06-18 17:30:17 +02:00
|
|
|
t.Run("Call with install", func(t *testing.T) {
|
|
|
|
config := npmExecuteScriptsOptions{Install: true, RunScripts: []string{"ci-build", "ci-test"}}
|
2021-01-04 13:46:14 +02:00
|
|
|
utils := npm.NewNpmMockUtilsBundle()
|
2020-06-18 17:30:17 +02:00
|
|
|
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2021-01-04 13:46:14 +02:00
|
|
|
npmExecutor := npm.NpmExecutorMock{Utils: utils, Config: npm.NpmConfig{Install: config.Install, RunScripts: config.RunScripts}}
|
2021-11-16 16:02:57 +02:00
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config, &cpe)
|
2020-04-24 18:29:30 +02:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2020-06-18 17:30:17 +02:00
|
|
|
t.Run("Call without install", func(t *testing.T) {
|
|
|
|
config := npmExecuteScriptsOptions{Install: true, RunScripts: []string{"ci-build", "ci-test"}}
|
2021-01-04 13:46:14 +02:00
|
|
|
utils := npm.NewNpmMockUtilsBundle()
|
2020-06-18 17:30:17 +02:00
|
|
|
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2021-01-04 13:46:14 +02:00
|
|
|
npmExecutor := npm.NpmExecutorMock{Utils: utils, Config: npm.NpmConfig{Install: config.Install, RunScripts: config.RunScripts}}
|
2021-11-16 16:02:57 +02:00
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config, &cpe)
|
2020-04-24 18:29:30 +02:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2020-06-18 17:30:17 +02:00
|
|
|
t.Run("Call with virtualFrameBuffer", func(t *testing.T) {
|
|
|
|
config := npmExecuteScriptsOptions{Install: true, RunScripts: []string{"ci-build", "ci-test"}, VirtualFrameBuffer: true}
|
2021-01-04 13:46:14 +02:00
|
|
|
utils := npm.NewNpmMockUtilsBundle()
|
2020-06-18 17:30:17 +02:00
|
|
|
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2021-01-04 13:46:14 +02:00
|
|
|
npmExecutor := npm.NpmExecutorMock{Utils: utils, Config: npm.NpmConfig{Install: config.Install, RunScripts: config.RunScripts, VirtualFrameBuffer: config.VirtualFrameBuffer}}
|
2021-11-16 16:02:57 +02:00
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config, &cpe)
|
2020-04-24 18:29:30 +02:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2020-06-16 11:42:51 +02:00
|
|
|
|
2020-06-18 17:30:17 +02:00
|
|
|
t.Run("Test integration with npm pkg", func(t *testing.T) {
|
|
|
|
config := npmExecuteScriptsOptions{Install: true, RunScripts: []string{"ci-build"}}
|
2020-06-16 11:42:51 +02:00
|
|
|
|
2020-08-11 15:58:39 +02:00
|
|
|
options := npm.ExecutorOptions{DefaultNpmRegistry: config.DefaultNpmRegistry}
|
2020-06-16 11:42:51 +02:00
|
|
|
|
2020-06-18 17:30:17 +02:00
|
|
|
utils := newNpmMockUtilsBundle()
|
|
|
|
utils.AddFile("package.json", []byte("{\"scripts\": { \"ci-build\": \"\" } }"))
|
|
|
|
utils.AddFile("package-lock.json", []byte(""))
|
|
|
|
|
|
|
|
npmExecutor := npm.Execute{Utils: &utils, Options: options}
|
|
|
|
|
2021-11-16 16:02:57 +02:00
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config, &cpe)
|
2020-06-18 17:30:17 +02:00
|
|
|
|
|
|
|
if assert.NoError(t, err) {
|
2020-08-11 15:58:39 +02:00
|
|
|
if assert.Equal(t, 4, len(utils.execRunner.Calls)) {
|
2020-06-18 17:30:17 +02:00
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"config", "get", "registry"}}, utils.execRunner.Calls[0])
|
2020-08-11 15:58:39 +02:00
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"ci"}}, utils.execRunner.Calls[1])
|
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"run", "ci-build"}}, utils.execRunner.Calls[3])
|
2020-06-18 17:30:17 +02:00
|
|
|
}
|
2020-06-16 11:42:51 +02:00
|
|
|
}
|
|
|
|
})
|
2021-03-04 11:16:59 +02:00
|
|
|
|
|
|
|
t.Run("Call with createBOM", func(t *testing.T) {
|
|
|
|
config := npmExecuteScriptsOptions{CreateBOM: true, RunScripts: []string{"ci-build", "ci-test"}}
|
|
|
|
|
|
|
|
options := npm.ExecutorOptions{DefaultNpmRegistry: config.DefaultNpmRegistry}
|
|
|
|
|
|
|
|
utils := newNpmMockUtilsBundle()
|
|
|
|
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
|
|
|
|
npmExecutor := npm.Execute{Utils: &utils, Options: options}
|
2021-11-16 16:02:57 +02:00
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config, &cpe)
|
2021-03-04 11:16:59 +02:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2020-04-24 18:29:30 +02:00
|
|
|
}
|