Skip to content

Commit

Permalink
Fix: Match files_sub_directory as a prefix (#1765)
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 dd8d6f4 commit aed08f6
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 @@ -21,6 +21,7 @@ Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-l
- Add Makefile linter within python flavor ([#1760](https://github.com/oxsecurity/megalinter/issues/1760))
- Set DEFAULT_WORKSPACE as git safe directory per default [#1766](https://github.com/oxsecurity/megalinter/issues/1766)
- Improve documentation for TAP_REPORTER
- Fix: Properly match `files_sub_directory` as a prefix instead of partial string matching

- Linter versions upgrades
- [actionlint](https://rhysd.github.io/actionlint/) from 1.6.15 to **1.6.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] = None,
) -> 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.normpath(file).startswith(os.path.normpath(os.path.join(prefix or "", 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 aed08f6

Please sign in to comment.