Fixed an issue where `getpass.getuser()` contained illegal characters for file directories (#8365)
* retry writing pytest-of dir when invalid chars are in directory name * add unit tests for getbasetemp() and changelog * patch _basetemp & _given_basetemp for testing basetemp() * Tweak changelog for #8317, tidy up comments
This commit is contained in:
parent
bbea18d7f9
commit
b7f2d7ca61
|
@ -0,0 +1 @@
|
||||||
|
Fixed an issue where illegal directory characters derived from ``getpass.getuser()`` raised an ``OSError``.
|
|
@ -115,6 +115,11 @@ class TempPathFactory:
|
||||||
# use a sub-directory in the temproot to speed-up
|
# use a sub-directory in the temproot to speed-up
|
||||||
# make_numbered_dir() call
|
# make_numbered_dir() call
|
||||||
rootdir = temproot.joinpath(f"pytest-of-{user}")
|
rootdir = temproot.joinpath(f"pytest-of-{user}")
|
||||||
|
try:
|
||||||
|
rootdir.mkdir(exist_ok=True)
|
||||||
|
except OSError:
|
||||||
|
# getuser() likely returned illegal characters for the platform, use unknown back off mechanism
|
||||||
|
rootdir = temproot.joinpath("pytest-of-unknown")
|
||||||
rootdir.mkdir(exist_ok=True)
|
rootdir.mkdir(exist_ok=True)
|
||||||
basetemp = make_numbered_dir_with_cleanup(
|
basetemp = make_numbered_dir_with_cleanup(
|
||||||
prefix="pytest-", root=rootdir, keep=3, lock_timeout=LOCK_TIMEOUT
|
prefix="pytest-", root=rootdir, keep=3, lock_timeout=LOCK_TIMEOUT
|
||||||
|
|
|
@ -11,6 +11,7 @@ import attr
|
||||||
import pytest
|
import pytest
|
||||||
from _pytest import pathlib
|
from _pytest import pathlib
|
||||||
from _pytest.config import Config
|
from _pytest.config import Config
|
||||||
|
from _pytest.monkeypatch import MonkeyPatch
|
||||||
from _pytest.pathlib import cleanup_numbered_dir
|
from _pytest.pathlib import cleanup_numbered_dir
|
||||||
from _pytest.pathlib import create_cleanup_lock
|
from _pytest.pathlib import create_cleanup_lock
|
||||||
from _pytest.pathlib import make_numbered_dir
|
from _pytest.pathlib import make_numbered_dir
|
||||||
|
@ -445,3 +446,14 @@ def test_basetemp_with_read_only_files(pytester: Pytester) -> None:
|
||||||
# running a second time and ensure we don't crash
|
# running a second time and ensure we don't crash
|
||||||
result = pytester.runpytest("--basetemp=tmp")
|
result = pytester.runpytest("--basetemp=tmp")
|
||||||
assert result.ret == 0
|
assert result.ret == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_tmp_path_factory_handles_invalid_dir_characters(
|
||||||
|
tmp_path_factory: TempPathFactory, monkeypatch: MonkeyPatch
|
||||||
|
) -> None:
|
||||||
|
monkeypatch.setattr("getpass.getuser", lambda: "os/<:*?;>agnostic")
|
||||||
|
# _basetemp / _given_basetemp are cached / set in parallel runs, patch them
|
||||||
|
monkeypatch.setattr(tmp_path_factory, "_basetemp", None)
|
||||||
|
monkeypatch.setattr(tmp_path_factory, "_given_basetemp", None)
|
||||||
|
p = tmp_path_factory.getbasetemp()
|
||||||
|
assert "pytest-of-unknown" in str(p)
|
||||||
|
|
Loading…
Reference in New Issue