From 91e40ea8c4a83ff5b74067ca6d1828e83aaf9d27 Mon Sep 17 00:00:00 2001 From: Christian Dassy <18606875+inxbit@users.noreply.github.com> Date: Thu, 5 Nov 2026 15:33:50 -0500 Subject: [PATCH] tests: scope blanket os fakes so pytest 9.2 capture still works pytest 9.2.0 FDCapture.snap() calls os.fstat(tmpfile).st_size after the test body returns and before fixture teardown. Three export tests replaced os.fstat (and os.lstat, os.open, os.close) for the whole call phase with fakes that have no st_size, so pytest's own capture crashed with AttributeError on every CI job of the dependabot python-tooling PR #171. Apply those fakes inside monkeypatch.context() so they are undone before the test returns. Assertions are unchanged. --- tests/test_export.py | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/tests/test_export.py b/tests/test_export.py index 8ec79d4..a6d6698 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -218,13 +218,16 @@ def test_existing_special_device_rejects_identity_change( after = SimpleNamespace(st_mode=stat.S_IFIFO, st_dev=1, st_ino=11, st_uid=os.geteuid()) closed: list[int] = [] - monkeypatch.setattr(os, "lstat", lambda _path: before) - monkeypatch.setattr(os, "open", lambda _path, _flags: 99) - monkeypatch.setattr(os, "fstat", lambda _fd: after) - monkeypatch.setattr(os, "close", closed.append) + # Undo the fakes before the test returns: pytest's own output capture calls + # os.fstat once the test body ends, before fixture teardown. + with monkeypatch.context() as patch: + patch.setattr(os, "lstat", lambda _path: before) + patch.setattr(os, "open", lambda _path, _flags: 99) + patch.setattr(os, "fstat", lambda _fd: after) + patch.setattr(os, "close", closed.append) - with pytest.raises(OSError, match="changed while opening"): - export_module._existing_special_device_fd(Path("out.fifo")) + with pytest.raises(OSError, match="changed while opening"): + export_module._existing_special_device_fd(Path("out.fifo")) assert closed == [99] @@ -284,13 +287,14 @@ def test_existing_special_device_rejects_fifo_owner_change_after_open( after = SimpleNamespace(st_mode=stat.S_IFIFO, st_dev=1, st_ino=10, st_uid=os.geteuid() + 1) closed: list[int] = [] - monkeypatch.setattr(os, "lstat", lambda _path: before) - monkeypatch.setattr(os, "open", lambda _path, _flags: 99) - monkeypatch.setattr(os, "fstat", lambda _fd: after) - monkeypatch.setattr(os, "close", closed.append) + with monkeypatch.context() as patch: + patch.setattr(os, "lstat", lambda _path: before) + patch.setattr(os, "open", lambda _path, _flags: 99) + patch.setattr(os, "fstat", lambda _fd: after) + patch.setattr(os, "close", closed.append) - with pytest.raises(PermissionError, match="FIFO owned by another user"): - export_module._existing_special_device_fd(Path("out.fifo")) + with pytest.raises(PermissionError, match="FIFO owned by another user"): + export_module._existing_special_device_fd(Path("out.fifo")) assert closed == [99] @@ -371,13 +375,14 @@ def test_existing_special_device_rechecks_fifo_mode_on_the_opened_node( ) closed: list[int] = [] - monkeypatch.setattr(os, "lstat", lambda _path: before) - monkeypatch.setattr(os, "open", lambda _path, _flags: 99) - monkeypatch.setattr(os, "fstat", lambda _fd: after) - monkeypatch.setattr(os, "close", closed.append) + with monkeypatch.context() as patch: + patch.setattr(os, "lstat", lambda _path: before) + patch.setattr(os, "open", lambda _path, _flags: 99) + patch.setattr(os, "fstat", lambda _fd: after) + patch.setattr(os, "close", closed.append) - with pytest.raises(PermissionError, match=r"other users can open \(mode 0644\)"): - export_module._existing_special_device_fd(Path("out.fifo")) + with pytest.raises(PermissionError, match=r"other users can open \(mode 0644\)"): + export_module._existing_special_device_fd(Path("out.fifo")) assert closed == [99]