Skip to content

Commit b26d1bb

Browse files
committed
cacheprovider: add cache.mkdir() as a Path-returning replacement to makedir()
It is not possible to change a return type in a compatible way, so a new method is added.
1 parent 2641761 commit b26d1bb

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

changelog/7259.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added :meth:`cache.mkdir() <pytest.Cache.mkdir>`, which is similar to the existing :meth:`cache.makedir() <pytest.Cache.makedir>`,
2+
but returns a :class:`pathlib.Path` instead of a legacy ``py.path.local``.

src/_pytest/cacheprovider.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ class Cache:
6060
_cachedir = attr.ib(type=Path, repr=False)
6161
_config = attr.ib(type=Config, repr=False)
6262

63-
# sub-directory under cache-dir for directories created by "makedir"
63+
# Sub-directory under cache-dir for directories created by `mkdir()`.
6464
_CACHE_PREFIX_DIRS = "d"
6565

66-
# sub-directory under cache-dir for values created by "set"
66+
# Sub-directory under cache-dir for values created by `set()`.
6767
_CACHE_PREFIX_VALUES = "v"
6868

6969
def __init__(
@@ -121,13 +121,15 @@ def warn(self, fmt: str, *, _ispytest: bool = False, **args: object) -> None:
121121
stacklevel=3,
122122
)
123123

124-
def makedir(self, name: str) -> LEGACY_PATH:
124+
def mkdir(self, name: str) -> Path:
125125
"""Return a directory path object with the given name.
126126
127127
If the directory does not yet exist, it will be created. You can use
128128
it to manage files to e.g. store/retrieve database dumps across test
129129
sessions.
130130
131+
.. versionadded:: 6.3
132+
131133
:param name:
132134
Must be a string not containing a ``/`` separator.
133135
Make sure the name contains your plugin or application
@@ -138,7 +140,14 @@ def makedir(self, name: str) -> LEGACY_PATH:
138140
raise ValueError("name is not allowed to contain path separators")
139141
res = self._cachedir.joinpath(self._CACHE_PREFIX_DIRS, path)
140142
res.mkdir(exist_ok=True, parents=True)
141-
return legacy_path(res)
143+
return res
144+
145+
def makedir(self, name: str) -> LEGACY_PATH:
146+
"""Return a directory path object with the given name.
147+
148+
Same as :func:`mkdir`, but returns a legacy py path instance.
149+
"""
150+
return legacy_path(self.mkdir(name))
142151

143152
def _getvaluepath(self, key: str) -> Path:
144153
return self._cachedir.joinpath(self._CACHE_PREFIX_VALUES, Path(key))
@@ -572,8 +581,8 @@ def cacheshow(config: Config, session: Session) -> int:
572581
contents = sorted(ddir.rglob(glob))
573582
tw.sep("-", "cache directories for %r" % glob)
574583
for p in contents:
575-
# if p.check(dir=1):
576-
# print("%s/" % p.relto(basedir))
584+
# if p.is_dir():
585+
# print("%s/" % p.relative_to(basedir))
577586
if p.is_file():
578587
key = str(p.relative_to(basedir))
579588
tw.line(f"{key} is a file of length {p.stat().st_size:d}")

testing/test_cacheprovider.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515

1616
class TestNewAPI:
17-
def test_config_cache_makedir(self, pytester: Pytester) -> None:
17+
def test_config_cache_mkdir(self, pytester: Pytester) -> None:
1818
pytester.makeini("[pytest]")
1919
config = pytester.parseconfigure()
2020
assert config.cache is not None
2121
with pytest.raises(ValueError):
22-
config.cache.makedir("key/name")
22+
config.cache.mkdir("key/name")
2323

24-
p = config.cache.makedir("name")
25-
assert p.check()
24+
p = config.cache.mkdir("name")
25+
assert p.is_dir()
2626

2727
def test_config_cache_dataerror(self, pytester: Pytester) -> None:
2828
pytester.makeini("[pytest]")
@@ -217,9 +217,9 @@ def pytest_configure(config):
217217
config.cache.set("my/name", [1,2,3])
218218
config.cache.set("my/hello", "world")
219219
config.cache.set("other/some", {1:2})
220-
dp = config.cache.makedir("mydb")
221-
dp.ensure("hello")
222-
dp.ensure("world")
220+
dp = config.cache.mkdir("mydb")
221+
dp.joinpath("hello").touch()
222+
dp.joinpath("world").touch()
223223
"""
224224
)
225225
result = pytester.runpytest()

0 commit comments

Comments
 (0)