Skip to content

Commit

Permalink
Fail on error also in case of no lint config present (#4658)
Browse files Browse the repository at this point in the history
* Fail on error also in case of no lint config present

* Fix errors

* test: add unit test
  • Loading branch information
o-liver authored Mar 25, 2024
1 parent 6e8fdb7 commit f6a3bbe
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
18 changes: 14 additions & 4 deletions cmd/npmExecuteLint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
}
Expand Down
35 changes: 35 additions & 0 deletions cmd/npmExecuteLint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

}

0 comments on commit f6a3bbe

Please sign in to comment.