Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ specific_args = {
}
```

And last but not least, it's possible to use regular expressions to associate specific arguments to
Another possibility is to use regular expressions to associate specific arguments to
a set of files:

```python
Expand All @@ -186,6 +186,16 @@ specific_args = {
}
```

And last but not least, it's possible to ignore a set of files from the reference directory (for
example because the reference directory contains temporary files that should not be compared). For
example, the following code will ignore all files whose name ends with `_tmp.yaml`:

```python
import dir_content_diff

dir_content_diff.compare_trees("reference_dir", "compared_dir", ignore_patterns=[r".*_tmp\.yaml"])
```


### Export formatted data

Expand Down
17 changes: 17 additions & 0 deletions dir_content_diff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ def compare_trees(
specific_args=None,
return_raw_diffs=False,
export_formatted_files=False,
ignore_patterns=None,
):
"""Compare all files from 2 different directory trees and return the differences.

Expand Down Expand Up @@ -306,6 +307,9 @@ def compare_trees(
new directory with formatted compared data files. If a string is passed, this string is
used as suffix for the new directory. If `True` is passed, the suffix is
``_FORMATTED``.
ignore_patterns (list): A list of regular expression patterns. If the relative path of a
file matches one of these patterns, it is ignored during the comparison. Note that
this means that any specific arguments for that file will also be ignored.

Returns:
dict: A ``dict`` in which the keys are the relative file paths and the values are the
Expand Down Expand Up @@ -333,6 +337,11 @@ def compare_trees(
for pattern in v.pop("patterns", []):
pattern_specific_args[re.compile(pattern)] = v

if ignore_patterns is None:
ignore_patterns = []
else:
ignore_patterns = [re.compile(i) for i in ignore_patterns]

# Loop over all files and call the correct comparator
different_files = {}
for ref_file in ref_path.glob("**/*"):
Expand All @@ -342,6 +351,14 @@ def compare_trees(
relative_path = ref_file.relative_to(ref_path).as_posix()
comp_file = comp_path / relative_path

ignored = False
for pattern in ignore_patterns:
if pattern.match(relative_path):
ignored = True
break
if ignored:
continue

if comp_file.exists():
specific_file_args = specific_args.get(relative_path, None)
if specific_file_args is None:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,26 @@ def test_diff_tree(
]:
assert match_i is not None

def test_diff_tree_ignore(
self, ref_tree, res_tree_diff, pdf_diff, dict_diff, xml_diff, ini_diff
):
"""Test that the returned differences are correct even with ignored files."""
res = compare_trees(
ref_tree, res_tree_diff, ignore_patterns=[r".*\.yaml", r".*\.ini"]
)

assert len(res) == 3
match_res_0 = re.match(pdf_diff, res["file.pdf"])
match_res_1 = re.match(dict_diff, res["file.json"])
match_res_3 = re.match(xml_diff, res["file.xml"])

for match_i in [
match_res_0,
match_res_1,
match_res_3,
]:
assert match_i is not None

def test_assert_equal_trees(
self, ref_tree, res_tree_diff, pdf_diff, dict_diff, xml_diff
):
Expand Down