You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	[refactor] Relocate npmExecutorMock so that it can be used from pkg (#2472)
From the current location inside "cmd" the npmExecutorMock cannot be used from any coding inside "pkg". When we would like to re-use the npm functionality we have also to provide tests and this requires having a mock. In order to be able to use the mock from pkg we move the mock from "cmd" to "pkg" into package "npm". With the shift from package "cmd" to "npm" a lot of fields in the mock has been made public.
This commit is contained in:
		| @@ -33,15 +33,15 @@ func TestNpmExecuteLint(t *testing.T) { | ||||
| 		lintUtils := newLintMockUtilsBundle() | ||||
| 		lintUtils.AddFile("package.json", []byte("{\"scripts\": { \"ci-lint\": \"\" } }")) | ||||
|  | ||||
| 		npmUtils := newNpmMockUtilsBundle() | ||||
| 		npmUtils.execRunner = lintUtils.execRunner | ||||
| 		npmUtils := npm.NewNpmMockUtilsBundle() | ||||
| 		npmUtils.ExecRunner = lintUtils.execRunner | ||||
| 		npmUtils.FilesMock = lintUtils.FilesMock | ||||
|  | ||||
| 		config := npmExecuteLintOptions{ | ||||
| 			FailOnError: true, | ||||
| 		} | ||||
|  | ||||
| 		npmExecutor := npmExecutorMock{utils: npmUtils, config: npmConfig{runScripts: []string{"ci-lint"}, runOptions: []string{"--silent"}}} | ||||
| 		npmExecutor := npm.NpmExecutorMock{Utils: npmUtils, Config: npm.NpmConfig{RunScripts: []string{"ci-lint"}, RunOptions: []string{"--silent"}}} | ||||
| 		err := runNpmExecuteLint(&npmExecutor, &lintUtils, &config) | ||||
|  | ||||
| 		assert.NoError(t, err) | ||||
|   | ||||
| @@ -1,129 +1,37 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"github.com/SAP/jenkins-library/pkg/mock" | ||||
| 	"github.com/SAP/jenkins-library/pkg/npm" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| // npmMockUtilsBundle for mocking | ||||
| type npmMockUtilsBundle struct { | ||||
| // NpmMockUtilsBundle for mocking | ||||
| type NpmMockUtilsBundle struct { | ||||
| 	*mock.FilesMock | ||||
| 	execRunner *mock.ExecMockRunner | ||||
| } | ||||
|  | ||||
| // GetExecRunner return the execRunner mock | ||||
| func (u *npmMockUtilsBundle) GetExecRunner() npm.ExecRunner { | ||||
| func (u *NpmMockUtilsBundle) GetExecRunner() npm.ExecRunner { | ||||
| 	return u.execRunner | ||||
| } | ||||
|  | ||||
| // newNpmMockUtilsBundle creates an instance of npmMockUtilsBundle | ||||
| func newNpmMockUtilsBundle() npmMockUtilsBundle { | ||||
| 	utils := npmMockUtilsBundle{FilesMock: &mock.FilesMock{}, execRunner: &mock.ExecMockRunner{}} | ||||
| // newNpmMockUtilsBundle creates an instance of NpmMockUtilsBundle | ||||
| func newNpmMockUtilsBundle() NpmMockUtilsBundle { | ||||
| 	utils := NpmMockUtilsBundle{FilesMock: &mock.FilesMock{}, execRunner: &mock.ExecMockRunner{}} | ||||
| 	return utils | ||||
| } | ||||
|  | ||||
| // 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 | ||||
| 	scriptOptions      []string | ||||
| 	virtualFrameBuffer bool | ||||
| 	excludeList        []string | ||||
| 	packagesList       []string | ||||
| } | ||||
|  | ||||
| // npmExecutorMock mocking struct | ||||
| type npmExecutorMock struct { | ||||
| 	utils  npmMockUtilsBundle | ||||
| 	config npmConfig | ||||
| } | ||||
|  | ||||
| // FindPackageJSONFiles mock implementation | ||||
| func (n *npmExecutorMock) FindPackageJSONFiles() []string { | ||||
| 	packages, _ := n.utils.Glob("**/package.json") | ||||
| 	return packages | ||||
| } | ||||
|  | ||||
| // FindPackageJSONFiles mock implementation | ||||
| func (n *npmExecutorMock) FindPackageJSONFilesWithExcludes(excludeList []string) ([]string, error) { | ||||
| 	packages, _ := n.utils.Glob("**/package.json") | ||||
| 	return packages, nil | ||||
| } | ||||
|  | ||||
| // FindPackageJSONFilesWithScript mock implementation | ||||
| func (n *npmExecutorMock) FindPackageJSONFilesWithScript(packageJSONFiles []string, script string) ([]string, error) { | ||||
| 	return packageJSONFiles, nil | ||||
| } | ||||
|  | ||||
| // RunScriptsInAllPackages mock implementation | ||||
| func (n *npmExecutorMock) RunScriptsInAllPackages(runScripts []string, runOptions []string, scriptOptions []string, virtualFrameBuffer bool, excludeList []string, packagesList []string) 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") | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if len(scriptOptions) != len(n.config.scriptOptions) { | ||||
| 		return fmt.Errorf("RunScriptsInAllPackages was called with a different list of scriptOptions than config.scriptOptions") | ||||
| 	} | ||||
|  | ||||
| 	if len(runOptions) != len(n.config.runOptions) { | ||||
| 		return fmt.Errorf("RunScriptsInAllPackages was called with a different list of runOptions than config.runOptions") | ||||
| 	} | ||||
|  | ||||
| 	if virtualFrameBuffer != n.config.virtualFrameBuffer { | ||||
| 		return fmt.Errorf("RunScriptsInAllPackages was called with a different value of virtualFrameBuffer than config.virtualFrameBuffer") | ||||
| 	} | ||||
|  | ||||
| 	if len(excludeList) != len(n.config.excludeList) { | ||||
| 		return fmt.Errorf("RunScriptsInAllPackages was called with a different value of excludeList than config.excludeList") | ||||
| 	} | ||||
|  | ||||
| 	if len(packagesList) != len(n.config.packagesList) { | ||||
| 		return fmt.Errorf("RunScriptsInAllPackages was called with a different value of packagesList than config.packagesList") | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // InstallAllDependencies mock implementation | ||||
| 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()") | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if !n.config.install { | ||||
| 		return fmt.Errorf("InstallAllDependencies was called but config.install was false") | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // SetNpmRegistries mock implementation | ||||
| func (n *npmExecutorMock) SetNpmRegistries() error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func TestNpmExecuteScripts(t *testing.T) { | ||||
| 	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 := npm.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}} | ||||
| 		npmExecutor := npm.NpmExecutorMock{Utils: utils, Config: npm.NpmConfig{Install: config.Install, RunScripts: config.RunScripts, PackagesList: config.BuildDescriptorList}} | ||||
| 		err := runNpmExecuteScripts(&npmExecutor, &config) | ||||
|  | ||||
| 		assert.NoError(t, err) | ||||
| @@ -131,11 +39,11 @@ func TestNpmExecuteScripts(t *testing.T) { | ||||
|  | ||||
| 	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 := npm.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}} | ||||
| 		npmExecutor := npm.NpmExecutorMock{Utils: utils, Config: npm.NpmConfig{Install: config.Install, RunScripts: config.RunScripts, ExcludeList: config.BuildDescriptorExcludeList}} | ||||
| 		err := runNpmExecuteScripts(&npmExecutor, &config) | ||||
|  | ||||
| 		assert.NoError(t, err) | ||||
| @@ -143,11 +51,11 @@ func TestNpmExecuteScripts(t *testing.T) { | ||||
|  | ||||
| 	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 := npm.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}} | ||||
| 		npmExecutor := npm.NpmExecutorMock{Utils: utils, Config: npm.NpmConfig{Install: config.Install, RunScripts: config.RunScripts, ScriptOptions: config.ScriptOptions}} | ||||
| 		err := runNpmExecuteScripts(&npmExecutor, &config) | ||||
|  | ||||
| 		assert.NoError(t, err) | ||||
| @@ -155,11 +63,11 @@ 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 := npm.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}} | ||||
| 		npmExecutor := npm.NpmExecutorMock{Utils: utils, Config: npm.NpmConfig{Install: config.Install, RunScripts: config.RunScripts}} | ||||
| 		err := runNpmExecuteScripts(&npmExecutor, &config) | ||||
|  | ||||
| 		assert.NoError(t, err) | ||||
| @@ -167,11 +75,11 @@ func TestNpmExecuteScripts(t *testing.T) { | ||||
|  | ||||
| 	t.Run("Call without install", func(t *testing.T) { | ||||
| 		config := npmExecuteScriptsOptions{Install: true, RunScripts: []string{"ci-build", "ci-test"}} | ||||
| 		utils := newNpmMockUtilsBundle() | ||||
| 		utils := npm.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}} | ||||
| 		npmExecutor := npm.NpmExecutorMock{Utils: utils, Config: npm.NpmConfig{Install: config.Install, RunScripts: config.RunScripts}} | ||||
| 		err := runNpmExecuteScripts(&npmExecutor, &config) | ||||
|  | ||||
| 		assert.NoError(t, err) | ||||
| @@ -179,11 +87,11 @@ func TestNpmExecuteScripts(t *testing.T) { | ||||
|  | ||||
| 	t.Run("Call with virtualFrameBuffer", func(t *testing.T) { | ||||
| 		config := npmExecuteScriptsOptions{Install: true, RunScripts: []string{"ci-build", "ci-test"}, VirtualFrameBuffer: true} | ||||
| 		utils := newNpmMockUtilsBundle() | ||||
| 		utils := npm.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, virtualFrameBuffer: config.VirtualFrameBuffer}} | ||||
| 		npmExecutor := npm.NpmExecutorMock{Utils: utils, Config: npm.NpmConfig{Install: config.Install, RunScripts: config.RunScripts, VirtualFrameBuffer: config.VirtualFrameBuffer}} | ||||
| 		err := runNpmExecuteScripts(&npmExecutor, &config) | ||||
|  | ||||
| 		assert.NoError(t, err) | ||||
|   | ||||
							
								
								
									
										116
									
								
								pkg/npm/mock.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										116
									
								
								pkg/npm/mock.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,116 @@ | ||||
| // +build !release | ||||
|  | ||||
| package npm | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"github.com/SAP/jenkins-library/pkg/mock" | ||||
| ) | ||||
|  | ||||
| // NpmMockUtilsBundle for mocking | ||||
| type NpmMockUtilsBundle struct { | ||||
| 	*mock.FilesMock | ||||
| 	ExecRunner *mock.ExecMockRunner | ||||
| } | ||||
|  | ||||
| // GetExecRunner return the execRunner mock | ||||
| func (u *NpmMockUtilsBundle) GetExecRunner() ExecRunner { | ||||
| 	return u.ExecRunner | ||||
| } | ||||
|  | ||||
| // NewNpmMockUtilsBundle creates an instance of NpmMockUtilsBundle | ||||
| func NewNpmMockUtilsBundle() NpmMockUtilsBundle { | ||||
| 	utils := NpmMockUtilsBundle{FilesMock: &mock.FilesMock{}, ExecRunner: &mock.ExecMockRunner{}} | ||||
| 	return utils | ||||
| } | ||||
|  | ||||
| // 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 | ||||
| 	ScriptOptions      []string | ||||
| 	VirtualFrameBuffer bool | ||||
| 	ExcludeList        []string | ||||
| 	PackagesList       []string | ||||
| } | ||||
|  | ||||
| // NpmExecutorMock mocking struct | ||||
| type NpmExecutorMock struct { | ||||
| 	Utils  NpmMockUtilsBundle | ||||
| 	Config NpmConfig | ||||
| } | ||||
|  | ||||
| // FindPackageJSONFiles mock implementation | ||||
| func (n *NpmExecutorMock) FindPackageJSONFiles() []string { | ||||
| 	packages, _ := n.Utils.Glob("**/package.json") | ||||
| 	return packages | ||||
| } | ||||
|  | ||||
| // FindPackageJSONFiles mock implementation | ||||
| func (n *NpmExecutorMock) FindPackageJSONFilesWithExcludes(excludeList []string) ([]string, error) { | ||||
| 	packages, _ := n.Utils.Glob("**/package.json") | ||||
| 	return packages, nil | ||||
| } | ||||
|  | ||||
| // FindPackageJSONFilesWithScript mock implementation | ||||
| func (n *NpmExecutorMock) FindPackageJSONFilesWithScript(packageJSONFiles []string, script string) ([]string, error) { | ||||
| 	return packageJSONFiles, nil | ||||
| } | ||||
|  | ||||
| // RunScriptsInAllPackages mock implementation | ||||
| func (n *NpmExecutorMock) RunScriptsInAllPackages(runScripts []string, runOptions []string, scriptOptions []string, virtualFrameBuffer bool, excludeList []string, packagesList []string) 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") | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if len(scriptOptions) != len(n.Config.ScriptOptions) { | ||||
| 		return fmt.Errorf("RunScriptsInAllPackages was called with a different list of scriptOptions than config.scriptOptions") | ||||
| 	} | ||||
|  | ||||
| 	if len(runOptions) != len(n.Config.RunOptions) { | ||||
| 		return fmt.Errorf("RunScriptsInAllPackages was called with a different list of runOptions than config.runOptions") | ||||
| 	} | ||||
|  | ||||
| 	if virtualFrameBuffer != n.Config.VirtualFrameBuffer { | ||||
| 		return fmt.Errorf("RunScriptsInAllPackages was called with a different value of virtualFrameBuffer than config.virtualFrameBuffer") | ||||
| 	} | ||||
|  | ||||
| 	if len(excludeList) != len(n.Config.ExcludeList) { | ||||
| 		return fmt.Errorf("RunScriptsInAllPackages was called with a different value of excludeList than config.excludeList") | ||||
| 	} | ||||
|  | ||||
| 	if len(packagesList) != len(n.Config.PackagesList) { | ||||
| 		return fmt.Errorf("RunScriptsInAllPackages was called with a different value of packagesList than config.packagesList") | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // InstallAllDependencies mock implementation | ||||
| 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()") | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if !n.Config.Install { | ||||
| 		return fmt.Errorf("InstallAllDependencies was called but config.install was false") | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // SetNpmRegistries mock implementation | ||||
| func (n *NpmExecutorMock) SetNpmRegistries() error { | ||||
| 	return nil | ||||
| } | ||||
		Reference in New Issue
	
	Block a user