Skip to content

Commit 3beb48e

Browse files
committed
13403: Disable assertion rewriting for external modules - add tests
1 parent 8671103 commit 3beb48e

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
@@ -1974,11 +1974,11 @@ def test_simple_failure():
19741974
assert self.find_spec_calls == ["file"]
19751975

19761976

1977-
def test_assert_excluded_rootpath(
1977+
def test_assert_rewrites_only_rootpath(
19781978
self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch
19791979
) -> None:
19801980
"""
1981-
If test files contained outside rootdir, then skip them
1981+
If test files contained outside the rootpath, then skip them
19821982
"""
19831983
pytester.makepyfile(
19841984
**{
@@ -1990,22 +1990,58 @@ def test_simple_failure():
19901990
)
19911991
with mock.patch.object(hook, "fnpats", ["*.py"]):
19921992
assert hook.find_spec("file") is not None
1993-
root_path = f"{os.getcwd()}/tests"
19941993

1995-
if not os.path.exists(root_path):
1996-
mkdir(root_path)
1997-
monkeypatch.chdir(root_path)
1994+
rootpath = f"{os.getcwd()}/tests"
1995+
if not os.path.exists(rootpath):
1996+
mkdir(rootpath)
1997+
monkeypatch.chdir(rootpath)
19981998
with mock.patch.object(hook, "fnpats", ["*.py"]):
19991999
assert hook.find_spec("file") is None
20002000

20012001

2002+
def test_assert_correct_for_conftfest(
2003+
self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch
2004+
) -> None:
2005+
"""
2006+
Conftest is always rewritten regardless of the working dir
2007+
"""
2008+
pytester.makeconftest(
2009+
"""
2010+
import pytest
2011+
@pytest.fixture
2012+
def fix(): return 1
2013+
"""
2014+
)
2015+
2016+
rootpath = f"{os.getcwd()}/tests"
2017+
if not os.path.exists(rootpath):
2018+
mkdir(rootpath)
2019+
monkeypatch.chdir(rootpath)
2020+
2021+
with mock.patch.object(hook, "fnpats", ["*.py"]):
2022+
assert hook.find_spec("conftest") is not None
2023+
2024+
20022025
def test_assert_excluded_rewrite_for_plugins(
20032026
self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch
20042027
) -> None:
2005-
plugins= {"ayncio", "fnpats", "pytest_bdd", "django", "mock", "pytest_twisted", "trio"}
2028+
pkgdir = pytester.mkpydir("plugin")
2029+
pkgdir.joinpath("__init__.py").write_text(
2030+
"import pytest\n"
2031+
"@pytest.fixture\n"
2032+
"def special_asserter():\n"
2033+
" def special_assert(x, y):\n"
2034+
" assert x == y\n"
2035+
" return special_assert\n",
2036+
encoding="utf-8",
2037+
)
2038+
pytester.makeconftest('pytest_plugins = ["plugin"]')
2039+
rootpath = f"{os.getcwd()}/tests"
2040+
if not os.path.exists(rootpath):
2041+
mkdir(rootpath)
2042+
monkeypatch.chdir(rootpath)
20062043
with mock.patch.object(hook, "fnpats", ["*.py"]):
2007-
for plugin in plugins:
2008-
assert hook.find_spec(plugin) is None
2044+
assert hook.find_spec("plugin") is not None
20092045

20102046

20112047
@pytest.mark.skipif(

0 commit comments

Comments
 (0)