Skip to content

Commit 91dc6d7

Browse files
authored
fix: add missing fix mode options and test cases (#5987)
- Add missing fix mode options for: CLANG_FORMAT, ENV, GOOGLE_JAVA_FORMAT, NATURAL_LANGUAGE, PYTHON_ISORT, RUST_CLIPPY. - Refactor linter tests to make them shorter because there's no need to have big test files. - Refactor 'bad' linter tests for linters that support fix mode so they contain only automatically fixable issues. This is needed to avoid adding another set of 'bad' linter tests for fix mode. - Provide configuration files for linters that support fix mode and for which the default configuration is not suitable to enable fix mode: ansible-lint, ESLint, golangci-lint. - Add a test case for linter commands options for linters that support fix mode, to ensure that fix mode and check-only mode options have been defined. - Refactor the fix mode test to check if linters actually applied modifications to files. - Update documentation about adding test cases for linters that support fix mode. - Don't exit with a fatal error if VALIDATE_xxx is false when testing fix mode because not all linters support fix mode. To enable this, set the new FIX_MODE_TEST_CASE_RUN variable to true.
1 parent ea16cd9 commit 91dc6d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+594
-711
lines changed

.github/linters/.eslintrc.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
env:
3+
browser: true
4+
es6: true
5+
jest: true
6+
node: true
7+
8+
extends:
9+
- "eslint:recommended"
10+
11+
ignorePatterns:
12+
- "!.*"
13+
- "**/node_modules/.*"
14+
15+
parser: '@typescript-eslint/parser'
16+
17+
plugins:
18+
- '@typescript-eslint'
19+
20+
# Don't set the jsonSyntax parser option for JSON, JSON5, and JSONC
21+
# so we can use eslint-plugin-jsonc to automatically fix issues
22+
# in tests, otherwise ESLint reports parsing errors and stops
23+
overrides:
24+
- files:
25+
- "*.json"
26+
extends:
27+
- plugin:jsonc/recommended-with-json
28+
parser: jsonc-eslint-parser
29+
- files:
30+
- "*.jsonc"
31+
extends:
32+
- plugin:jsonc/recommended-with-jsonc
33+
parser: jsonc-eslint-parser
34+
- files:
35+
- "*.json5"
36+
extends:
37+
- plugin:jsonc/recommended-with-json5
38+
parser: jsonc-eslint-parser
39+
- files:
40+
- "*.jsx"
41+
- "*.tsx"
42+
extends:
43+
- plugin:react/recommended
44+
...

.github/linters/.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
# This file is only used in tests
3+
# TODO: move in a dedicated directory in test/linters-config
4+
linters:
5+
enable:
6+
- gofmt
7+
...

.github/linters/.jscpd.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"**/test/linters/typescript_es/**",
3939
"**/test/linters/typescript_prettier/**",
4040
"**/test/linters/typescript_standard/**",
41+
"**/test/linters-config/**",
4142
"**/github_conf/**",
4243
"**/workflows/cd.yml",
4344
"**/workflows/ci.yml"

Makefile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
all: info docker test ## Run all targets.
55

66
.PHONY: test
7-
test: info validate-container-image-labels docker-build-check docker-dev-container-build-check test-lib inspec lint-codebase fix-codebase test-default-config-files test-actions-runner-debug test-actions-steps-debug test-runner-debug test-find lint-subset-files test-custom-ssl-cert test-non-default-workdir test-git-flags test-non-default-home-directory test-git-initial-commit test-git-merge-commit-push test-log-level test-use-find-and-ignore-gitignored-files test-linters-expect-failure-log-level-notice test-bash-exec-library-expect-success test-bash-exec-library-expect-failure test-save-super-linter-output test-save-super-linter-output-custom-path test-save-super-linter-custom-summary test-linters test-linters-fix-mode-expect-success ## Run the test suite
7+
test: info validate-container-image-labels docker-build-check docker-dev-container-build-check test-lib inspec lint-codebase fix-codebase test-default-config-files test-actions-runner-debug test-actions-steps-debug test-runner-debug test-find lint-subset-files test-custom-ssl-cert test-non-default-workdir test-git-flags test-non-default-home-directory test-git-initial-commit test-git-merge-commit-push test-log-level test-use-find-and-ignore-gitignored-files test-linters-expect-failure-log-level-notice test-bash-exec-library-expect-success test-bash-exec-library-expect-failure test-save-super-linter-output test-save-super-linter-output-custom-path test-save-super-linter-custom-summary test-linters test-linters-fix-mode ## Run the test suite
88

99
# if this session isn't interactive, then we don't want to allocate a
1010
# TTY, which would fail, but if it is interactive, we do want to attach
@@ -318,6 +318,14 @@ test-globals-languages: ## Test globals/languages.sh
318318
--entrypoint /tmp/lint/test/lib/globalsLanguagesTest.sh \
319319
$(SUPER_LINTER_TEST_CONTAINER_URL)
320320

321+
.PHONY: test-globals-linter-command-options
322+
test-globals-linter-command-options: ## Test globals/LinterCommandsOptions.sh
323+
docker run \
324+
-v "$(CURDIR):/tmp/lint" \
325+
-w /tmp/lint \
326+
--entrypoint /tmp/lint/test/lib/globalsLinterCommandsOptionsTest.sh \
327+
$(SUPER_LINTER_TEST_CONTAINER_URL)
328+
321329
.PHONY: test-linter-rules
322330
test-linter-rules: ## Test linterRules.sh
323331
docker run \
@@ -416,14 +424,13 @@ test-non-default-home-directory: ## Test a non-default HOME directory
416424
"run_test_cases_non_default_home" \
417425
"$(IMAGE)"
418426

419-
.PHONY: test-linters-fix-mode-expect-success
420-
test-linters-fix-mode-expect-success: ## Run the linters test suite (fix mode) expecting successes
427+
.PHONY: test-linters-fix-mode
428+
test-linters-fix-mode: ## Run the linters test suite (fix mode)
421429
$(CURDIR)/test/run-super-linter-tests.sh \
422430
$(SUPER_LINTER_TEST_CONTAINER_URL) \
423431
"run_test_case_fix_mode" \
424432
"$(IMAGE)"
425433

426-
427434
.PHONY: test-linters
428435
test-linters: test-linters-expect-success test-linters-expect-failure ## Run the linters test suite
429436

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ You can configure Super-linter using the following environment variables:
234234
| **FIX_RUST_2015** | `false` | Option to enable fix mode for `RUST_2015`. |
235235
| **FIX_RUST_2018** | `false` | Option to enable fix mode for `RUST_2018`. |
236236
| **FIX_RUST_2021** | `false` | Option to enable fix mode for `RUST_2021`. |
237-
| **FIX_RUST_CLIPPY** | `false` | Option to enable fix mode for `RUST_CLIPPY`. |
237+
| **FIX_RUST_CLIPPY** | `false` | Option to enable fix mode for `RUST_CLIPPY`. When `FIX_RUST_CLIPPY` is `true`, Clippy is allowed to fix issues in the workspace even if there are unstaged and staged changes in the workspace. |
238238
| **FIX_SCALAFMT** | `false` | Option to enable fix mode for `SCALAFMT`. |
239239
| **FIX_SHELL_SHFMT** | `false` | Option to enable fix mode for `SHELL_SHFMT`. |
240240
| **FIX_SNAKEMAKE_SNAKEFMT** | `false` | Option to enable fix mode for `SNAKEMAKE_SNAKEFMT`. |
@@ -421,6 +421,20 @@ Super-linter supports the following locations to deliver fixes:
421421
- If you're running Super-linter locally, you can commit the changes as you
422422
would with any other change in your working directory.
423423

424+
### Fix mode for ansible-lint
425+
426+
ansible-lint requires that the `yaml` rule is enabled to for the ansible-lint
427+
fix mode to work. The default ansible-lint configuration that Super-linter ships
428+
disables the `yaml` rule because it might not be compatible with yamllint. If
429+
you need to enable the ansible-lint fix mode, provide an ansible-lint
430+
configuration that doesn't ignore the `yaml` rule.
431+
432+
### Fix mode file and directory ownership
433+
434+
When fix mode is enabled, some linters and formatters don't maintain the
435+
original file or directory ownership, and use the user that Super-linter uses
436+
to run the linter or formatter.
437+
424438
## Configure linters
425439

426440
Super-linter provides default configurations for some linters in the [`TEMPLATES/`](TEMPLATES/)

docs/add-new-linter.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ new tool, it should include:
77
- `README.md`
88
- Provide test cases:
99

10-
1. Create the `test/linters/<LANGUGAGE>` directory.
10+
1. Create the `test/linters/<LANGUAGE_NAME>` directory.
1111
2. Provide at least one test case with a file that is supposed to pass validation,
12-
with the right file extension if needed: `test/linters/<LANGUAGE>/<name-of-tool>-good`
12+
with the right file extension if needed: `test/linters/<LANGUAGE_NAME>/<name-of-tool>-good`
1313
3. Provide at least one test case with a file that is supposed to fail validation,
14-
with the right file extension if needed: `test/linters/<LANGUAGE>/<name-of-tool>-bad`
14+
with the right file extension if needed: `test/linters/<LANGUAGE_NAME>/<name-of-tool>-bad`.
15+
If the linter supports fix mode, the test case supposed to fail validation
16+
should only contain violations that the fix mode can automatically fix.
17+
Avoid test cases that fail only because of syntax errors, when possible.
18+
4. If the linter supports check-only mode or fix mode, add the `<LANGUGAGE>`
19+
to the `LANGUAGES_WITH_FIX_MODE` array in `test/testUtils.sh`
1520

1621
- Update the test suite to check for installed packages, the commands that your new tool needs in the `PATH`, and the expected version command:
1722

lib/functions/validation.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function ValidateBooleanConfigurationVariables() {
77
ValidateBooleanVariable "DISABLE_ERRORS" "${DISABLE_ERRORS}"
88
ValidateBooleanVariable "ENABLE_GITHUB_ACTIONS_GROUP_TITLE" "${ENABLE_GITHUB_ACTIONS_GROUP_TITLE}"
99
ValidateBooleanVariable "ENABLE_GITHUB_ACTIONS_STEP_SUMMARY" "${ENABLE_GITHUB_ACTIONS_STEP_SUMMARY}"
10+
ValidateBooleanVariable "FIX_MODE_TEST_CASE_RUN" "${FIX_MODE_TEST_CASE_RUN}"
1011
ValidateBooleanVariable "IGNORE_GENERATED_FILES" "${IGNORE_GENERATED_FILES}"
1112
ValidateBooleanVariable "IGNORE_GITIGNORED_FILES" "${IGNORE_GITIGNORED_FILES}"
1213
ValidateBooleanVariable "LOG_DEBUG" "${LOG_DEBUG}"

lib/functions/worker.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ function LintCodebase() {
1515
unset -n VALIDATE_LANGUAGE
1616
return 0
1717
else
18-
fatal "Don't disable any validation when running in test mode. VALIDATE_${FILE_TYPE} is set to: ${VALIDATE_LANGUAGE}. Set it to: true"
18+
if [[ "${FIX_MODE_TEST_CASE_RUN}" == "true" ]]; then
19+
debug "Don't fail the test even if VALIDATE_${FILE_TYPE} is set to ${VALIDATE_LANGUAGE} because ${FILE_TYPE} might not support fix mode"
20+
return 0
21+
else
22+
fatal "Don't disable any validation when running in test mode. VALIDATE_${FILE_TYPE} is set to: ${VALIDATE_LANGUAGE}. Set it to: true"
23+
fi
1924
fi
2025
fi
2126

lib/globals/linterCommandsOptions.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ STANDARD_FIX_MODE_OPTIONS=(--fix)
3535
# Define configuration options to enable "fix mode".
3636
# Not all linters and formatters support this.
3737
ANSIBLE_FIX_MODE_OPTIONS=(--fix)
38+
CLANG_FORMAT_FIX_MODE_OPTIONS=(-i)
3839
CSS_FIX_MODE_OPTIONS=(--fix)
39-
ENV_FIX_MODE_OPTIONS=(fix)
40+
ENV_FIX_MODE_OPTIONS=(fix --no-backup)
4041
GO_FIX_MODE_OPTIONS=("${GOLANGCI_LINT_FIX_MODE_OPTIONS[@]}")
4142
GO_MODULES_FIX_MODE_OPTIONS=("${GOLANGCI_LINT_FIX_MODE_OPTIONS[@]}")
43+
GOOGLE_JAVA_FORMAT_FIX_MODE_OPTIONS=(--replace)
4244
GROOVY_FIX_MODE_OPTIONS=(--fix)
4345
JAVASCRIPT_ES_FIX_MODE_OPTIONS=("${ESLINT_FIX_MODE_OPTIONS[@]}")
4446
JAVASCRIPT_PRETTIER_FIX_MODE_OPTIONS=("${PRETTIER_FIX_MODE_OPTIONS[@]}")
@@ -47,11 +49,13 @@ JSON_FIX_MODE_OPTIONS=("${ESLINT_FIX_MODE_OPTIONS[@]}")
4749
JSONC_FIX_MODE_OPTIONS=("${ESLINT_FIX_MODE_OPTIONS[@]}")
4850
JSX_FIX_MODE_OPTIONS=("${ESLINT_FIX_MODE_OPTIONS[@]}")
4951
MARKDOWN_FIX_MODE_OPTIONS=(--fix)
52+
NATURAL_LANGUAGE_FIX_MODE_OPTIONS=(--fix)
5053
POWERSHELL_FIX_MODE_OPTIONS=(-Fix)
5154
PROTOBUF_FIX_MODE_OPTIONS=(-fix)
55+
PYTHON_ISORT_FIX_MODE_OPTIONS=(--overwrite-in-place)
5256
PYTHON_RUFF_FIX_MODE_OPTIONS=(--fix)
5357
RUBY_FIX_MODE_OPTIONS=(--autocorrect)
54-
RUST_CLIPPY_FIX_MODE_OPTIONS=(--fix)
58+
RUST_CLIPPY_FIX_MODE_OPTIONS=(--fix --allow-dirty --allow-staged)
5559
SHELL_SHFMT_FIX_MODE_OPTIONS=(--write)
5660
SQLFLUFF_FIX_MODE_OPTIONS=(fix)
5761
TSX_FIX_MODE_OPTIONS=("${ESLINT_FIX_MODE_OPTIONS[@]}")

lib/linter.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ declare -l TEST_CASE_RUN
113113
TEST_CASE_RUN="${TEST_CASE_RUN:-"false"}"
114114
export TEST_CASE_RUN
115115

116+
declare -l FIX_MODE_TEST_CASE_RUN
117+
FIX_MODE_TEST_CASE_RUN="${FIX_MODE_TEST_CASE_RUN:-"false"}"
118+
export FIX_MODE_TEST_CASE_RUN
119+
116120
# We want a lowercase value
117121
declare -l USE_FIND_ALGORITHM
118122
USE_FIND_ALGORITHM="${USE_FIND_ALGORITHM:-false}"

0 commit comments

Comments
 (0)