Skip to content

Commit

Permalink
Feat: Add parameters for load and save methods of base comparators (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-berchet authored Sep 9, 2024
1 parent d1a407f commit 0b3b40e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
rev: v9.5.0
rev: v9.13.0
hooks:
- id: commitlint
stages:
Expand Down
24 changes: 12 additions & 12 deletions dir_content_diff/base_comparators.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,16 +406,16 @@ class JsonComparator(DictComparator):
This comparator is based on the :class:`DictComparator` and uses the same parameters.
"""

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

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


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

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

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


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

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

@staticmethod
def _cast_from_attribute(text, attr):
Expand Down Expand Up @@ -544,10 +544,10 @@ def load(self, path, **kwargs): # pylint: disable=arguments-differ
data.read(path)
return self.configparser_to_dict(data)

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

@staticmethod
def configparser_to_dict(config):
Expand Down
36 changes: 36 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,35 @@ def report(
assert kwargs_msg in no_report_diff_default
assert no_report_diff_default.replace(kwargs_msg, "") == diff

@staticmethod
def common_test_load_save(tmp_path, comparator):
"""Test load and save capabilities of the given comparator."""
initial_data = {
"a": {
"b": 1,
"c": [1, 2, 3],
"d": {
"test_str": "a str",
"test_int": 999,
},
}
}

initial_file = tmp_path / "initial_file.json"
comparator.save(initial_data, initial_file)

loaded_data = comparator.load(initial_file)

assert loaded_data == initial_data

class TestJsonComparator:
"""Test the JSON comparator."""

def test_load_save(self, tmp_path):
"""Test load and save capabilities of the comparator."""
comparator = dir_content_diff.JsonComparator()
TestBaseComparator.common_test_load_save(tmp_path, comparator)

def test_format_data(self):
"""Test data formatting."""
data = {
Expand Down Expand Up @@ -490,6 +516,11 @@ def test_format_data(self):
class TestXmlComparator:
"""Test the XML comparator."""

def test_load_save(self, tmp_path):
"""Test load and save capabilities of the comparator."""
comparator = dir_content_diff.XmlComparator()
TestBaseComparator.common_test_load_save(tmp_path, comparator)

def test_xmltodict(self):
"""Test all types of the xmltodict auto cast feature."""
comparator = dir_content_diff.XmlComparator()
Expand Down Expand Up @@ -574,6 +605,11 @@ def test_add_to_output_with_none(self):
class TestIniComparator:
"""Test the INI comparator."""

def test_load_save(self, tmp_path):
"""Test load and save capabilities of the comparator."""
comparator = dir_content_diff.IniComparator()
TestBaseComparator.common_test_load_save(tmp_path, comparator)

def test_initodict(self, ref_tree):
"""Test conversion of INI files into dict."""
data = configparser.ConfigParser()
Expand Down

0 comments on commit 0b3b40e

Please sign in to comment.