Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions airflow-core/src/airflow/utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,21 @@ def compile(pattern: str, base_dir: Path, definition_file: Path) -> _IgnoreRule
def match(path: Path, rules: list[_IgnoreRule]) -> bool:
"""Match a list of ignore rules against the supplied path, accounting for exclusion rules and ordering."""
matched = False
has_negation = False
for rule in rules:
if not isinstance(rule, _GlobIgnoreRule):
raise ValueError(f"_GlobIgnoreRule cannot match rules of type: {type(rule)}")

rel_path = str(path.relative_to(rule.relative_to) if rule.relative_to else path.name)
if (
rule.wild_match_pattern.include is not None
and rule.wild_match_pattern.match_file(rel_path) is not None
):
matched = rule.wild_match_pattern.include
if rule.wild_match_pattern.include is not None:
if rule.wild_match_pattern.match_file(rel_path) is not None:
matched = rule.wild_match_pattern.include

if rule.wild_match_pattern.include is False:
has_negation = True

if matched and has_negation and path.is_dir():
return False

return matched

Expand Down
22 changes: 22 additions & 0 deletions airflow-core/tests/unit/utils/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ def test_find_path_from_directory_glob_ignore(self):
f"actual_included_filenames: {pformat(actual_included_filenames)}\nexpected_included_filenames: {pformat(should_not_ignore)}"
)

def test_find_path_from_directory_glob_negation(self, tmp_path):
(tmp_path / "subdir").mkdir()
(tmp_path / "dag.py").write_text("# root")
(tmp_path / "ignored.py").write_text("# ignored")
(tmp_path / "subdir" / "dag.py").write_text("# dag")
(tmp_path / "subdir" / "ignored.py").write_text("# other")

(tmp_path / ".airflowignore").write_text(
"\n".join(
[
"*",
"!dag.py",
"!subdir/dag.py",
]
)
)

found = list(find_path_from_directory(tmp_path, ".airflowignore", "glob"))
found_paths = {str(Path(f).relative_to(tmp_path)) for f in found}

assert found_paths == {"dag.py", "subdir/dag.py"}

def test_find_path_from_directory_respects_symlinks_regexp_ignore(self, test_dir):
ignore_list_file = ".airflowignore"
found = list(find_path_from_directory(test_dir, ignore_list_file))
Expand Down