Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.2.x] cacheprovider: fix .pytest_cache not being world-readable #12327

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/12308.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +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``.
7 changes: 7 additions & 0 deletions src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ def _ensure_cache_dir_and_supporting_files(self) -> None:
dir=self._cachedir.parent,
) as newpath:
path = Path(newpath)

# Reset permissions to the default, see #12308.
# Note: there's no way to get the current umask atomically, eek.
umask = os.umask(0o022)
os.umask(umask)
path.chmod(0o777 - umask)

with open(path.joinpath("README.md"), "xt", encoding="UTF-8") as f:
f.write(README_CONTENT)
with open(path.joinpath(".gitignore"), "xt", encoding="UTF-8") as f:
Expand Down
15 changes: 15 additions & 0 deletions testing/test_cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ def test_config_cache_mkdir(self, pytester: Pytester) -> None:
p = config.cache.mkdir("name")
assert p.is_dir()

def test_cache_dir_permissions(self, pytester: Pytester) -> None:
"""The .pytest_cache directory should have world-readable permissions
(depending on umask).

Regression test for #12308.
"""
pytester.makeini("[pytest]")
config = pytester.parseconfigure()
assert config.cache is not None
p = config.cache.mkdir("name")
assert p.is_dir()
# Instead of messing with umask, make sure .pytest_cache has the same
# permissions as the default that `mkdir` gives `p`.
assert (p.parent.stat().st_mode & 0o777) == (p.stat().st_mode & 0o777)

def test_config_cache_dataerror(self, pytester: Pytester) -> None:
pytester.makeini("[pytest]")
config = pytester.parseconfigure()
Expand Down
Loading