2020-04-24 18:29:30 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-06-18 17:30:17 +02:00
|
|
|
"fmt"
|
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"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2020-06-22 10:12:28 +02:00
|
|
|
// npmMockUtilsBundle for mocking
|
2020-06-18 17:30:17 +02:00
|
|
|
type npmMockUtilsBundle struct {
|
|
|
|
*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
|
2020-06-18 17:30:17 +02:00
|
|
|
func (u *npmMockUtilsBundle) GetExecRunner() npm.ExecRunner {
|
|
|
|
return u.execRunner
|
2020-04-24 18:29:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 10:12:28 +02:00
|
|
|
// newNpmMockUtilsBundle creates an instance of npmMockUtilsBundle
|
2020-06-18 17:30:17 +02:00
|
|
|
func newNpmMockUtilsBundle() npmMockUtilsBundle {
|
|
|
|
utils := npmMockUtilsBundle{FilesMock: &mock.FilesMock{}, execRunner: &mock.ExecMockRunner{}}
|
|
|
|
return utils
|
2020-04-24 18:29:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 10:12:28 +02:00
|
|
|
// npmConfig holds the config parameters needed for checking if the function is called with correct parameters
|
|
|
|
type npmConfig struct {
|
|
|
|
install bool
|
|
|
|
runScripts []string
|
|
|
|
runOptions []string
|
2020-07-09 14:57:41 +02:00
|
|
|
scriptOptions []string
|
2020-06-22 10:12:28 +02:00
|
|
|
virtualFrameBuffer bool
|
2020-07-16 17:16:55 +02:00
|
|
|
excludeList []string
|
2020-11-04 17:20:26 +02:00
|
|
|
packagesList []string
|
2020-06-22 10:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// npmExecutorMock mocking struct
|
2020-06-18 17:30:17 +02:00
|
|
|
type npmExecutorMock struct {
|
|
|
|
utils npmMockUtilsBundle
|
2020-06-22 10:12:28 +02:00
|
|
|
config npmConfig
|
2020-04-24 18:29:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 10:12:28 +02:00
|
|
|
// FindPackageJSONFiles mock implementation
|
2020-06-18 17:30:17 +02:00
|
|
|
func (n *npmExecutorMock) FindPackageJSONFiles() []string {
|
|
|
|
packages, _ := n.utils.Glob("**/package.json")
|
|
|
|
return packages
|
2020-04-24 18:29:30 +02:00
|
|
|
}
|
|
|
|
|
2020-07-16 17:16:55 +02:00
|
|
|
// FindPackageJSONFiles mock implementation
|
|
|
|
func (n *npmExecutorMock) FindPackageJSONFilesWithExcludes(excludeList []string) ([]string, error) {
|
|
|
|
packages, _ := n.utils.Glob("**/package.json")
|
|
|
|
return packages, nil
|
|
|
|
}
|
|
|
|
|
2020-06-22 10:12:28 +02:00
|
|
|
// FindPackageJSONFilesWithScript mock implementation
|
2020-06-18 17:30:17 +02:00
|
|
|
func (n *npmExecutorMock) FindPackageJSONFilesWithScript(packageJSONFiles []string, script string) ([]string, error) {
|
|
|
|
return packageJSONFiles, nil
|
2020-04-24 18:29:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 10:12:28 +02:00
|
|
|
// RunScriptsInAllPackages mock implementation
|
2020-11-04 17:20:26 +02:00
|
|
|
func (n *npmExecutorMock) RunScriptsInAllPackages(runScripts []string, runOptions []string, scriptOptions []string, virtualFrameBuffer bool, excludeList []string, packagesList []string) error {
|
2020-06-22 10:12:28 +02:00
|
|
|
if len(runScripts) != len(n.config.runScripts) {
|
|
|
|
return fmt.Errorf("RunScriptsInAllPackages was called with a different list of runScripts than config.runScripts")
|
2020-06-18 17:30:17 +02:00
|
|
|
}
|
|
|
|
for i, script := range runScripts {
|
2020-06-22 10:12:28 +02:00
|
|
|
if script != n.config.runScripts[i] {
|
|
|
|
return fmt.Errorf("RunScriptsInAllPackages was called with a different list of runScripts than config.runScripts")
|
2020-06-18 17:30:17 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2020-07-09 14:57:41 +02:00
|
|
|
if len(scriptOptions) != len(n.config.scriptOptions) {
|
2020-07-16 17:16:55 +02:00
|
|
|
return fmt.Errorf("RunScriptsInAllPackages was called with a different list of scriptOptions than config.scriptOptions")
|
2020-07-09 14:57:41 +02:00
|
|
|
}
|
|
|
|
|
2020-07-08 11:57:21 +02:00
|
|
|
if len(runOptions) != len(n.config.runOptions) {
|
|
|
|
return fmt.Errorf("RunScriptsInAllPackages was called with a different list of runOptions than config.runOptions")
|
2020-06-18 17:30:17 +02:00
|
|
|
}
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2020-06-22 10:12:28 +02:00
|
|
|
if virtualFrameBuffer != n.config.virtualFrameBuffer {
|
2020-06-18 17:30:17 +02:00
|
|
|
return fmt.Errorf("RunScriptsInAllPackages was called with a different value of virtualFrameBuffer than config.virtualFrameBuffer")
|
|
|
|
}
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2020-07-16 17:16:55 +02:00
|
|
|
if len(excludeList) != len(n.config.excludeList) {
|
|
|
|
return fmt.Errorf("RunScriptsInAllPackages was called with a different value of excludeList than config.excludeList")
|
|
|
|
}
|
|
|
|
|
2020-11-04 17:20:26 +02:00
|
|
|
if len(packagesList) != len(n.config.packagesList) {
|
|
|
|
return fmt.Errorf("RunScriptsInAllPackages was called with a different value of packagesList than config.packagesList")
|
|
|
|
}
|
|
|
|
|
2020-06-18 17:30:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2020-06-22 10:12:28 +02:00
|
|
|
// InstallAllDependencies mock implementation
|
2020-06-18 17:30:17 +02:00
|
|
|
func (n *npmExecutorMock) InstallAllDependencies(packageJSONFiles []string) error {
|
|
|
|
allPackages := n.FindPackageJSONFiles()
|
|
|
|
if len(packageJSONFiles) != len(allPackages) {
|
|
|
|
return fmt.Errorf("packageJSONFiles != n.FindPackageJSONFiles()")
|
|
|
|
}
|
|
|
|
for i, packageJSON := range packageJSONFiles {
|
|
|
|
if packageJSON != allPackages[i] {
|
|
|
|
return fmt.Errorf("InstallAllDependencies was called with a different list of package.json files than result of n.FindPackageJSONFiles()")
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2020-06-22 10:12:28 +02:00
|
|
|
if !n.config.install {
|
|
|
|
return fmt.Errorf("InstallAllDependencies was called but config.install was false")
|
2020-06-18 17:30:17 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-22 10:12:28 +02:00
|
|
|
// SetNpmRegistries mock implementation
|
2020-06-18 17:30:17 +02:00
|
|
|
func (n *npmExecutorMock) SetNpmRegistries() error {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2020-06-18 17:30:17 +02:00
|
|
|
func TestNpmExecuteScripts(t *testing.T) {
|
2020-11-04 17:20:26 +02:00
|
|
|
t.Run("Call with packagesList", func(t *testing.T) {
|
|
|
|
config := npmExecuteScriptsOptions{Install: true, RunScripts: []string{"ci-build", "ci-test"}, BuildDescriptorList: []string{"src/package.json"}}
|
|
|
|
utils := newNpmMockUtilsBundle()
|
|
|
|
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
|
|
|
|
npmExecutor := npmExecutorMock{utils: utils, config: npmConfig{install: config.Install, runScripts: config.RunScripts, packagesList: config.BuildDescriptorList}}
|
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config)
|
|
|
|
|
|
|
|
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/**"}}
|
|
|
|
utils := newNpmMockUtilsBundle()
|
|
|
|
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
|
|
|
|
npmExecutor := npmExecutorMock{utils: utils, config: npmConfig{install: config.Install, runScripts: config.RunScripts, excludeList: config.BuildDescriptorExcludeList}}
|
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config)
|
|
|
|
|
|
|
|
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"}}
|
|
|
|
utils := newNpmMockUtilsBundle()
|
|
|
|
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
|
|
|
|
npmExecutor := npmExecutorMock{utils: utils, config: npmConfig{install: config.Install, runScripts: config.RunScripts, scriptOptions: config.ScriptOptions}}
|
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config)
|
|
|
|
|
|
|
|
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"}}
|
|
|
|
utils := newNpmMockUtilsBundle()
|
|
|
|
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2020-06-22 10:12:28 +02:00
|
|
|
npmExecutor := npmExecutorMock{utils: utils, config: npmConfig{install: config.Install, runScripts: config.RunScripts}}
|
2020-06-18 17:30:17 +02:00
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config)
|
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"}}
|
|
|
|
utils := newNpmMockUtilsBundle()
|
|
|
|
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2020-06-22 10:12:28 +02:00
|
|
|
npmExecutor := npmExecutorMock{utils: utils, config: npmConfig{install: config.Install, runScripts: config.RunScripts}}
|
2020-06-18 17:30:17 +02:00
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config)
|
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}
|
|
|
|
utils := newNpmMockUtilsBundle()
|
|
|
|
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
|
|
|
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2020-06-22 10:12:28 +02:00
|
|
|
npmExecutor := npmExecutorMock{utils: utils, config: npmConfig{install: config.Install, runScripts: config.RunScripts, virtualFrameBuffer: config.VirtualFrameBuffer}}
|
2020-06-18 17:30:17 +02:00
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config)
|
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}
|
|
|
|
|
|
|
|
err := runNpmExecuteScripts(&npmExecutor, &config)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
})
|
2020-04-24 18:29:30 +02:00
|
|
|
}
|