Skip to content

Commit 0b3b40e

Browse files
Feat: Add parameters for load and save methods of base comparators (#64)
1 parent d1a407f commit 0b3b40e

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ repos:
1313
- id: end-of-file-fixer
1414
- id: trailing-whitespace
1515
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
16-
rev: v9.5.0
16+
rev: v9.13.0
1717
hooks:
1818
- id: commitlint
1919
stages:

dir_content_diff/base_comparators.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -406,16 +406,16 @@ class JsonComparator(DictComparator):
406406
This comparator is based on the :class:`DictComparator` and uses the same parameters.
407407
"""
408408

409-
def load(self, path):
409+
def load(self, path, **kwargs):
410410
"""Open a JSON file."""
411411
with open(path) as file: # pylint: disable=unspecified-encoding
412-
data = json.load(file)
412+
data = json.load(file, **kwargs)
413413
return data
414414

415-
def save(self, data, path):
415+
def save(self, data, path, **kwargs):
416416
"""Save formatted data into a JSON file."""
417417
with open(path, "w", encoding="utf-8") as file:
418-
json.dump(data, file)
418+
json.dump(data, file, **kwargs)
419419

420420

421421
class YamlComparator(DictComparator):
@@ -424,16 +424,16 @@ class YamlComparator(DictComparator):
424424
This comparator is based on the :class:`DictComparator` and uses the same parameters.
425425
"""
426426

427-
def load(self, path):
427+
def load(self, path, **kwargs):
428428
"""Open a YAML file."""
429429
with open(path) as file: # pylint: disable=unspecified-encoding
430-
data = yaml.full_load(file)
430+
data = yaml.full_load(file, **kwargs)
431431
return data
432432

433-
def save(self, data, path):
433+
def save(self, data, path, **kwargs):
434434
"""Save formatted data into a YAML file."""
435435
with open(path, "w", encoding="utf-8") as file:
436-
yaml.dump(data, file)
436+
yaml.dump(data, file, **kwargs)
437437

438438

439439
class XmlComparator(DictComparator):
@@ -468,10 +468,10 @@ def load(self, path): # pylint: disable=arguments-differ
468468
data = self.xmltodict(file.read())
469469
return data
470470

471-
def save(self, data, path):
471+
def save(self, data, path, root=False, **kwargs):
472472
"""Save formatted data into a XML file."""
473473
with open(path, "w", encoding="utf-8") as file:
474-
file.write(dicttoxml(data["root"]).decode())
474+
file.write(dicttoxml(data, root=root, **kwargs).decode())
475475

476476
@staticmethod
477477
def _cast_from_attribute(text, attr):
@@ -544,10 +544,10 @@ def load(self, path, **kwargs): # pylint: disable=arguments-differ
544544
data.read(path)
545545
return self.configparser_to_dict(data)
546546

547-
def save(self, data, path):
547+
def save(self, data, path, **kwargs):
548548
"""Save formatted data into a INI file."""
549549
with open(path, "w", encoding="utf-8") as file:
550-
self.dict_to_configparser(data).write(file)
550+
self.dict_to_configparser(data, **kwargs).write(file)
551551

552552
@staticmethod
553553
def configparser_to_dict(config):

tests/test_base.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,35 @@ def report(
410410
assert kwargs_msg in no_report_diff_default
411411
assert no_report_diff_default.replace(kwargs_msg, "") == diff
412412

413+
@staticmethod
414+
def common_test_load_save(tmp_path, comparator):
415+
"""Test load and save capabilities of the given comparator."""
416+
initial_data = {
417+
"a": {
418+
"b": 1,
419+
"c": [1, 2, 3],
420+
"d": {
421+
"test_str": "a str",
422+
"test_int": 999,
423+
},
424+
}
425+
}
426+
427+
initial_file = tmp_path / "initial_file.json"
428+
comparator.save(initial_data, initial_file)
429+
430+
loaded_data = comparator.load(initial_file)
431+
432+
assert loaded_data == initial_data
433+
413434
class TestJsonComparator:
414435
"""Test the JSON comparator."""
415436

437+
def test_load_save(self, tmp_path):
438+
"""Test load and save capabilities of the comparator."""
439+
comparator = dir_content_diff.JsonComparator()
440+
TestBaseComparator.common_test_load_save(tmp_path, comparator)
441+
416442
def test_format_data(self):
417443
"""Test data formatting."""
418444
data = {
@@ -490,6 +516,11 @@ def test_format_data(self):
490516
class TestXmlComparator:
491517
"""Test the XML comparator."""
492518

519+
def test_load_save(self, tmp_path):
520+
"""Test load and save capabilities of the comparator."""
521+
comparator = dir_content_diff.XmlComparator()
522+
TestBaseComparator.common_test_load_save(tmp_path, comparator)
523+
493524
def test_xmltodict(self):
494525
"""Test all types of the xmltodict auto cast feature."""
495526
comparator = dir_content_diff.XmlComparator()
@@ -574,6 +605,11 @@ def test_add_to_output_with_none(self):
574605
class TestIniComparator:
575606
"""Test the INI comparator."""
576607

608+
def test_load_save(self, tmp_path):
609+
"""Test load and save capabilities of the comparator."""
610+
comparator = dir_content_diff.IniComparator()
611+
TestBaseComparator.common_test_load_save(tmp_path, comparator)
612+
577613
def test_initodict(self, ref_tree):
578614
"""Test conversion of INI files into dict."""
579615
data = configparser.ConfigParser()

0 commit comments

Comments
 (0)