You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-07-01 00:54:55 +02:00
Add Java-/Typescript linting capabilities (#1636)
This change adds linting support for Java-/TypeScript projects based on the checkDefaultLint and checkUserLint steps from Cloud SDK Pipeline. Following options are supported: 1. Define a linting script named ci-lint in the package.json file(s) of the project 2. Provide a configuration for ESLint as part of the project 3. If none of the above: ESLint is executed with a general purpose configuration. Co-authored-by: Daniel Kurzynski <daniel.kurzynski@sap.com>
This commit is contained in:
@ -8,41 +8,56 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// npmMockUtilsBundle for mocking
|
||||
type npmMockUtilsBundle struct {
|
||||
*mock.FilesMock
|
||||
execRunner *mock.ExecMockRunner
|
||||
}
|
||||
|
||||
// GetExecRunner return the execRunner mock
|
||||
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{}}
|
||||
return utils
|
||||
}
|
||||
|
||||
type npmExecutorMock struct {
|
||||
utils npmMockUtilsBundle
|
||||
config npmExecuteScriptsOptions
|
||||
// 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
|
||||
virtualFrameBuffer bool
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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, 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")
|
||||
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 script != n.config.runScripts[i] {
|
||||
return fmt.Errorf("RunScriptsInAllPackages was called with a different list of runScripts than config.runScripts")
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,13 +65,14 @@ func (n *npmExecutorMock) RunScriptsInAllPackages(runScripts []string, runOption
|
||||
return fmt.Errorf("RunScriptsInAllPackages was unexpectedly called with a list of runOptions")
|
||||
}
|
||||
|
||||
if virtualFrameBuffer != n.config.VirtualFrameBuffer {
|
||||
if virtualFrameBuffer != n.config.virtualFrameBuffer {
|
||||
return fmt.Errorf("RunScriptsInAllPackages was called with a different value of virtualFrameBuffer than config.virtualFrameBuffer")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// InstallAllDependencies mock implementation
|
||||
func (n *npmExecutorMock) InstallAllDependencies(packageJSONFiles []string) error {
|
||||
allPackages := n.FindPackageJSONFiles()
|
||||
if len(packageJSONFiles) != len(allPackages) {
|
||||
@ -68,12 +84,13 @@ func (n *npmExecutorMock) InstallAllDependencies(packageJSONFiles []string) erro
|
||||
}
|
||||
}
|
||||
|
||||
if !n.config.Install {
|
||||
return fmt.Errorf("InstallAllDependencies was called but config.Install was false")
|
||||
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
|
||||
}
|
||||
@ -85,7 +102,7 @@ func TestNpmExecuteScripts(t *testing.T) {
|
||||
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
||||
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
||||
|
||||
npmExecutor := npmExecutorMock{utils: utils, config: config}
|
||||
npmExecutor := npmExecutorMock{utils: utils, config: npmConfig{install: config.Install, runScripts: config.RunScripts}}
|
||||
err := runNpmExecuteScripts(&npmExecutor, &config)
|
||||
|
||||
assert.NoError(t, err)
|
||||
@ -97,7 +114,7 @@ func TestNpmExecuteScripts(t *testing.T) {
|
||||
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
||||
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
||||
|
||||
npmExecutor := npmExecutorMock{utils: utils, config: config}
|
||||
npmExecutor := npmExecutorMock{utils: utils, config: npmConfig{install: config.Install, runScripts: config.RunScripts}}
|
||||
err := runNpmExecuteScripts(&npmExecutor, &config)
|
||||
|
||||
assert.NoError(t, err)
|
||||
@ -109,7 +126,7 @@ func TestNpmExecuteScripts(t *testing.T) {
|
||||
utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))
|
||||
utils.AddFile("src/package.json", []byte("{\"name\": \"Test\" }"))
|
||||
|
||||
npmExecutor := npmExecutorMock{utils: utils, config: config}
|
||||
npmExecutor := npmExecutorMock{utils: utils, config: npmConfig{install: config.Install, runScripts: config.RunScripts, virtualFrameBuffer: config.VirtualFrameBuffer}}
|
||||
err := runNpmExecuteScripts(&npmExecutor, &config)
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
Reference in New Issue
Block a user