1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-09-16 09:26:22 +02:00

Fail on error also in case of no lint config present (#4658)

* Fail on error also in case of no lint config present

* Fix errors

* test: add unit test
This commit is contained in:
Oliver Feldmann
2024-03-25 15:13:29 +01:00
committed by GitHub
parent 6e8fdb7979
commit f6a3bbe655
2 changed files with 49 additions and 4 deletions

View File

@@ -166,10 +166,15 @@ func runDefaultLint(npmExecutor npm.Executor, utils lintUtils, failOnError bool,
// install dependencies manually, since npx cannot resolve the dependencies required for general purpose
// ESLint config, e.g., TypeScript ESLint plugin
log.Entry().Info("Run ESLint with general purpose config")
utils.getGeneralPurposeConfig("https://raw.githubusercontent.com/SAP/jenkins-library/master/resources/.eslintrc.json")
generalPurposeLintConfigURI := "https://raw.githubusercontent.com/SAP/jenkins-library/master/resources/.eslintrc.json"
utils.getGeneralPurposeConfig(generalPurposeLintConfigURI)
// Ignore possible errors when invoking ESLint to not fail the pipeline based on linting results
_ = execRunner.RunExecutable("npm", "install", "eslint@^7.0.0", "typescript@^3.7.4", "@typescript-eslint/parser@^3.0.0", "@typescript-eslint/eslint-plugin@^3.0.0")
err = execRunner.RunExecutable("npm", "install", "eslint@^7.0.0", "typescript@^3.7.4", "@typescript-eslint/parser@^3.0.0", "@typescript-eslint/eslint-plugin@^3.0.0")
if err != nil {
if failOnError {
return fmt.Errorf("linter installation failed: %s", err)
}
}
args := prepareArgs([]string{
"--no-install",
@@ -181,7 +186,12 @@ func runDefaultLint(npmExecutor npm.Executor, utils lintUtils, failOnError bool,
"--ignore-pattern", ".eslintrc.js",
}, "./%s", outputFileName)
_ = execRunner.RunExecutable("npx", args...)
err = execRunner.RunExecutable("npx", args...)
if err != nil {
if failOnError {
return fmt.Errorf("lint execution failed. This might be the result of severe linting findings. The lint configuration used can be found here: %s", generalPurposeLintConfigURI)
}
}
}
return nil
}

View File

@@ -418,4 +418,39 @@ func TestNpmExecuteLint(t *testing.T) {
assert.EqualError(t, err, "runScript is not allowed to be empty!")
})
t.Run("Test linter installation failed", func(t *testing.T) {
lintUtils := newLintMockUtilsBundle()
lintUtils.execRunner = &mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{"npm install eslint@^7.0.0 typescript@^3.7.4 @typescript-eslint/parser@^3.0.0 @typescript-eslint/eslint-plugin@^3.0.0": errors.New("exit 1")}}
npmUtils := newNpmMockUtilsBundle()
npmUtils.execRunner = lintUtils.execRunner
npmUtils.FilesMock = lintUtils.FilesMock
config := defaultConfig
config.FailOnError = true
npmExecutor := npm.Execute{Utils: &npmUtils, Options: npm.ExecutorOptions{}}
err := runNpmExecuteLint(&npmExecutor, &lintUtils, &config)
assert.EqualError(t, err, "linter installation failed: exit 1")
})
t.Run("Test npx eslint fail", func(t *testing.T) {
lintUtils := newLintMockUtilsBundle()
lintUtils.execRunner = &mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{"npx --no-install eslint . --ext .js,.jsx,.ts,.tsx -c .pipeline/.eslintrc.json -f checkstyle --ignore-pattern .eslintrc.js -o ./defaultlint.xml": errors.New("exit 1")}}
npmUtils := newNpmMockUtilsBundle()
npmUtils.execRunner = lintUtils.execRunner
npmUtils.FilesMock = lintUtils.FilesMock
config := defaultConfig
config.FailOnError = true
npmExecutor := npm.Execute{Utils: &npmUtils, Options: npm.ExecutorOptions{}}
err := runNpmExecuteLint(&npmExecutor, &lintUtils, &config)
assert.EqualError(t, err, "lint execution failed. This might be the result of severe linting findings. The lint configuration used can be found here: https://raw.githubusercontent.com/SAP/jenkins-library/master/resources/.eslintrc.json")
})
}