Skip to content

Commit fa44ff9

Browse files
committed
13403: Disable assertion rewriting for external modules - add tests
1 parent 6ec5c30 commit fa44ff9

File tree

15 files changed

+52
-11
lines changed

15 files changed

+52
-11
lines changed

changelog/13403.bugfix.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Disable assertion rewriting of external modules
1+
Disable assertion for modules outside current working dir(cwd)

src/_pytest/assertion/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def __init__(self, config: Config, mode) -> None:
110110
self.hook: rewrite.AssertionRewritingHook | None = None
111111

112112
@property
113-
def rootpath(self):
113+
def invocation_path( self ):
114114
"""Get current root path (current working dir)"""
115115
return str(self.config.invocation_params.dir)
116116

src/_pytest/assertion/rewrite.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ 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) and fn_path.is_relative_to(state.rootpath):
242+
if fnmatch_ex(pat, fn_path) and fn_path.is_relative_to( state.invocation_path ):
243243
state.trace(f"matched test file {fn!r}")
244244
return True
245245

src/_pytest/helpconfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def pytest_addoption(parser: Parser) -> None:
8787
help="Trace considerations of conftest.py files",
8888
)
8989
group.addoption(
90-
"--debug",
90+
"--debug",
9191
action="store",
9292
nargs="?",
9393
const="pytestdebug.log",
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pytest
2+
3+
@pytest.fixture
4+
def special_asserter():
5+
def special_assert(x, y):
6+
assert {'x': x} == {'x': y}
7+
return special_assert

testing/example_scripts/rewrite/src/__init__.py

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def func(x: int, y: int):
2+
assert x>0
3+
assert y>0
4+
return 0 if x == y else 1 if x>y else -1

testing/example_scripts/rewrite/tests/__init__.py

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from _pytest.fixtures import fixture
2+
3+
pytest_plugins = ["pytester", "some_plugin"]
4+
5+
6+
@fixture
7+
def b():
8+
return 1
9+
10+
@fixture
11+
def a():
12+
return 2

0 commit comments

Comments
 (0)