From 9257ba8af3e69fe30bcb9364167fabb738c70efb Mon Sep 17 00:00:00 2001 From: Marco Ferrari Date: Tue, 2 Jan 2024 18:47:45 +0100 Subject: [PATCH] build: use embedded checkstyle configuration files (#5089) checkstyle embeds sun_checks.xml and google_checks.xml so we can use them instead of shipping our own. --- README.md | 2 +- TEMPLATES/sun_checks.xml | 201 ------------------ lib/functions/linterRules.sh | 16 +- .../super-linter/controls/super_linter.rb | 3 +- test/linters/java/java_good_2.java | 5 + 5 files changed, 21 insertions(+), 206 deletions(-) delete mode 100644 TEMPLATES/sun_checks.xml diff --git a/README.md b/README.md index 742da0a3..66a9d7d3 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,7 @@ You can configure super-linter using the following environment variables: | **GITLEAKS_CONFIG_FILE** | `.gitleaks.toml` | Filename for [GitLeaks configuration](https://github.com/zricethezav/gitleaks#configuration) (ex: `.gitleaks.toml`) | | **IGNORE_GENERATED_FILES** | `false` | If set to `true`, super-linter will ignore all the files with `@generated` marker but without `@not-generated` marker. | | **IGNORE_GITIGNORED_FILES** | `false` | If set to `true`, super-linter will ignore all the files that are ignored by Git. | -| **JAVA_FILE_NAME** | `sun_checks.xml` | Filename for [Checkstyle configuration](https://checkstyle.sourceforge.io/config.html) (ex: `checkstyle.xml`) | +| **JAVA_FILE_NAME** | `sun_checks.xml` | Filename for [Checkstyle configuration](https://checkstyle.sourceforge.io/config.html). Checkstyle embeds several configuration files, such as `sun_checks.xml`, `google_checks.xml` that you can use without providing your own configuration file. | | **JAVASCRIPT_DEFAULT_STYLE** | `standard` | Flag to set the default style of JavaScript. Available options: **standard**/**prettier** | | **JAVASCRIPT_ES_CONFIG_FILE** | `.eslintrc.yml` | Filename for [ESLint configuration](https://eslint.org/docs/user-guide/configuring#configuration-file-formats) (ex: `.eslintrc.yml`, `.eslintrc.json`) | | **JSCPD_CONFIG_FILE** | `.jscpd.json` | Filename for JSCPD configuration | diff --git a/TEMPLATES/sun_checks.xml b/TEMPLATES/sun_checks.xml deleted file mode 100644 index 9b68056d..00000000 --- a/TEMPLATES/sun_checks.xml +++ /dev/null @@ -1,201 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/lib/functions/linterRules.sh b/lib/functions/linterRules.sh index 9bc1245f..ed99f17f 100755 --- a/lib/functions/linterRules.sh +++ b/lib/functions/linterRules.sh @@ -161,8 +161,20 @@ GetLinterRules() { # Found the rules file debug " -> ${LANGUAGE_LINTER_RULES} rules file (${!LANGUAGE_LINTER_RULES}) exists." else - # Here we expect a rules file, so fail if not available. - fatal " -> ${LANGUAGE_LINTER_RULES} rules file (${!LANGUAGE_LINTER_RULES}) doesn't exist. Terminating..." + local LANGUAGE_LINTER_RULES_BASENAME + LANGUAGE_LINTER_RULES_BASENAME="$(basename "${!LANGUAGE_LINTER_RULES}")" + debug "LANGUAGE_LINTER_RULES_BASENAME: ${LANGUAGE_LINTER_RULES_BASENAME}" + # checkstyle embeds some configuration files, such as google_checks.xml and sun_checks.xml. + # If we or the user specified one of those files and the file is missing, fall back to + # the embedded one. + if [[ "${LANGUAGE_NAME}" == "JAVA" && ("${LANGUAGE_LINTER_RULES_BASENAME}" == "google_checks.xml" || "${LANGUAGE_LINTER_RULES_BASENAME}" == "sun_checks.xml") ]]; then + debug "${!LANGUAGE_LINTER_RULES} for ${LANGUAGE_NAME} doesn't exist. Falling back to ${LANGUAGE_LINTER_RULES_BASENAME} that the linter ships." + eval "${LANGUAGE_LINTER_RULES}=/${LANGUAGE_LINTER_RULES_BASENAME}" + debug "Updated ${LANGUAGE_LINTER_RULES}: ${!LANGUAGE_LINTER_RULES}" + else + # Here we expect a rules file, so fail if not available. + fatal " -> ${LANGUAGE_LINTER_RULES} rules file (${!LANGUAGE_LINTER_RULES}) doesn't exist. Terminating..." + fi fi ###################### diff --git a/test/inspec/super-linter/controls/super_linter.rb b/test/inspec/super-linter/controls/super_linter.rb index 1e248ba9..118efe7c 100644 --- a/test/inspec/super-linter/controls/super_linter.rb +++ b/test/inspec/super-linter/controls/super_linter.rb @@ -484,8 +484,7 @@ control "super-linter-validate-files" do "/action/lib/.automation/.yaml-lint.yml", "/action/lib/.automation/phpcs.xml", "/action/lib/.automation/phpstan.neon", - "/action/lib/.automation/psalm.xml", - "/action/lib/.automation/sun_checks.xml" + "/action/lib/.automation/psalm.xml" ] files.each do |item| diff --git a/test/linters/java/java_good_2.java b/test/linters/java/java_good_2.java index 376cef1f..95142512 100644 --- a/test/linters/java/java_good_2.java +++ b/test/linters/java/java_good_2.java @@ -1,6 +1,11 @@ @SuppressWarnings("checkstyle:hideutilityclassconstructor") public class Application { + protected StringUtils() { + // prevents calls from subclass + throw new UnsupportedOperationException(); + } + /** * main. *