Skip to content

Commit 0cd03d6

Browse files
committed
13403: Disable assertion rewriting for external modules - move root path to AssertState
1 parent 3b34bfe commit 0cd03d6

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
@@ -107,7 +108,7 @@ def __init__(self, config: Config, mode) -> None:
107108
self.mode = mode
108109
self.trace = config.trace.root.get("assertion")
109110
self.hook: rewrite.AssertionRewritingHook | None = None
110-
111+
self.root_path=os.getcwd()
111112

112113
def install_importhook(config: Config) -> rewrite.AssertionRewritingHook:
113114
"""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
@@ -1970,7 +1971,8 @@ def test_simple_failure():
19701971
}
19711972
)
19721973
root_path = f"{os.getcwd()}/tests"
1973-
monkeypatch.setattr("os.getcwd", lambda: root_path)
1974+
mkdir(root_path)
1975+
monkeypatch.chdir(root_path)
19741976
with mock.patch.object(hook, "fnpats", ["*.py"]):
19751977
assert hook.find_spec("file") is None
19761978

0 commit comments

Comments
 (0)