Skip to content

Commit fab22c9

Browse files
committed
changes based on PR comments
1 parent 7237926 commit fab22c9

File tree

6 files changed

+22
-18
lines changed

6 files changed

+22
-18
lines changed

doc/en/how-to/tmp_path.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ File permissions
163163

164164
Any file or directory created by the above fixtures are by default created with private permissions (file mask 700).
165165

166-
You can override the file mask by setting the :envvar:`PYTEST_TMPDIR_FILE_MODE` environment variable as an octal string, the default being `0o700`.
166+
You can override the file mask by setting the :envvar:`PYTEST_TMPDIR_FILE_MODE` environment variable as an octal string, the default being `700`.
167167

168168
This is for example useful in cases where created files or directories have to be shared by docker containers etc.
169169

doc/en/reference/reference.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ Sets the :envvar:`PYTEST_THEME` to be either *dark* or *light*.
10921092

10931093
.. envvar:: PYTEST_TMPDIR_FILE_MODE
10941094

1095-
Sets the file mode of any temporary files or directories. Defaults to `0o700`. See :fixture:`tmp_path` :fixture:`tmp_path_factory`.
1095+
Sets the file mode of any temporary files or directories. Defaults to `700`. See :fixture:`tmp_path` :fixture:`tmp_path_factory`.
10961096

10971097
.. envvar:: PY_COLORS
10981098

src/_pytest/pathlib.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import shutil
88
import sys
9+
import typing
910
import uuid
1011
import warnings
1112
from enum import Enum
@@ -51,8 +52,6 @@
5152
1921, # ERROR_CANT_RESOLVE_FILENAME - fix for broken symlink pointing to itself
5253
)
5354

54-
TMPDIR_FILE_MODE = int(os.getenv("PYTEST_TMPDIR_FILE_MODE", "0o700"), 8)
55-
5655

