Skip to content

Commit

Permalink
Fix: Multiple pattern matches
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-berchet committed Feb 5, 2024
1 parent 6ec8412 commit 241c4b1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dir_content_diff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def compare_trees(
if specific_file_args is None:
for pattern, pattern_args in pattern_specific_args.items():
if pattern.match(relative_path):
specific_file_args = pattern_args
specific_file_args = copy.deepcopy(pattern_args)
break
if specific_file_args is None:
specific_file_args = {}
Expand Down
33 changes: 32 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ def test_specific_comparator(self, ref_tree, res_tree_equal):

assert res == {}

def test_specific_patterns(self, ref_tree, res_tree_equal):
def test_specific_patterns(self, ref_tree, res_tree_equal, base_diff):
"""Test specific args."""
specific_args = {
"all yaml files": {
Expand All @@ -739,6 +739,18 @@ def test_specific_patterns(self, ref_tree, res_tree_equal):

assert res == {}

# Test pattern multiple matches
specific_args = {
"all files": {
"comparator": dir_content_diff.DefaultComparator(),
"patterns": [r"file\..*"],
},
}
res = compare_trees(ref_tree, res_tree_equal, specific_args=specific_args)

assert list(res.keys()) == ["file.pdf"]
assert re.match(base_diff, res["file.pdf"]) is not None


class TestDiffTrees:
"""Tests that should return differences."""
Expand Down Expand Up @@ -897,6 +909,25 @@ def test_specific_patterns(self, ref_tree, res_tree_diff, base_diff, dict_diff):

assert re.match(dict_diff, res["file.json"]) is not None

# Test pattern multiple matches
specific_args = {
"all files": {
"comparator": dir_content_diff.DefaultComparator(),
"patterns": [r"file\..*"],
},
}
res = compare_trees(ref_tree, res_tree_diff, specific_args=specific_args)

assert sorted(res.keys()) == [
"file.ini",
"file.json",
"file.pdf",
"file.xml",
"file.yaml",
]
for k, v in res.items():
assert re.match(base_diff, v)

def test_unknown_comparator(self, ref_tree, res_tree_diff, registry_reseter):
"""Test with an unknown extension."""
dir_content_diff.unregister_comparator(".yaml")
Expand Down

0 comments on commit 241c4b1

Please sign in to comment.