Skip to content

Commit e9969a3

Browse files
committed
Fix patching in pytest setup phase
- relevant for module and session-scoped fs fixtures
1 parent 3d488c8 commit e9969a3

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

.github/workflows/testsuite.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ jobs:
134134
run: |
135135
echo "$(python -m pytest pyfakefs/pytest_tests/pytest_plugin_failing_helper.py)" > ./testresult.txt
136136
pytest pyfakefs/pytest_tests
137+
pytest pyfakefs/pytest_session_tests
137138
cd pyfakefs/pytest_tests/ns_package
138139
pytest --log-cli-level=INFO test
139140
shell: bash

CHANGES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ The released versions correspond to PyPI releases.
2525
* fixed a regression that could break tests under Posix in Python 3.12
2626
(see [#1126](../../issues/1126))
2727
* fixed behavior for `os.access` for symlinks under Windows
28-
* fixes permission problem on querying file properties (see [#1122](../../issues/1122))
28+
* fixed permission problem on querying file properties (see [#1122](../../issues/1122))
29+
* fixed patching in pytest setup phase for module and session-scoped fs fixtures
30+
(see [#1126](../../issues/1126))
2931

3032
### Documentation
3133
* use a theme for documentation supporting dark mode

pyfakefs/pytest_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def pytest_runtest_logreport(report):
9292

9393

9494
@pytest.hookimpl(hookwrapper=True, trylast=True)
95-
def pytest_runtest_call(item):
95+
def pytest_runtest_setup(item):
9696
if Patcher.PATCHER is not None:
9797
Patcher.PATCHER.resume()
9898
yield

pyfakefs/pytest_session_tests/__init__.py

Whitespace-only changes.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
from pyfakefs.fake_filesystem import FakeFilesystem
6+
7+
8+
def pytest_generate_tests(metafunc):
9+
handlers = [a, b, c]
10+
if "handler_class" in metafunc.fixturenames:
11+
metafunc.parametrize("handler_class", handlers)
12+
13+
14+
def a():
15+
pass
16+
17+
18+
def b():
19+
pass
20+
21+
22+
def c():
23+
pass
24+
25+
26+
@pytest.fixture
27+
def class_a():
28+
pass
29+
30+
31+
@pytest.fixture
32+
def class_b():
33+
pass
34+
35+
36+
@pytest.fixture
37+
def class_c():
38+
pass
39+
40+
41+
@pytest.fixture
42+
def make_handler(request):
43+
def _make_handler(cls):
44+
return request.getfixturevalue(f"class_{cls.__name__}")
45+
46+
yield _make_handler
47+
48+
49+
@pytest.fixture
50+
def handler_and_check(handler_class, make_handler):
51+
assert Path("/foo/bar").exists()
52+
yield
53+
54+
55+
def test_handler_and_check_in_fixture(handler_and_check):
56+
assert Path("/foo/bar").exists()
57+
58+
59+
@pytest.fixture(scope="session", autouse=True)
60+
def config(fs_session: FakeFilesystem):
61+
fs_session.create_file("/foo/bar")

0 commit comments

Comments
 (0)