Skip to content

Commit e12901c

Browse files
Feat: The specific_args can support a 'comparator' entry (#48)
1 parent 2bf5b1b commit e12901c

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ dir_content_diff.assert_equal_trees(
152152

153153
Each comparator has different arguments that are detailed in the documentation.
154154

155+
It's also possible to specify a arbitrary comparator for a specific file:
156+
157+
```python
158+
specific_args = {
159+
"sub_dir_1/sub_file_1.a": {
160+
"comparator": dir_content_diff.JsonComparator(),
161+
"tolerance": 0.5,
162+
}
163+
}
164+
```
165+
155166

156167
### Export formatted data
157168

dir_content_diff/__init__.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ def register_comparator(ext, comparator, force=False):
6262
registered and the comparator is replaced.
6363
6464
.. note::
65-
The given comparator should have the following signature:
65+
It is possible to create and register custom comparators. The easiest way to do it is to
66+
derive a class from :class:`dir_content_diff.BaseComparator`.
67+
68+
Otherwise, the given comparator should be a callable with the following signature:
6669
6770
.. code-block:: python
6871
@@ -74,7 +77,8 @@ def register_comparator(ext, comparator, force=False):
7477
**diff_kwargs: Mapping[str, Any],
7578
) -> Union[False, str]
7679
77-
The return type can be Any when used with `return_raw_diffs == True`.
80+
The return type can be Any when used with `return_raw_diffs == True`, else it should be a
81+
string object.
7882
"""
7983
ext = format_ext(ext)
8084
if not force and ext in _COMPARATORS:
@@ -229,6 +233,7 @@ def compare_trees(
229233
230234
{
231235
<relative_file_path>: {
236+
comparator: ComparatorInstance,
232237
args: [arg1, arg2, ...],
233238
kwargs: {
234239
kwarg_name_1: kwarg_value_1,
@@ -237,6 +242,8 @@ def compare_trees(
237242
},
238243
<another_file_path>: {...}
239244
}
245+
246+
Note that all entries in this ``dict`` are optional.
240247
return_raw_diffs (bool): If set to ``True``, only the raw differences are returned instead
241248
of a formatted report.
242249
export_formatted_files (bool or str): If set to ``True`` or a not empty string, create a
@@ -265,6 +272,8 @@ def compare_trees(
265272

266273
if specific_args is None:
267274
specific_args = {}
275+
else:
276+
specific_args = copy.deepcopy(specific_args)
268277

269278
# Loop over all files and call the correct comparator
270279
different_files = {}
@@ -277,15 +286,21 @@ def compare_trees(
277286

278287
if comp_file.exists():
279288
specific_file_args = specific_args.get(relative_path, {})
280-
comparator = comparators.get(ref_file.suffix, _COMPARATORS.get(None))
281-
comparator_kwargs = {k: v for k, v in specific_file_args.items() if k != "args"}
289+
comparator = specific_file_args.pop(
290+
"comparator",
291+
comparators.get(
292+
ref_file.suffix,
293+
_COMPARATORS.get(None),
294+
),
295+
)
296+
comparator_args = specific_file_args.pop("args", [])
282297
res = compare_files(
283298
ref_file,
284299
comp_file,
285300
comparator,
286-
*specific_file_args.get("args", []),
301+
*comparator_args,
287302
return_raw_diffs=return_raw_diffs,
288-
**comparator_kwargs,
303+
**specific_file_args,
289304
)
290305
if res is not False:
291306
different_files[relative_path] = res
@@ -294,7 +309,7 @@ def compare_trees(
294309
comp_file,
295310
formatted_data_path / relative_path,
296311
comparator,
297-
**comparator_kwargs,
312+
**specific_file_args,
298313
)
299314
else:
300315
msg = f"The file '{relative_path}' does not exist in '{comp_path}'."

tests/test_base.py

+10
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,16 @@ def test_specific_args(self, ref_tree, res_tree_equal):
706706

707707
assert res == {}
708708

709+
def test_specific_comparator(self, ref_tree, res_tree_equal):
710+
"""Test specific args."""
711+
specific_args = {
712+
"file.yaml": {"args": [None, None, None, False, 0, False]},
713+
"file.json": {"comparator": dir_content_diff.DefaultComparator()},
714+
}
715+
res = compare_trees(ref_tree, res_tree_equal, specific_args=specific_args)
716+
717+
assert res == {}
718+
709719

710720
class TestDiffTrees:
711721
"""Tests that should return differences."""

0 commit comments

Comments
 (0)