5756
def _ignore_error(exception):
5857
return (
@@ -208,8 +207,11 @@ def _force_symlink(
208207
pass
209208

210209

211-
def make_numbered_dir(root: Path, prefix: str, mode: int = TMPDIR_FILE_MODE) -> Path:
210+
def make_numbered_dir(
211+
root: Path, prefix: str, mode: typing.Union[int, None] = None
212+
) -> Path:
212213
"""Create a directory with an increased number as suffix for the given prefix."""
214+
mode = mode or tmpdir_file_mode()
213215
for i in range(10):
214216
# try up to 10 times to create the folder
215217
max_existing = max(map(parse_num, find_suffixes(root, prefix)), default=-1)
@@ -748,3 +750,7 @@ def copytree(source: Path, target: Path) -> None:
748750
shutil.copyfile(x, newx)
749751
elif x.is_dir():
750752
newx.mkdir(exist_ok=True)
753+
754+
755+
def tmpdir_file_mode() -> int:
756+
return int(os.getenv("PYTEST_TMPDIR_FILE_MODE", "700"), 8)

src/_pytest/pytester.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
from _pytest.pathlib import bestrelpath
6363
from _pytest.pathlib import copytree
6464
from _pytest.pathlib import make_numbered_dir
65+
from _pytest.pathlib import tmpdir_file_mode
6566
from _pytest.reports import CollectReport
6667
from _pytest.reports import TestReport
6768
from _pytest.tmpdir import TempPathFactory
@@ -82,8 +83,6 @@
8283
"/var/lib/sss/mc/passwd"
8384
]
8485

85-
TMPDIR_FILE_MODE = int(os.getenv("PYTEST_TMPDIR_FILE_MODE", "0o700"), 8)
86-
8786

8887
def pytest_addoption(parser: Parser) -> None:
8988
parser.addoption(
@@ -1505,7 +1504,7 @@ def runpytest_subprocess(
15051504
"""
15061505
__tracebackhide__ = True
15071506
p = make_numbered_dir(
1508-
root=self.path, prefix="runpytest-", mode=TMPDIR_FILE_MODE
1507+
root=self.path, prefix="runpytest-", mode=tmpdir_file_mode()
15091508
)
15101509
args = ("--basetemp=%s" % p,) + args
15111510
plugins = [x for x in self.plugins if isinstance(x, str)]
@@ -1525,7 +1524,7 @@ def spawn_pytest(
15251524
The pexpect child is returned.
15261525
"""
15271526
basetemp = self.path / "temp-pexpect"
1528-
basetemp.mkdir(mode=TMPDIR_FILE_MODE)
1527+
basetemp.mkdir(mode=tmpdir_file_mode())
15291528
invoke = " ".join(map(str, self._getpytestargs()))
15301529
cmd = f"{invoke} --basetemp={basetemp} {string}"
15311530
return self.spawn(cmd, expect_timeout=expect_timeout)

src/_pytest/tmpdir.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from .pathlib import make_numbered_dir_with_cleanup
3131
from .pathlib import rm_rf
3232
from .pathlib import cleanup_dead_symlink
33+
from .pathlib import tmpdir_file_mode
3334
from _pytest.compat import final
3435
from _pytest.config import Config
3536
from _pytest.config import ExitCode
@@ -41,8 +42,6 @@
4142

4243
tmppath_result_key = StashKey[Dict[str, bool]]()
4344

44-
TMPDIR_FILE_MODE = int(os.getenv("PYTEST_TMPDIR_FILE_MODE", "0o700"), 8)
45-
4645

4746
@final
4847
@dataclasses.dataclass
@@ -138,10 +137,10 @@ def mktemp(self, basename: str, numbered: bool = True) -> Path:
138137
basename = self._ensure_relative_to_basetemp(basename)
139138
if not numbered:
140139
p = self.getbasetemp().joinpath(basename)
141-
p.mkdir(mode=TMPDIR_FILE_MODE)
140+
p.mkdir(mode=tmpdir_file_mode())
142141
else:
143142
p = make_numbered_dir(
144-
root=self.getbasetemp(), prefix=basename, mode=TMPDIR_FILE_MODE
143+
root=self.getbasetemp(), prefix=basename, mode=tmpdir_file_mode()
145144
)
146145
self._trace("mktemp", p)
147146
return p
@@ -159,7 +158,7 @@ def getbasetemp(self) -> Path:
159158
basetemp = self._given_basetemp
160159
if basetemp.exists():
161160
rm_rf(basetemp)
162-
basetemp.mkdir(mode=TMPDIR_FILE_MODE)
161+
basetemp.mkdir(mode=tmpdir_file_mode())
163162
basetemp = basetemp.resolve()
164163
else:
165164
from_env = os.environ.get("PYTEST_DEBUG_TEMPROOT")
@@ -169,11 +168,11 @@ def getbasetemp(self) -> Path:
169168
# make_numbered_dir() call
170169
rootdir = temproot.joinpath(f"pytest-of-{user}")
171170
try:
172-
rootdir.mkdir(mode=TMPDIR_FILE_MODE, exist_ok=True)
171+
rootdir.mkdir(mode=tmpdir_file_mode(), exist_ok=True)
173172
except OSError:
174173
# getuser() likely returned illegal characters for the platform, use unknown back off mechanism
175174
rootdir = temproot.joinpath("pytest-of-unknown")
176-
rootdir.mkdir(mode=TMPDIR_FILE_MODE, exist_ok=True)
175+
rootdir.mkdir(mode=tmpdir_file_mode(), exist_ok=True)
177176
# Because we use exist_ok=True with a predictable name, make sure
178177
# we are the owners, to prevent any funny business (on unix, where
179178
# temproot is usually shared).
@@ -201,7 +200,7 @@ def getbasetemp(self) -> Path:
201200
root=rootdir,
202201
keep=keep,
203202
lock_timeout=LOCK_TIMEOUT,
204-
mode=TMPDIR_FILE_MODE,
203+
mode=tmpdir_file_mode(),
205204
)
206205
assert basetemp is not None, basetemp
207206
self._basetemp = basetemp

testing/test_tmpdir.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ def test_tmp_path_factory_user_specified_permissions(
631631
"""Verify that pytest creates directories under /tmp with user specified permissions."""
632632
# Use the test's tmp_path as the system temproot (/tmp).
633633
monkeypatch.setenv("PYTEST_DEBUG_TEMPROOT", str(tmp_path))
634-
monkeypatch.setenv("PYTEST_TMPDIR_FILE_MODE", "0o777")
634+
monkeypatch.setenv("PYTEST_TMPDIR_FILE_MODE", "777")
635635
tmp_factory = TempPathFactory(None, 3, "all", lambda *args: None, _ispytest=True)
636636
basetemp = tmp_factory.getbasetemp()
637637

0 commit comments

Comments
 (0)