Skip to content

Commit 2df4e2b

Browse files
pre-commit-ci[bot]Tusenka
authored andcommitted
13403: Disable assertion rewriting for external modules
1 parent 03db745 commit 2df4e2b

File tree

15 files changed

+123
-1
lines changed

15 files changed

+123
-1
lines changed

changelog/13403.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Disable assertion for modules outside current working dir(cwd)

changelog/13492.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed outdated warning about ``faulthandler`` not working on Windows.

src/_pytest/assertion/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class AssertionState:
110110
def __init__(self, config: Config, mode) -> None:
111111
self.mode = mode
112112
self.trace = config.trace.root.get("assertion")
113+
self.invocation_path = str(config.invocation_params.dir)
113114
self.hook: rewrite.AssertionRewritingHook | None = None
114115

115116

src/_pytest/assertion/rewrite.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ def _should_rewrite(self, name: str, fn: str, state: AssertionState) -> bool:
239239
# rewritten if they match the naming convention for test files
240240
fn_path = PurePath(fn)
241241
for pat in self.fnpats:
242-
if fnmatch_ex(pat, fn_path):
242+
if fnmatch_ex(pat, fn_path) and fn_path.is_relative_to(
243+
state.invocation_path
244+
):
243245
state.trace(f"matched test file {fn!r}")
244246
return True
245247

src/_pytest/pytester.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
from _pytest import timing
4242
from _pytest._code import Source
43+
from _pytest.assertion.rewrite import assertstate_key
4344
from _pytest.capture import _get_multicapture
4445
from _pytest.compat import NOTSET
4546
from _pytest.compat import NotSetType
@@ -751,6 +752,9 @@ def chdir(self) -> None:
751752
This is done automatically upon instantiation.
752753
"""
753754
self._monkeypatch.chdir(self.path)
755+
self._monkeypatch.setattr(
756+
self._request.config.stash[assertstate_key], "invocation_path", self.path
757+
)
754758

755759
def _makefile(
756760
self,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
python_files = *.py
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
5+
6+
@pytest.fixture
7+
def special_asserter():
8+
def special_assert(x, y):
9+
assert {"x": x} == {"x": y}
10+
11+
return special_assert

testing/example_scripts/rewrite/src/__init__.py

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""Stub file for testing"""
2+
3+
from __future__ import annotations
4+
5+
6+
def func(x: int, y: int) -> int:
7+
"""Stub function"""
8+
assert (x) > 0
9+
return 0 if x == y else 1 if x > y else -1

testing/example_scripts/rewrite/tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)