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-18 17:30:17 +02:00
|
|
|
type npmMockUtilsBundle struct {
|
|
|
|
*mock.FilesMock
|
|
|
|
execRunner *mock.ExecMockRunner
|
2020-04-24 18:29:30 +02:00
|
|
|
}
|
|
|
|
|
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-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-18 17:30:17 +02:00
|
|
|
type npmExecutorMock struct {
|
|
|
|
utils npmMockUtilsBundle
|
|
|
|
config npmExecuteScriptsOptions
|
2020-04-24 18:29:30 +02:00
|
|
|
}
|
|
|
|
|
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-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-18 17:30:17 +02:00
|
|
|
func (n *npmExecutorMock) RunScriptsInAllPackages(runScripts []string, runOptions []string, virtualFrameBuffer bool) error {
|
|
|
|
if len(runScripts) != len(n.config.RunScripts) {
|
|
|
|
return fmt.Errorf("RunScriptsInAllPackages was called with a different list of runScripts than config.RunScripts")
|
|
|
|
}
|
|
|
|
for i, script := range runScripts {
|
|
|
|
if script != n.config.RunScripts[i] {
|
|
|
|
return fmt.Errorf("RunScriptsInAllPackages was called with a different list of runScripts than config.RunScripts")
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2020-06-18 17:30:17 +02:00
|
|
|
if len(runOptions) != 0 {
|
|
|
|
return fmt.Errorf("RunScriptsInAllPackages was unexpectedly called with a list of runOptions")
|
|
|
|
}
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2020-06-18 17:30:17 +02:00
|
|
|
if virtualFrameBuffer != n.config.VirtualFrameBuffer {
|
|
|
|
return fmt.Errorf("RunScriptsInAllPackages was called with a different value of virtualFrameBuffer than config.virtualFrameBuffer")
|
|
|
|
}
|
2020-04-24 18:29:30 +02:00
|
|
|
|
2020-06-18 17:30:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-24 18:29:30 +02:00
|
|
|
|
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-18 17:30:17 +02:00
|
|
|
if !n.config.Install {
|
|
|
|
return fmt.Errorf("InstallAllDependencies was called but config.Install was false")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
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-18 17:30:17 +02:00
|
|
|
npmExecutor := npmExecutorMock{utils: utils, config: config}
|
|
|
|
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-18 17:30:17 +02:00
|
|
|
npmExecutor := npmExecutorMock{utils: utils, config: config}
|
|
|
|
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-18 17:30:17 +02:00
|
|
|
npmExecutor := npmExecutorMock{utils: utils, config: config}
|
|
|
|
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-06-18 17:30:17 +02:00
|
|
|
options := npm.ExecutorOptions{SapNpmRegistry: config.SapNpmRegistry, 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) {
|
|
|
|
if assert.Equal(t, 6, len(utils.execRunner.Calls)) {
|
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"config", "get", "registry"}}, utils.execRunner.Calls[0])
|
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"config", "get", "@sap:registry"}}, utils.execRunner.Calls[1])
|
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"ci"}}, utils.execRunner.Calls[2])
|
|
|
|
assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"run", "ci-build"}}, utils.execRunner.Calls[5])
|
|
|
|
}
|
2020-06-16 11:42:51 +02:00
|
|
|
}
|
|
|
|
})
|
2020-04-24 18:29:30 +02:00
|
|
|
}
|