Skip to content

Commit

Permalink
Fix: Match files_sub_directory as a prefix
Browse files Browse the repository at this point in the history
Properly match `files_sub_directory` as a prefix instead of partial string matching.

Before

- If directory named "files_sub_directory" exists then all files
  matching "files_sub_directory" are selected. For example if
  files_sub_directory is set to "dir" and dir exists under the workspace
  directory then "mode/direct/file.yml" and "new_directory/f.yml" will
  be selected

After

- If directory named "files_sub_directory" exists then all files
  which names start with "files_sub_directory" are selected. For example if
  files_sub_directory is set to "dir" (or "./dir" or "dir/" or "./dir/")
  and dir exists under the workspace directory then "dir/file.yml" will be
  selected but not "new_directory/f.yml" and not "some/dir/f.yml"
  • Loading branch information
giner committed Aug 22, 2022
1 parent b5b7741 commit 7d01b6a
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-l
- Fix version in URL in logs produced by reporters
- Improve documentation for TAP_REPORTER
- Fix flavors suggestions to ignore linters not relevant for such flavor ([#1746](https://github.com/oxsecurity/megalinter/issues/1746))
- Fix: Properly match `files_sub_directory` as a prefix instead of partial string matching

- Linter versions upgrades
- [eslint-plugin-jsonc](https://ota-meshi.github.io/eslint-plugin-jsonc/) from 2.3.1 to **2.4.0** on 2022-08-16
Expand Down
1 change: 1 addition & 0 deletions megalinter/Linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@ def collect_files(self, all_files):
file_contains_regex=self.file_contains_regex,
files_sub_directory=self.files_sub_directory,
lint_all_other_linters_files=self.lint_all_other_linters_files,
prefix=self.workspace,
)
self.files_number = len(self.files)
logging.debug(
Expand Down
3 changes: 2 additions & 1 deletion megalinter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def filter_files(
file_contains_regex: Optional[Sequence[str]] = None,
files_sub_directory: Optional[str] = None,
lint_all_other_linters_files: bool = False,
prefix: Optional[str] = "",
) -> Sequence[str]:
file_extensions = set(file_extensions)
filter_regex_include_object = (
Expand Down Expand Up @@ -114,7 +115,7 @@ def filter_files(
if filter_regex_exclude_object and filter_regex_exclude_object.search(file):
continue
# Skip if file is not in defined files_sub_directory
if files_sub_directory and files_sub_directory not in file:
if files_sub_directory and not os.path.relpath(file, prefix).startswith(os.path.normpath(files_sub_directory) + os.path.sep):
continue

# Skip according to file extension (only if lint_all_other_linter_files is false)
Expand Down

0 comments on commit 7d01b6a

Please sign in to comment.