Skip to content

Commit c6f2074

Browse files
committed
13403: Disable assertion rewriting for external modules
1 parent 919ae9d commit c6f2074

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
@@ -218,6 +218,10 @@ def _early_rewrite_bailout(self, name: str, state: AssertionState) -> bool:
218218
if fnmatch_ex(pat, path):
219219
return False
220220

221+
root_path = self._get_root_path()
222+
if not path.is_relative_to(root_path):
223+
return True
224+
221225
if self._is_marked_for_rewrite(name, state):
222226
return False
223227

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

246254
return self._is_marked_for_rewrite(name, state)
247255

256+
@staticmethod
257+
def _get_root_path():
258+
try:
259+
root_path = os.getcwd()
260+
return root_path
261+
except FileNotFoundError:
262+
return os.path.dirname(os.path.abspath(sys.argv[0]))
263+
248264
def _is_marked_for_rewrite(self, name: str, state: AssertionState) -> bool:
249265
try:
250266
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
@@ -1955,6 +1957,27 @@ def test_simple_failure():
19551957
assert hook.find_spec("file") is not None
19561958
assert self.find_spec_calls == ["file"]
19571959

1960+
1961+
def test_assert_excluded_rootpath(
1962+
self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch
1963+
) -> None:
1964+
"""
1965+
If test files contained outside rootdir, then skip them
1966+
"""
1967+
pytester.makepyfile(
1968+
**{
1969+
"file.py": """\
1970+
def test_simple_failure():
1971+
assert 1 + 1 == 3
1972+
"""
1973+
}
1974+
)
1975+
root_path= "{0}/tests".format(os.getcwd())
1976+
monkeypatch.setattr("os.getcwd", lambda: root_path)
1977+
with mock.patch.object(hook, "fnpats", ["*.py"]):
1978+
assert hook.find_spec("file") is None
1979+
1980+
19581981
@pytest.mark.skipif(
19591982
sys.platform.startswith("win32"), reason="cannot remove cwd on Windows"
19601983
)

0 commit comments

Comments
 (0)