Skip to content

Commit cd3dc82

Browse files
committed
Use hookwrapper to pause patching during logreport
- shall fix #904 more reliably
1 parent bbb7ad6 commit cd3dc82

File tree

5 files changed

+70
-14
lines changed

5 files changed

+70
-14
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ The released versions correspond to PyPI releases.
99
(see [#912](../../issues/912))
1010
* fixed result of `os.walk` with a path-like top directory
1111
(see [#915](../../issues/915))
12+
* properly fixed the problem that filesystem patching was still active in the pytest
13+
logreport phase (see [#904](../../issues/904)), the previous fix was incomplete
1214

1315
## [Version 5.3.1](https://pypi.python.org/pypi/pyfakefs/5.3.0) (2023-11-15)
1416
Mostly a bugfix release.

pyfakefs/pytest_plugin.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,12 @@ def pytest_sessionfinish(session, exitstatus):
8282
Patcher.clear_fs_cache()
8383

8484

85-
@pytest.hookimpl(tryfirst=True)
85+
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
8686
def pytest_runtest_logreport(report):
8787
"""Make sure that patching is not active during reporting."""
88-
if report.when == "call" and Patcher.PATCHER is not None:
88+
pause = Patcher.PATCHER is not None and report.when == "call"
89+
if pause:
8990
Patcher.PATCHER.pause()
90-
91-
92-
def pytest_runtest_call(item):
93-
"""Resume paused patching before test start."""
94-
if Patcher.PATCHER is not None:
91+
yield
92+
if pause:
9593
Patcher.PATCHER.resume()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
from pathlib import Path
13+
14+
import pytest
15+
16+
17+
# Used for testing paused patching during reporting.
18+
19+
20+
@pytest.hookimpl
21+
def pytest_runtest_logreport(report):
22+
if report.when == "call":
23+
report_path = Path(__file__).parent / "report.txt"
24+
with open(report_path, "w") as f:
25+
f.write("Test")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
from pathlib import Path
13+
14+
import pytest
15+
16+
17+
@pytest.fixture
18+
def report_path():
19+
yield Path(__file__).parent / "report.txt"
20+
21+
22+
def test_1(fs):
23+
pass
24+
25+
26+
def test_2_report_in_real_fs(report_path):
27+
print("test_2_report_in_real_fs")
28+
assert report_path.exists()
29+
report_path.unlink()
30+
31+
32+
def test_3(fs):
33+
pass
34+
35+
36+
def test_4_report_in_real_fs(report_path):
37+
assert report_path.exists()
38+
report_path.unlink()

pyfakefs/pytest_tests/pytest_module_fixture_test.py

-7
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ def use_fs(fs_module):
2020
yield fs_module
2121

2222

23-
@pytest.fixture(autouse=True)
24-
def check_patching_stopped(fs):
25-
# patching shall be paused at test end, even in module scope (see #904)
26-
yield
27-
assert not fs.patcher.is_patching
28-
29-
3023
@pytest.mark.usefixtures("fs")
3124
def test_fs_uses_fs_module1():
3225
# check that `fs` uses the same filesystem as `fs_module`

0 commit comments

Comments
 (0)