Skip to content

Commit 0701fcb

Browse files
committed
13403: Disable assertion rewriting for external modules - move root path to AssertState
1 parent bdc096c commit 0701fcb

File tree

3 files changed

+8
-14
lines changed

3 files changed

+8
-14
lines changed

src/_pytest/assertion/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from __future__ import annotations
55

6+
import os
67
from collections.abc import Generator
78
import sys
89
from typing import Any
@@ -111,7 +112,7 @@ def __init__(self, config: Config, mode) -> None:
111112
self.mode = mode
112113
self.trace = config.trace.root.get("assertion")
113114
self.hook: rewrite.AssertionRewritingHook | None = None
114-
115+
self.root_path=os.getcwd()
115116

116117
def install_importhook(config: Config) -> rewrite.AssertionRewritingHook:
117118
"""Try to install the rewrite hook, raise SystemError if it fails."""

src/_pytest/assertion/rewrite.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def find_spec(
133133

134134
return importlib.util.spec_from_file_location(
135135
name,
136-
fn,
136+
fn ,
137137
loader=self,
138138
submodule_search_locations=spec.submodule_search_locations,
139139
)
@@ -218,8 +218,7 @@ 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):
221+
if not path.is_relative_to(state.root_path):
223222
return True
224223

225224
if self._is_marked_for_rewrite(name, state):
@@ -242,8 +241,7 @@ def _should_rewrite(self, name: str, fn: str, state: AssertionState) -> bool:
242241
# modules not passed explicitly on the command line are only
243242
# rewritten if they match the naming convention for test files
244243
fn_path = PurePath(fn)
245-
root_path = self._get_root_path()
246-
if not fn_path.is_relative_to(root_path):
244+
if not fn_path.is_relative_to(state.root_path):
247245
return False
248246

249247
for pat in self.fnpats:
@@ -253,13 +251,6 @@ def _should_rewrite(self, name: str, fn: str, state: AssertionState) -> bool:
253251

254252
return self._is_marked_for_rewrite(name, state)
255253

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]))
263254

264255
def _is_marked_for_rewrite(self, name: str, state: AssertionState) -> bool:
265256
try:

testing/test_assertrewrite.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import inspect
1313
import marshal
1414
import os
15+
from os import mkdir
1516
from pathlib import Path
1617
import py_compile
1718
import re
@@ -1986,7 +1987,8 @@ def test_simple_failure():
19861987
}
19871988
)
19881989
root_path = f"{os.getcwd()}/tests"
1989-
monkeypatch.setattr("os.getcwd", lambda: root_path)
1990+
mkdir(root_path)
1991+
monkeypatch.chdir(root_path)
19901992
with mock.patch.object(hook, "fnpats", ["*.py"]):
19911993
assert hook.find_spec("file") is None
19921994

0 commit comments

Comments
 (0)