Skip to content

Commit 48cfbc3

Browse files
committed
13403: Disable assertion rewriting for external modules - add tests
1 parent 3da821d commit 48cfbc3

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
@@ -114,7 +114,7 @@ def __init__(self, config: Config, mode) -> None:
114114
self.hook: rewrite.AssertionRewritingHook | None = None
115115

116116
@property
117-
def rootpath(self):
117+
def invocation_path( self ):
118118
"""Get current root path (current working dir)"""
119119
return str(self.config.invocation_params.dir)
120120

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
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from typing import Callable
2+
3+
from testing.example_scripts.rewrite.src.main import func
4+
5+
6+
def test_plugin(a: int, b: int, special_asserter: Callable[[int, int], bool]):
7+
special_asserter(a, b)
8+
9+
def test_func(a: int, b: int, special_asserter: Callable[[int, int], bool]):
10+
assert {'res': func(a, b)} == {'res': 0}

testing/example_scripts/rewrite/venv/lib64/python3.11/site-packages/external_lib/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def some_check(a: int):
2+
assert abs(a)<2
3+
return a in set(0, 1, -1)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
home = /usr/bin
2+
include-system-site-packages = false
3+
version = 3.11.0
4+
executable = /usr/bin/python3.11
5+
command = /home/cas12/PycharmProjects/pytest-play/.venv/bin/python3 -m venv /home/cas12/PycharmProjects/pytest-play/testing/example_scripts/rewrite/venv

testing/test_assertrewrite.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,13 +1297,13 @@ def test_meta_path():
12971297
)
12981298
assert pytester.runpytest().ret == 0
12991299

1300-
def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
1301-
"""Base cases for get rootpath from AssertionState"""
1300+
def test_invocation_dir(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
1301+
"""Test get invocation param afrom AssertionState"""
13021302
from _pytest.assertion import AssertionState
13031303

13041304
config = pytester.parseconfig()
13051305
state = AssertionState(config, "rewrite")
1306-
assert state.rootpath == str(config.invocation_params.dir)
1306+
assert state.invocation_path == str( config.invocation_params.dir )
13071307
new_rootpath = str(pytester.path / "test")
13081308
if not os.path.exists(new_rootpath):
13091309
os.mkdir(new_rootpath)
@@ -1317,7 +1317,7 @@ def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> No
13171317
),
13181318
)
13191319
state = AssertionState(config, "rewrite")
1320-
assert state.rootpath == new_rootpath
1320+
assert state.invocation_path == new_rootpath
13211321

13221322
@pytest.mark.skipif(
13231323
sys.platform.startswith("win32"), reason="cannot remove cwd on Windows"
@@ -2062,9 +2062,7 @@ def fix(): return 1
20622062
def test_assert_rewrite_correct_for_plugins(
20632063
self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch
20642064
) -> None:
2065-
"""
2066-
Plugins has always been rewritten regardless of the root dir
2067-
"""
2065+
"""Plugins has always been rewritten regardless of the root dir"""
20682066
pkgdir = pytester.mkpydir("plugin")
20692067
pkgdir.joinpath("__init__.py").write_text(
20702068
"import pytest\n"

0 commit comments

Comments
 (0)