From aabb94066ea448a17546c29c170f60ca4d50266c Mon Sep 17 00:00:00 2001 From: Andrew Mitchell Date: Thu, 12 Feb 2026 14:23:33 +0000 Subject: [PATCH 01/12] Adds JSON support to data regression fixture --- src/pytest_regressions/data_regression.py | 40 +++++++++---- tests/test_data_regression.py | 70 +++++++++++++++++++---- 2 files changed, 86 insertions(+), 24 deletions(-) diff --git a/src/pytest_regressions/data_regression.py b/src/pytest_regressions/data_regression.py index 8252cef..20a754d 100644 --- a/src/pytest_regressions/data_regression.py +++ b/src/pytest_regressions/data_regression.py @@ -1,4 +1,5 @@ import os +import json from collections.abc import Callable from collections.abc import MutableMapping from functools import partial @@ -41,6 +42,7 @@ def check( basename: str | None = None, fullpath: Optional["os.PathLike[str]"] = None, round_digits: int | None = None, + extension: str = ".yml", ) -> None: """ Checks the given dict against a previously recorded version, or generate a new file. @@ -58,6 +60,10 @@ def check( :param round_digits: If given, round all floats in the dict to the given number of digits. + :param extension: Extension of the file. Defaults to ".yml". + If equal to ".json", expects `data_dict` to be JSON serializable + and dumps it using standard `json.dump`. + ``basename`` and ``fullpath`` are exclusive. """ __tracebackhide__ = True @@ -67,17 +73,27 @@ def check( def dump(filename: Path) -> None: """Dump dict contents to the given filename""" - - dumped_str = yaml.dump_all( - [data_dict], - Dumper=RegressionYamlDumper, - default_flow_style=False, - allow_unicode=True, - indent=2, - encoding="utf-8", - ) - with filename.open("wb") as f: - f.write(dumped_str) + if extension.lower() in [".yml", ".yaml"]: + dumped_str = yaml.dump_all( + [data_dict], + Dumper=RegressionYamlDumper, + default_flow_style=False, + allow_unicode=True, + indent=2, + encoding="utf-8", + ) + with filename.open("wb") as f: + f.write(dumped_str) + elif extension.lower() == ".json": + with filename.open("w", encoding="utf-8") as f: + json.dump( + data_dict, f, indent=2, sort_keys=True, ensure_ascii=False + ) + else: + raise NotImplementedError( + f"file extension `{extension}` is not supported by data_regression; " + "supported extensions are '.yml', '.yaml', '.json'" + ) perform_regression_check( datadir=self.datadir, @@ -85,7 +101,7 @@ def dump(filename: Path) -> None: request=self.request, check_fn=partial(check_text_files, encoding="UTF-8"), dump_fn=dump, - extension=".yml", + extension=extension, basename=basename, fullpath=fullpath, force_regen=self.force_regen, diff --git a/tests/test_data_regression.py b/tests/test_data_regression.py index f3c3aa3..795ba3e 100644 --- a/tests/test_data_regression.py +++ b/tests/test_data_regression.py @@ -8,16 +8,18 @@ from pytest_regressions.testing import check_regression_fixture_workflow -def test_example(data_regression: DataRegressionFixture) -> None: +@pytest.mark.parametrize("extension", [".yml", ".json"]) +def test_example(data_regression: DataRegressionFixture, extension: str) -> None: """Basic example""" contents = {"contents": "Foo", "value": 11} - data_regression.check(contents) + data_regression.check(contents, extension=extension) -def test_basename(data_regression: DataRegressionFixture) -> None: +@pytest.mark.parametrize("extension", [".yml", ".json"]) +def test_basename(data_regression: DataRegressionFixture, extension: str) -> None: """Basic example using basename parameter""" contents = {"contents": "Foo", "value": 11} - data_regression.check(contents, basename="case.normal") + data_regression.check(contents, basename="case.normal", extension=extension) def test_integer_keys(data_regression: DataRegressionFixture) -> None: @@ -51,14 +53,15 @@ def dump_scalar(dumper, scalar): data_regression.check(contents) -def test_round_digits(data_regression: DataRegressionFixture) -> None: +@pytest.mark.parametrize("extension", [".yml", ".json"]) +def test_round_digits(data_regression: DataRegressionFixture, extension: str) -> None: """Example including float numbers and check rounding capabilities.""" contents = { "content": {"value1": "toto", "value": 1.123456789}, "values": [1.12345, 2.34567], "value": 1.23456789, } - data_regression.check(contents, round_digits=2) + data_regression.check(contents, round_digits=2, extension=extension) with pytest.raises(AssertionError): contents = { @@ -66,7 +69,7 @@ def test_round_digits(data_regression: DataRegressionFixture) -> None: "values": [1.13456, 2.45678], "value": 1.23456789, } - data_regression.check(contents, round_digits=2) + data_regression.check(contents, round_digits=2, extension=extension) def test_usage_workflow(pytester, monkeypatch): @@ -101,21 +104,56 @@ def get_yaml_contents(): ) -def test_data_regression_full_path(pytester, tmp_path): +def test_usage_workflow_json(pytester, monkeypatch): + import json + + monkeypatch.setattr( + sys, "testing_get_data", lambda: {"contents": "Foo", "value": 10}, raising=False + ) + source = """ + import sys + def test_1(data_regression) -> None: + contents = sys.testing_get_data() + data_regression.check(contents, extension=".json") + """ + + def get_json_contents(): + json_filename = pytester.path / "test_file" / "test_1.json" + assert json_filename.is_file() + with json_filename.open() as f: + return json.load(f) + + check_regression_fixture_workflow( + pytester, + source=source, + data_getter=get_json_contents, + data_modifier=lambda: monkeypatch.setattr( + sys, + "testing_get_data", + lambda: {"contents": "Bar", "value": 20}, + raising=False, + ), + expected_data_1={"contents": "Foo", "value": 10}, + expected_data_2={"contents": "Bar", "value": 20}, + ) + + +@pytest.mark.parametrize("extension", [".yml", ".json"]) +def test_data_regression_full_path(pytester, tmp_path, extension): """ Test data_regression with ``fullpath`` parameter. """ - fullpath = tmp_path.joinpath("full/path/to/contents.yaml") + fullpath = tmp_path.joinpath(f"full/path/to/contents{extension}") fullpath.parent.mkdir(parents=True) assert not fullpath.is_file() source = """ def test(data_regression) -> None: contents = {'data': [1, 2]} - data_regression.check(contents, fullpath=%s) - """ % (repr(str(fullpath))) + data_regression.check(contents, fullpath=%s, extension=%r) + """ % (repr(str(fullpath)), extension) pytester.makepyfile(test_foo=source) - # First run fails because there's no yml file yet + # First run fails because there's no expected file yet result = pytester.inline_run() result.assertoutcome(failed=1) @@ -210,6 +248,14 @@ def __init__(self, value, unit): assert not yaml_file.is_file() +def test_unsupported_extension(data_regression): + data = {"foo": "bar"} + with pytest.raises( + NotImplementedError, match=r"file extension `\.txt` is not supported" + ): + data_regression.check(data, extension=".txt") + + def test_regen_all(pytester, tmp_path): source = """ def test_1(data_regression) -> None: From fa7a1f45241d67db953675974f13e758342ab163 Mon Sep 17 00:00:00 2001 From: Andrew Mitchell Date: Thu, 12 Feb 2026 14:23:34 +0000 Subject: [PATCH 02/12] Adds expected JSON regression files --- tests/test_data_regression/case.normal.json | 4 ++++ tests/test_data_regression/test_example__json_.json | 4 ++++ tests/test_data_regression/test_example__yml_.yml | 2 ++ .../test_round_digits__json_.json | 11 +++++++++++ .../test_data_regression/test_round_digits__yml_.yml | 7 +++++++ tests/test_data_regression_json/custom_name.json | 3 +++ .../test_json_regression.json | 8 ++++++++ 7 files changed, 39 insertions(+) create mode 100644 tests/test_data_regression/case.normal.json create mode 100644 tests/test_data_regression/test_example__json_.json create mode 100644 tests/test_data_regression/test_example__yml_.yml create mode 100644 tests/test_data_regression/test_round_digits__json_.json create mode 100644 tests/test_data_regression/test_round_digits__yml_.yml create mode 100644 tests/test_data_regression_json/custom_name.json create mode 100644 tests/test_data_regression_json/test_json_regression.json diff --git a/tests/test_data_regression/case.normal.json b/tests/test_data_regression/case.normal.json new file mode 100644 index 0000000..f6de75c --- /dev/null +++ b/tests/test_data_regression/case.normal.json @@ -0,0 +1,4 @@ +{ + "contents": "Foo", + "value": 11 +} \ No newline at end of file diff --git a/tests/test_data_regression/test_example__json_.json b/tests/test_data_regression/test_example__json_.json new file mode 100644 index 0000000..f6de75c --- /dev/null +++ b/tests/test_data_regression/test_example__json_.json @@ -0,0 +1,4 @@ +{ + "contents": "Foo", + "value": 11 +} \ No newline at end of file diff --git a/tests/test_data_regression/test_example__yml_.yml b/tests/test_data_regression/test_example__yml_.yml new file mode 100644 index 0000000..f605447 --- /dev/null +++ b/tests/test_data_regression/test_example__yml_.yml @@ -0,0 +1,2 @@ +contents: Foo +value: 11 diff --git a/tests/test_data_regression/test_round_digits__json_.json b/tests/test_data_regression/test_round_digits__json_.json new file mode 100644 index 0000000..3fa1033 --- /dev/null +++ b/tests/test_data_regression/test_round_digits__json_.json @@ -0,0 +1,11 @@ +{ + "content": { + "value": 1.12, + "value1": "toto" + }, + "value": 1.23, + "values": [ + 1.12, + 2.35 + ] +} \ No newline at end of file diff --git a/tests/test_data_regression/test_round_digits__yml_.yml b/tests/test_data_regression/test_round_digits__yml_.yml new file mode 100644 index 0000000..54cc585 --- /dev/null +++ b/tests/test_data_regression/test_round_digits__yml_.yml @@ -0,0 +1,7 @@ +content: + value: 1.12 + value1: toto +value: 1.23 +values: +- 1.12 +- 2.35 diff --git a/tests/test_data_regression_json/custom_name.json b/tests/test_data_regression_json/custom_name.json new file mode 100644 index 0000000..b42f309 --- /dev/null +++ b/tests/test_data_regression_json/custom_name.json @@ -0,0 +1,3 @@ +{ + "foo": "bar" +} \ No newline at end of file diff --git a/tests/test_data_regression_json/test_json_regression.json b/tests/test_data_regression_json/test_json_regression.json new file mode 100644 index 0000000..62cc568 --- /dev/null +++ b/tests/test_data_regression_json/test_json_regression.json @@ -0,0 +1,8 @@ +{ + "baz": [ + 1, + 2, + 3 + ], + "foo": "bar" +} \ No newline at end of file From 4555f36fe540c9d65ca5b97fadb1f06ae9b0e59c Mon Sep 17 00:00:00 2001 From: Andrew Mitchell Date: Thu, 12 Feb 2026 15:35:12 +0000 Subject: [PATCH 03/12] Add recursive dictionary sorting utility and integrate it into data regression fixture --- src/pytest_regressions/common.py | 14 ++++++++++++++ src/pytest_regressions/data_regression.py | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/pytest_regressions/common.py b/src/pytest_regressions/common.py index de2f23d..68f1a6b 100644 --- a/src/pytest_regressions/common.py +++ b/src/pytest_regressions/common.py @@ -20,6 +20,20 @@ def import_error_message(libname: str) -> str: return f"'{libname}' library is an optional dependency and must be installed explicitly when the fixture 'check' is used" +def sort_dict_by_keys(data: MutableMapping[Any, Any]) -> MutableMapping[Any, Any]: + """Recursively sort a dict by its keys. + + :param data: The dict to sort. + :return: The sorted dict. + """ + data = { + k: sort_dict_by_keys(v) if isinstance(v, MutableMapping) else v + for k, v in data.items() + } + + return dict(sorted(data.items())) + + def check_text_files( obtained_fn: "os.PathLike[str]", expected_fn: "os.PathLike[str]", diff --git a/src/pytest_regressions/data_regression.py b/src/pytest_regressions/data_regression.py index 20a754d..5ebaa2b 100644 --- a/src/pytest_regressions/data_regression.py +++ b/src/pytest_regressions/data_regression.py @@ -11,7 +11,7 @@ import pytest import yaml -from .common import check_text_files +from .common import check_text_files, sort_dict_by_keys from .common import perform_regression_check from .common import round_digits_in_data @@ -71,6 +71,8 @@ def check( if round_digits is not None: round_digits_in_data(data_dict, round_digits) + data_dict = sort_dict_by_keys(data_dict) + def dump(filename: Path) -> None: """Dump dict contents to the given filename""" if extension.lower() in [".yml", ".yaml"]: From 1658c31a1678dc1cd57f17287f46887343688f52 Mon Sep 17 00:00:00 2001 From: Andrew Mitchell Date: Thu, 12 Feb 2026 15:40:28 +0000 Subject: [PATCH 04/12] Update JSON output formatting in data regression fixture to use 4-space indentation --- src/pytest_regressions/data_regression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytest_regressions/data_regression.py b/src/pytest_regressions/data_regression.py index 5ebaa2b..1e4bc11 100644 --- a/src/pytest_regressions/data_regression.py +++ b/src/pytest_regressions/data_regression.py @@ -89,7 +89,7 @@ def dump(filename: Path) -> None: elif extension.lower() == ".json": with filename.open("w", encoding="utf-8") as f: json.dump( - data_dict, f, indent=2, sort_keys=True, ensure_ascii=False + data_dict, f, indent=4, sort_keys=True, ensure_ascii=False ) else: raise NotImplementedError( From ed8e9b5d766425644d21a974dffa9138adab7513 Mon Sep 17 00:00:00 2001 From: Andrew Mitchell Date: Thu, 23 Apr 2026 12:22:08 +0100 Subject: [PATCH 05/12] Refactor JSON dumping in DataRegressionFixture to use 2-space indentation --- src/pytest_regressions/data_regression.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/pytest_regressions/data_regression.py b/src/pytest_regressions/data_regression.py index 1e4bc11..4d57db5 100644 --- a/src/pytest_regressions/data_regression.py +++ b/src/pytest_regressions/data_regression.py @@ -1,5 +1,5 @@ -import os import json +import os from collections.abc import Callable from collections.abc import MutableMapping from functools import partial @@ -11,9 +11,10 @@ import pytest import yaml -from .common import check_text_files, sort_dict_by_keys +from .common import check_text_files from .common import perform_regression_check from .common import round_digits_in_data +from .common import sort_dict_by_keys if TYPE_CHECKING: from pytest_datadir.plugin import LazyDataDir @@ -89,7 +90,7 @@ def dump(filename: Path) -> None: elif extension.lower() == ".json": with filename.open("w", encoding="utf-8") as f: json.dump( - data_dict, f, indent=4, sort_keys=True, ensure_ascii=False + data_dict, f, indent=2, sort_keys=True, ensure_ascii=False ) else: raise NotImplementedError( From 28913a8ae91dab6c3122ab17e702b8b35026d026 Mon Sep 17 00:00:00 2001 From: Andrew Mitchell Date: Fri, 24 Apr 2026 11:10:06 +0100 Subject: [PATCH 06/12] Update src/pytest_regressions/data_regression.py When using the JSON path, the file is opened and json.dump writes directly to disk. If serialization fails (TypeError for non-serializable objects), this can leave an empty/partial expected/obtained file behind, unlike the YAML path which renders to bytes before opening the file. serializing to a string first (e.g., json.dumps) and only writing after successful serialization. Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/pytest_regressions/data_regression.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/pytest_regressions/data_regression.py b/src/pytest_regressions/data_regression.py index 4d57db5..fa8cfd1 100644 --- a/src/pytest_regressions/data_regression.py +++ b/src/pytest_regressions/data_regression.py @@ -88,10 +88,11 @@ def dump(filename: Path) -> None: with filename.open("wb") as f: f.write(dumped_str) elif extension.lower() == ".json": + dumped_str = json.dumps( + data_dict, indent=2, sort_keys=True, ensure_ascii=False + ) with filename.open("w", encoding="utf-8") as f: - json.dump( - data_dict, f, indent=2, sort_keys=True, ensure_ascii=False - ) + f.write(dumped_str) else: raise NotImplementedError( f"file extension `{extension}` is not supported by data_regression; " From 8e40b08f29b3f60d9bc6d21002ab58d1ee77a49b Mon Sep 17 00:00:00 2001 From: Andrew Mitchell Date: Fri, 24 Apr 2026 11:24:17 +0100 Subject: [PATCH 07/12] Update src/pytest_regressions/common.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The docstring says this function recursively sorts dicts, but the implementation only recurses into mapping values and won’t sort dicts nested inside sequences (e.g., [{...}, {...}]). Consider extending it to walk MutableSequence values too, or tightening the docstring/name to match the actual behavior. Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/pytest_regressions/common.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/pytest_regressions/common.py b/src/pytest_regressions/common.py index 68f1a6b..e413181 100644 --- a/src/pytest_regressions/common.py +++ b/src/pytest_regressions/common.py @@ -21,19 +21,21 @@ def import_error_message(libname: str) -> str: def sort_dict_by_keys(data: MutableMapping[Any, Any]) -> MutableMapping[Any, Any]: - """Recursively sort a dict by its keys. + """Recursively sort a dict by its keys, including nested dicts in sequences. :param data: The dict to sort. :return: The sorted dict. """ - data = { - k: sort_dict_by_keys(v) if isinstance(v, MutableMapping) else v - for k, v in data.items() - } - - return dict(sorted(data.items())) + def _sort_nested(value: Any) -> Any: + if isinstance(value, MutableMapping): + normalized = {k: _sort_nested(v) for k, v in value.items()} + return dict(sorted(normalized.items())) + if isinstance(value, MutableSequence): + return [_sort_nested(item) for item in value] + return value + return _sort_nested(data) def check_text_files( obtained_fn: "os.PathLike[str]", expected_fn: "os.PathLike[str]", From 93e5cd406a84c6e42474f102790349831b4346c1 Mon Sep 17 00:00:00 2001 From: Andrew Mitchell Date: Fri, 24 Apr 2026 11:29:21 +0100 Subject: [PATCH 08/12] Add JSON sorting test to validate sort_dict_by_keys functionality Co-authored-by: Copilot --- tests/test_data_regression.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_data_regression.py b/tests/test_data_regression.py index 795ba3e..beb62f1 100644 --- a/tests/test_data_regression.py +++ b/tests/test_data_regression.py @@ -1,13 +1,33 @@ +import json import sys from textwrap import dedent import pytest import yaml +from pytest_regressions.common import sort_dict_by_keys from pytest_regressions.data_regression import DataRegressionFixture from pytest_regressions.testing import check_regression_fixture_workflow +def test_sort_dict_by_keys_matches_json_sort_keys() -> None: + contents = { + "z": [{"b": 2, "a": 1}, {"k": 0, "inner": [{"d": 4, "c": 3}]}], + "a": {"y": 2, "x": 1}, + "m": [{"beta": 2, "alpha": 1}, ["keep", {"bb": 2, "aa": 1}]], + } + + sorted_contents = sort_dict_by_keys(contents) + + assert list(sorted_contents) == ["a", "m", "z"] + assert list(sorted_contents["z"][0]) == ["a", "b"] + assert list(sorted_contents["z"][1]["inner"][0]) == ["c", "d"] + assert list(sorted_contents["m"][0]) == ["alpha", "beta"] + assert list(sorted_contents["m"][1][1]) == ["aa", "bb"] + + assert json.dumps(sorted_contents) == json.dumps(contents, sort_keys=True) + + @pytest.mark.parametrize("extension", [".yml", ".json"]) def test_example(data_regression: DataRegressionFixture, extension: str) -> None: """Basic example""" From b44d3dbfbe7e72accbbdab298b6b7134f21e22e1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 24 Apr 2026 10:30:01 +0000 Subject: [PATCH 09/12] Remove stale and unused baseline test data files Agent-Logs-Url: https://github.com/MitchellAcoustics/pytest-regressions/sessions/ecff2466-7c11-4a25-87cb-0ebd388921c3 Co-authored-by: MitchellAcoustics <22335636+MitchellAcoustics@users.noreply.github.com> --- tests/test_data_regression/test_example.yml | 2 -- tests/test_data_regression/test_round_digits.yml | 7 ------- tests/test_data_regression_json/custom_name.json | 3 --- tests/test_data_regression_json/test_json_regression.json | 8 -------- 4 files changed, 20 deletions(-) delete mode 100644 tests/test_data_regression/test_example.yml delete mode 100644 tests/test_data_regression/test_round_digits.yml delete mode 100644 tests/test_data_regression_json/custom_name.json delete mode 100644 tests/test_data_regression_json/test_json_regression.json diff --git a/tests/test_data_regression/test_example.yml b/tests/test_data_regression/test_example.yml deleted file mode 100644 index f605447..0000000 --- a/tests/test_data_regression/test_example.yml +++ /dev/null @@ -1,2 +0,0 @@ -contents: Foo -value: 11 diff --git a/tests/test_data_regression/test_round_digits.yml b/tests/test_data_regression/test_round_digits.yml deleted file mode 100644 index 54cc585..0000000 --- a/tests/test_data_regression/test_round_digits.yml +++ /dev/null @@ -1,7 +0,0 @@ -content: - value: 1.12 - value1: toto -value: 1.23 -values: -- 1.12 -- 2.35 diff --git a/tests/test_data_regression_json/custom_name.json b/tests/test_data_regression_json/custom_name.json deleted file mode 100644 index b42f309..0000000 --- a/tests/test_data_regression_json/custom_name.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "foo": "bar" -} \ No newline at end of file diff --git a/tests/test_data_regression_json/test_json_regression.json b/tests/test_data_regression_json/test_json_regression.json deleted file mode 100644 index 62cc568..0000000 --- a/tests/test_data_regression_json/test_json_regression.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "baz": [ - 1, - 2, - 3 - ], - "foo": "bar" -} \ No newline at end of file From 87e433a39bc04c455d598a2734ea7a3b715776dd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 24 Apr 2026 10:48:38 +0000 Subject: [PATCH 10/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/pytest_regressions/common.py | 2 ++ tests/test_data_regression/case.normal.json | 2 +- tests/test_data_regression/test_example__json_.json | 2 +- tests/test_data_regression/test_round_digits__json_.json | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/pytest_regressions/common.py b/src/pytest_regressions/common.py index e413181..123c974 100644 --- a/src/pytest_regressions/common.py +++ b/src/pytest_regressions/common.py @@ -36,6 +36,8 @@ def _sort_nested(value: Any) -> Any: return value return _sort_nested(data) + + def check_text_files( obtained_fn: "os.PathLike[str]", expected_fn: "os.PathLike[str]", diff --git a/tests/test_data_regression/case.normal.json b/tests/test_data_regression/case.normal.json index f6de75c..573d49b 100644 --- a/tests/test_data_regression/case.normal.json +++ b/tests/test_data_regression/case.normal.json @@ -1,4 +1,4 @@ { "contents": "Foo", "value": 11 -} \ No newline at end of file +} diff --git a/tests/test_data_regression/test_example__json_.json b/tests/test_data_regression/test_example__json_.json index f6de75c..573d49b 100644 --- a/tests/test_data_regression/test_example__json_.json +++ b/tests/test_data_regression/test_example__json_.json @@ -1,4 +1,4 @@ { "contents": "Foo", "value": 11 -} \ No newline at end of file +} diff --git a/tests/test_data_regression/test_round_digits__json_.json b/tests/test_data_regression/test_round_digits__json_.json index 3fa1033..a7593a6 100644 --- a/tests/test_data_regression/test_round_digits__json_.json +++ b/tests/test_data_regression/test_round_digits__json_.json @@ -8,4 +8,4 @@ 1.12, 2.35 ] -} \ No newline at end of file +} From 2a7ea5e9889a4a868e2e020c258afcd418a426e7 Mon Sep 17 00:00:00 2001 From: Andrew Mitchell Date: Fri, 24 Apr 2026 13:49:13 +0100 Subject: [PATCH 11/12] Make indent configurable in data regression functions --- src/pytest_regressions/data_regression.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/pytest_regressions/data_regression.py b/src/pytest_regressions/data_regression.py index fa8cfd1..e3685d7 100644 --- a/src/pytest_regressions/data_regression.py +++ b/src/pytest_regressions/data_regression.py @@ -44,6 +44,8 @@ def check( fullpath: Optional["os.PathLike[str]"] = None, round_digits: int | None = None, extension: str = ".yml", + *, + indent: int = 2 ) -> None: """ Checks the given dict against a previously recorded version, or generate a new file. @@ -82,14 +84,14 @@ def dump(filename: Path) -> None: Dumper=RegressionYamlDumper, default_flow_style=False, allow_unicode=True, - indent=2, + indent=indent, encoding="utf-8", ) with filename.open("wb") as f: f.write(dumped_str) elif extension.lower() == ".json": dumped_str = json.dumps( - data_dict, indent=2, sort_keys=True, ensure_ascii=False + data_dict, indent=indent, sort_keys=True, ensure_ascii=False ) with filename.open("w", encoding="utf-8") as f: f.write(dumped_str) From 918f3ab6dc3db1b92d5bb86b76875dda8d6c7519 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 24 Apr 2026 12:49:26 +0000 Subject: [PATCH 12/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/pytest_regressions/data_regression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytest_regressions/data_regression.py b/src/pytest_regressions/data_regression.py index e3685d7..0cd44d8 100644 --- a/src/pytest_regressions/data_regression.py +++ b/src/pytest_regressions/data_regression.py @@ -45,7 +45,7 @@ def check( round_digits: int | None = None, extension: str = ".yml", *, - indent: int = 2 + indent: int = 2, ) -> None: """ Checks the given dict against a previously recorded version, or generate a new file.