Skip to content

Commit b3d8825

Browse files
committed
13403: Disable assertion rewriting for external modules
1 parent acf1303 commit b3d8825

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
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 rewriting of external modules

src/_pytest/assertion/rewrite.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ def _early_rewrite_bailout(self, name: str, state: AssertionState) -> bool:
216216
if fnmatch_ex(pat, path):
217217
return False
218218

219+
root_path = self._get_root_path()
220+
if not path.is_relative_to(root_path):
221+
return True
222+
219223
if self._is_marked_for_rewrite(name, state):
220224
return False
221225

@@ -236,13 +240,25 @@ def _should_rewrite(self, name: str, fn: str, state: AssertionState) -> bool:
236240
# modules not passed explicitly on the command line are only
237241
# rewritten if they match the naming convention for test files
238242
fn_path = PurePath(fn)
243+
root_path = self._get_root_path()
244+
if not fn_path.is_relative_to(root_path):
245+
return False
246+
239247
for pat in self.fnpats:
240248
if fnmatch_ex(pat, fn_path):
241249
state.trace(f"matched test file {fn!r}")
242250
return True
243251

244252
return self._is_marked_for_rewrite(name, state)
245253

254+
@staticmethod
255+
def _get_root_path():
256+
try:
257+
root_path = os.getcwd()
258+
return root_path
259+
except FileNotFoundError:
260+
return os.path.dirname(os.path.abspath(sys.argv[0]))
261+
246262
def _is_marked_for_rewrite(self, name: str, state: AssertionState) -> bool:
247263
try:
248264
return self._marked_for_rewrite_cache[name]

testing/test_assertrewrite.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from unittest import mock
2323
import zipfile
2424

25+
from _pytest.monkeypatch import MonkeyPatch
26+
2527
import _pytest._code
2628
from _pytest._io.saferepr import DEFAULT_REPR_MAX_SIZE
2729
from _pytest.assertion import util
@@ -1942,6 +1944,27 @@ def test_simple_failure():
19421944
assert hook.find_spec("file") is not None
19431945
assert self.find_spec_calls == ["file"]
19441946

1947+
1948+
def test_assert_excluded_rootpath(
1949+
self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch
1950+
) -> None:
1951+
"""
1952+
If test files contained outside rootdir, then skip them
1953+
"""
1954+
pytester.makepyfile(
1955+
**{
1956+
"file.py": """\
1957+
def test_simple_failure():
1958+
assert 1 + 1 == 3
1959+
"""
1960+
}
1961+
)
1962+
root_path= "{0}/tests".format(os.getcwd())
1963+
monkeypatch.setattr("os.getcwd", lambda: root_path)
1964+
with mock.patch.object(hook, "fnpats", ["*.py"]):
1965+
assert hook.find_spec("file") is None
1966+
1967+
19451968
@pytest.mark.skipif(
19461969
sys.platform.startswith("win32"), reason="cannot remove cwd on Windows"
19471970
)

0 commit comments

Comments
 (0)