Skip to content

Commit ad5317b

Browse files
committed
13403: Disable assertion rewriting for external modules - add tests
1 parent ba67fe3 commit ad5317b

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

src/_pytest/assertion/__init__.py

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

112112
@property
113-
def root_path(self):
113+
def rootpath(self):
114+
"""
115+
get current root path (current working dir)
116+
"""
114117
try:
115118
return os.getcwd()
116119
except FileNotFoundError:

src/_pytest/assertion/rewrite.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ def _early_rewrite_bailout(self, name: str, state: AssertionState) -> bool:
218218
if fnmatch_ex(pat, path):
219219
return False
220220

221-
222221
if self._is_marked_for_rewrite(name, state):
223222
return False
224223

@@ -241,7 +240,7 @@ def _should_rewrite(self, name: str, fn: str, state: AssertionState) -> bool:
241240
fn_path = PurePath(fn)
242241

243242
for pat in self.fnpats:
244-
if fnmatch_ex(pat, fn_path) and fn_path.is_relative_to(state.root_path):
243+
if fnmatch_ex(pat, fn_path) and fn_path.is_relative_to(state.rootpath):
245244
state.trace(f"matched test file {fn!r}")
246245
return True
247246

testing/test_assertrewrite.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,11 +1956,11 @@ def test_simple_failure():
19561956
assert self.find_spec_calls == ["file"]
19571957

19581958

1959-
def test_assert_excluded_rootpath(
1959+
def test_assert_rewrites_only_rootpath(
19601960
self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch
19611961
) -> None:
19621962
"""
1963-
If test files contained outside rootdir, then skip them
1963+
If test files contained outside the rootpath, then skip them
19641964
"""
19651965
pytester.makepyfile(
19661966
**{
@@ -1972,22 +1972,58 @@ def test_simple_failure():
19721972
)
19731973
with mock.patch.object(hook, "fnpats", ["*.py"]):
19741974
assert hook.find_spec("file") is not None
1975-
root_path = f"{os.getcwd()}/tests"
19761975

1977-
if not os.path.exists(root_path):
1978-
mkdir(root_path)
1979-
monkeypatch.chdir(root_path)
1976+
rootpath = f"{os.getcwd()}/tests"
1977+
if not os.path.exists(rootpath):
1978+
mkdir(rootpath)
1979+
monkeypatch.chdir(rootpath)
19801980
with mock.patch.object(hook, "fnpats", ["*.py"]):
19811981
assert hook.find_spec("file") is None
19821982

19831983

1984+
def test_assert_correct_for_conftfest(
1985+
self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch
1986+
) -> None:
1987+
"""
1988+
Conftest is always rewritten regardless of the working dir
1989+
"""
1990+
pytester.makeconftest(
1991+
"""
1992+
import pytest
1993+
@pytest.fixture
1994+
def fix(): return 1
1995+
"""
1996+
)
1997+
1998+
rootpath = f"{os.getcwd()}/tests"
1999+
if not os.path.exists(rootpath):
2000+
mkdir(rootpath)
2001+
monkeypatch.chdir(rootpath)
2002+
2003+
with mock.patch.object(hook, "fnpats", ["*.py"]):
2004+
assert hook.find_spec("conftest") is not None
2005+
2006+
19842007
def test_assert_excluded_rewrite_for_plugins(
19852008
self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch
19862009
) -> None:
1987-
plugins= {"ayncio", "fnpats", "pytest_bdd", "django", "mock", "pytest_twisted", "trio"}
2010+
pkgdir = pytester.mkpydir("plugin")
2011+
pkgdir.joinpath("__init__.py").write_text(
2012+
"import pytest\n"
2013+
"@pytest.fixture\n"
2014+
"def special_asserter():\n"
2015+
" def special_assert(x, y):\n"
2016+
" assert x == y\n"
2017+
" return special_assert\n",
2018+
encoding="utf-8",
2019+
)
2020+
pytester.makeconftest('pytest_plugins = ["plugin"]')
2021+
rootpath = f"{os.getcwd()}/tests"
2022+
if not os.path.exists(rootpath):
2023+
mkdir(rootpath)
2024+
monkeypatch.chdir(rootpath)
19882025
with mock.patch.object(hook, "fnpats", ["*.py"]):
1989-
for plugin in plugins:
1990-
assert hook.find_spec(plugin) is None
2026+
assert hook.find_spec("plugin") is not None
19912027

19922028

19932029
@pytest.mark.skipif(

0 commit comments

Comments
 (0)