Skip to content

Commit 2b671b5

Browse files
bluetechpytestbot
authored andcommitted
[8.2.x] cacheprovider: fix .pytest_cache not being world-readable
1 parent 65ab7cb commit 2b671b5

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Diff for: changelog/12308.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a regression in pytest 8.2.0 where the permissions of automatically-created ``.pytest_cache`` directories became ``rwx------`` instead of the expected ``rwxr-xr-x``.

Diff for: src/_pytest/cacheprovider.py

+7
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ def _ensure_cache_dir_and_supporting_files(self) -> None:
213213
dir=self._cachedir.parent,
214214
) as newpath:
215215
path = Path(newpath)
216+
217+
# Reset permissions to the default, see #12308.
218+
# Note: there's no way to get the current umask atomically, eek.
219+
umask = os.umask(0o022)
220+
os.umask(umask)
221+
path.chmod(0o777 - umask)
222+
216223
with open(path.joinpath("README.md"), "xt", encoding="UTF-8") as f:
217224
f.write(README_CONTENT)
218225
with open(path.joinpath(".gitignore"), "xt", encoding="UTF-8") as f:

Diff for: testing/test_cacheprovider.py

+15
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ def test_config_cache_mkdir(self, pytester: Pytester) -> None:
3131
p = config.cache.mkdir("name")
3232
assert p.is_dir()
3333

34+
def test_cache_dir_permissions(self, pytester: Pytester) -> None:
35+
"""The .pytest_cache directory should have world-readable permissions
36+
(depending on umask).
37+
38+
Regression test for #12308.
39+
"""
40+
pytester.makeini("[pytest]")
41+
config = pytester.parseconfigure()
42+
assert config.cache is not None
43+
p = config.cache.mkdir("name")
44+
assert p.is_dir()
45+
# Instead of messing with umask, make sure .pytest_cache has the same
46+
# permissions as the default that `mkdir` gives `p`.
47+
assert (p.parent.stat().st_mode & 0o777) == (p.stat().st_mode & 0o777)
48+
3449
def test_config_cache_dataerror(self, pytester: Pytester) -> None:
3550
pytester.makeini("[pytest]")
3651
config = pytester.parseconfigure()

0 commit comments

Comments
 (0)