Cache.set preserves key order when saving dicts (#9206)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
This commit is contained in:
parent
0696d3eda1
commit
cbcfeca78c
1
AUTHORS
1
AUTHORS
|
@ -76,6 +76,7 @@ Christopher Gilling
|
||||||
Claire Cecil
|
Claire Cecil
|
||||||
Claudio Madotto
|
Claudio Madotto
|
||||||
CrazyMerlyn
|
CrazyMerlyn
|
||||||
|
Cristian Vera
|
||||||
Cyrus Maden
|
Cyrus Maden
|
||||||
Damian Skrzypczak
|
Damian Skrzypczak
|
||||||
Daniel Grana
|
Daniel Grana
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
:meth:`pytest.Cache.set` now preserves key order when saving dicts.
|
|
@ -193,7 +193,7 @@ class Cache:
|
||||||
return
|
return
|
||||||
if not cache_dir_exists_already:
|
if not cache_dir_exists_already:
|
||||||
self._ensure_supporting_files()
|
self._ensure_supporting_files()
|
||||||
data = json.dumps(value, indent=2, sort_keys=True)
|
data = json.dumps(value, indent=2)
|
||||||
try:
|
try:
|
||||||
f = path.open("w")
|
f = path.open("w")
|
||||||
except OSError:
|
except OSError:
|
||||||
|
|
|
@ -1210,6 +1210,17 @@ def test_gitignore(pytester: Pytester) -> None:
|
||||||
assert gitignore_path.read_text(encoding="UTF-8") == "custom"
|
assert gitignore_path.read_text(encoding="UTF-8") == "custom"
|
||||||
|
|
||||||
|
|
||||||
|
def test_preserve_keys_order(pytester: Pytester) -> None:
|
||||||
|
"""Ensure keys order is preserved when saving dicts (#9205)."""
|
||||||
|
from _pytest.cacheprovider import Cache
|
||||||
|
|
||||||
|
config = pytester.parseconfig()
|
||||||
|
cache = Cache.for_config(config, _ispytest=True)
|
||||||
|
cache.set("foo", {"z": 1, "b": 2, "a": 3, "d": 10})
|
||||||
|
read_back = cache.get("foo", None)
|
||||||
|
assert list(read_back.items()) == [("z", 1), ("b", 2), ("a", 3), ("d", 10)]
|
||||||
|
|
||||||
|
|
||||||
def test_does_not_create_boilerplate_in_existing_dirs(pytester: Pytester) -> None:
|
def test_does_not_create_boilerplate_in_existing_dirs(pytester: Pytester) -> None:
|
||||||
from _pytest.cacheprovider import Cache
|
from _pytest.cacheprovider import Cache
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue