Skip to content

Commit 3fa092d

Browse files
Feat: Add patterns to ignore files in the reference directory
1 parent 15264bf commit 3fa092d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

dir_content_diff/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ def compare_trees(
259259
specific_args=None,
260260
return_raw_diffs=False,
261261
export_formatted_files=False,
262+
ignore_patterns=None,
262263
):
263264
"""Compare all files from 2 different directory trees and return the differences.
264265
@@ -306,6 +307,9 @@ def compare_trees(
306307
new directory with formatted compared data files. If a string is passed, this string is
307308
used as suffix for the new directory. If `True` is passed, the suffix is
308309
``_FORMATTED``.
310+
ignore_patterns (list): A list of regular expression patterns. If the relative path of a
311+
file matches one of these patterns, it is ignored during the comparison. Note that
312+
this means that any specific arguments for that file will also be ignored.
309313
310314
Returns:
311315
dict: A ``dict`` in which the keys are the relative file paths and the values are the
@@ -333,6 +337,11 @@ def compare_trees(
333337
for pattern in v.pop("patterns", []):
334338
pattern_specific_args[re.compile(pattern)] = v
335339

340+
if ignore_patterns is None:
341+
ignore_patterns = []
342+
else:
343+
ignore_patterns = [re.compile(i) for i in ignore_patterns]
344+
336345
# Loop over all files and call the correct comparator
337346
different_files = {}
338347
for ref_file in ref_path.glob("**/*"):
@@ -342,6 +351,14 @@ def compare_trees(
342351
relative_path = ref_file.relative_to(ref_path).as_posix()
343352
comp_file = comp_path / relative_path
344353

354+
ignored = False
355+
for pattern in ignore_patterns:
356+
if pattern.match(relative_path):
357+
ignored = True
358+
break
359+
if ignored:
360+
continue
361+
345362
if comp_file.exists():
346363
specific_file_args = specific_args.get(relative_path, None)
347364
if specific_file_args is None:

tests/test_base.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,26 @@ def test_diff_tree(
10181018
]:
10191019
assert match_i is not None
10201020

1021+
def test_diff_tree_ignore(
1022+
self, ref_tree, res_tree_diff, pdf_diff, dict_diff, xml_diff, ini_diff
1023+
):
1024+
"""Test that the returned differences are correct even with ignored files."""
1025+
res = compare_trees(
1026+
ref_tree, res_tree_diff, ignore_patterns=[r".*\.yaml", r".*\.ini"]
1027+
)
1028+
1029+
assert len(res) == 3
1030+
match_res_0 = re.match(pdf_diff, res["file.pdf"])
1031+
match_res_1 = re.match(dict_diff, res["file.json"])
1032+
match_res_3 = re.match(xml_diff, res["file.xml"])
1033+
1034+
for match_i in [
1035+
match_res_0,
1036+
match_res_1,
1037+
match_res_3,
1038+
]:
1039+
assert match_i is not None
1040+
10211041
def test_assert_equal_trees(
10221042
self, ref_tree, res_tree_diff, pdf_diff, dict_diff, xml_diff
10231043
):

0 commit comments

Comments
 (0)