Skip to content

Commit 32bd032

Browse files
committed
test: cover tmpdir get_user OSError fallback
1 parent 5dfd4ea commit 32bd032

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

testing/test_tmpdir.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,30 @@ def test_get_user_uid_not_found():
357357
assert get_user() is None
358358

359359

360+
def test_get_user_handles_oserror(monkeypatch: MonkeyPatch) -> None:
361+
"""Test that get_user() handles getpass.getuser() raising OSError.
362+
363+
Python 3.13+ consistently raises OSError when no username can be
364+
determined from the environment (for example on Windows with no username
365+
environment variables set).
366+
"""
367+
368+
def raise_oserror() -> str:
369+
raise OSError("No username set in the environment")
370+
371+
monkeypatch.setattr("getpass.getuser", raise_oserror)
372+
373+
assert get_user() is None
374+
375+
360376
@pytest.mark.skipif(not sys.platform.startswith("win"), reason="win only")
361-
def test_get_user(monkeypatch):
362-
"""Test that get_user() function works even if environment variables
363-
required by getpass module are missing from the environment on Windows
364-
(#1010).
377+
def test_get_user(monkeypatch: MonkeyPatch) -> None:
378+
"""Test that get_user() works even if the environment variables
379+
required by getpass are missing from the environment on Windows (#1010,
380+
#13835).
365381
"""
366-
monkeypatch.delenv("USER", raising=False)
367-
monkeypatch.delenv("USERNAME", raising=False)
382+
for envvar in ("LOGNAME", "USER", "LNAME", "USERNAME"):
383+
monkeypatch.delenv(envvar, raising=False)
368384
assert get_user() is None
369385

370386

0 commit comments

Comments
 (0)