Skip to content

Commit

Permalink
dir_traversal: Prioritize .exclude rules over .exclude_from rules
Browse files Browse the repository at this point in the history
Ensure rules read from files (i.e. passed to .exclude_from() are
inserted _before_ any rules parsed from patterns passed to .exclude().

Later rules override earlier rules, so this allows a user to override a
pattern from a file with a pattern passed directly on the command line.

For example, we want "--exclude !foo/bar" on the command line to always
override a "foo/*" pattern read from a file.
  • Loading branch information
jherland committed Mar 12, 2024
1 parent a418687 commit d9ed0df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion fawltydeps/dir_traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ def exclude_from(self, file_with_exclude_patterns: Path) -> None:
See .exclude() for details about how each gitignore pattern is used.
"""
logger.debug(f"Reading exclude patterns from {file_with_exclude_patterns}...")
self.exclude_rules.extend(parse_gitignore(file_with_exclude_patterns))
self.exclude_rules = (
list(parse_gitignore(file_with_exclude_patterns)) + self.exclude_rules
)

def is_excluded(self, path: Path, is_dir: bool) -> bool:
"""Check if given path is excluded by any of our exclude rules."""
Expand Down
19 changes: 19 additions & 0 deletions tests/test_dir_traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,25 @@ def on_windows(msg: str) -> Optional[str]:
),
],
),
#
# Testing combination of .exclude() and .exclude_from() patterns
#
DirectoryTraversalVector(
"exclude_patterns_take_precedence_over_exclude_from_patterns",
given=[
File(".gitignore", "foo/*"), # exclude everything inside foo/
File("foo/bar"), # NOT excluded due to exclude_pattern below
File("foo/baz"), # excluded
],
exclude_patterns=[
ExcludePattern("!foo/bar"), # overrides "foo/*" in .gitignore
],
exclude_from=[".gitignore"],
expect=[
ExpectedTraverseStep(".", subdirs=["foo"], files=[".gitignore"]),
ExpectedTraverseStep("foo", files=["bar"], excluded_files=["baz"]),
],
),
]


Expand Down

0 comments on commit d9ed0df

Please sign in to comment.