From ac577a9cfb72e9f51f70d7df731ed1dd57e7ed9f Mon Sep 17 00:00:00 2001 From: Nicolas Vuillamy Date: Sun, 28 May 2023 22:51:06 +0200 Subject: [PATCH] Manage v6 retrocompatiblity with FILTER_REGEX_INCLUDE and FILTER_REGEX_EXCLUDE expression (#2698) * [automation] Auto-update linters version, help and documentation * [MegaLinter] Apply linters fixes * TERRAFORM_TFLINT_SECURED_ENV management * [MegaLinter] Apply linters fixes * Fix FILTER_REGEX_EXCLUDE regression Fixes https://github.com/oxsecurity/megalinter/issues/2697 * [MegaLinter] Apply linters fixes * tflint test class * Build doc * Retrocompatiblity of FILTER_REGEX_EXCLUDE with v6 * label --------- Co-authored-by: nvuillam --- CHANGELOG.md | 8 ++++++-- .../tests/test_megalinter/filters_test.py | 20 +++++++++++++++++++ megalinter/utils.py | 12 +++++++---- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da1c86a9e11..e357afc038a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-linter.yml file, or with `oxsecurity/megalinter:beta` docker image -- Secure PRE_COMMANDS and POST_COMMANDS by default +- Linter enhancements & fixes + - New variable **TERRAFORM_TFLINT_SECURED_ENV** with default value `true`. Set to `false` to allow `tflint --init` to access your env vars. + +- Core + - Secure PRE_COMMANDS and POST_COMMANDS by default - Can be disabled with **secured_env: false** in the command definition -- New variable **TERRAFORM_TFLINT_SECURED_ENV** with default value `true`. Set to `false` to allow `tflint --init` to access your env vars. + - Manage v6 retrocompatibility with FILTER_REGEX_INCLUDE and FILTER_REGEX_EXCLUDE expression - Linter versions upgrades - [checkstyle](https://checkstyle.sourceforge.io) from 10.11.0 to **10.12.0** on 2023-05-27 diff --git a/megalinter/tests/test_megalinter/filters_test.py b/megalinter/tests/test_megalinter/filters_test.py index 845519cd033..16317c483b4 100644 --- a/megalinter/tests/test_megalinter/filters_test.py +++ b/megalinter/tests/test_megalinter/filters_test.py @@ -105,6 +105,26 @@ def test_filter_files_with_file_extensions(self): sorted(filtered_files), sorted(expected), f"check {file_extensions}" ) + def test_filter_regex_exclude_single_level(self): + all_files = [ + "index.html", + "target/index.html", + ] + filtered_files = utils.filter_files( + all_files=all_files, + filter_regex_include=None, + filter_regex_exclude=["(^index.html)"], + file_names_regex=[], + file_extensions=["*"], + ignored_files=[], + ignore_generated_files=False, + ) + self.assertListEqual( + sorted(filtered_files), + sorted(["target/index.html"]), + "check regex_exclude_multilevel", + ) + def test_filter_regex_exclude_multilevel(self): all_files = [ "should/be/excluded/descriptor-level/test.md", diff --git a/megalinter/utils.py b/megalinter/utils.py index 665f1f49d62..00c90312d67 100644 --- a/megalinter/utils.py +++ b/megalinter/utils.py @@ -151,15 +151,19 @@ def filter_files( base_file_name = os.path.basename(file) _, file_extension = os.path.splitext(base_file_name) # Skip according to FILTER_REGEX_INCLUDE - if filter_regex_include_object and not filter_regex_include_object.search( - file_with_workspace + if filter_regex_include_object and ( + not filter_regex_include_object.search(file) + # Compatibility with v6 regexes + and not filter_regex_include_object.search(file_with_workspace) ): continue # Skip according to FILTER_REGEX_EXCLUDE list excluded_by_regex = False for filter_regex_exclude_object in filter_regex_exclude_objects: - if filter_regex_exclude_object and filter_regex_exclude_object.search( - file_with_workspace + if filter_regex_exclude_object and ( + filter_regex_exclude_object.search(file) + # Compatibility with v6 regexes + or filter_regex_exclude_object.search(file_with_workspace) ): excluded_by_regex = True break