Skip to content

Commit 23e0d70

Browse files
committed
13403: Disable assertion rewriting for external modules - eliminate os.getcwd
1 parent dbc5a59 commit 23e0d70

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

src/_pytest/assertion/__init__.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,14 @@ class AssertionState:
107107
def __init__(self, config: Config, mode) -> None:
108108
self.mode = mode
109109
self.trace = config.trace.root.get("assertion")
110+
self.config=config
110111
self.hook: rewrite.AssertionRewritingHook | None = None
111112

112113
@property
113114
def rootpath(self):
115+
"""Get current root path (current working dir)
114116
"""
115-
Get current root path (current working dir)
116-
"""
117-
try:
118-
return os.getcwd()
119-
except FileNotFoundError:
120-
# Fixes for py's trying to os.getcwd() on py34
121-
# when current working directory doesn't exist (previously triggered via xdist only).
122-
# Ref: https://github.com/pytest-dev/py/pull/207
123-
return os.path.abspath(os.sep)
117+
return str(self.config.invocation_params.dir)
124118

125119

126120
def install_importhook(config: Config) -> rewrite.AssertionRewritingHook:

src/_pytest/pytester.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,11 @@ def chdir(self) -> None:
749749
This is done automatically upon instantiation.
750750
"""
751751
self._monkeypatch.chdir(self.path)
752+
self._monkeypatch.setattr(self._request.config,"invocation_params", Config.InvocationParams(
753+
args= self._request.config.invocation_params.args,
754+
plugins=self._request.config.invocation_params.plugins,
755+
dir=Path(self._path),
756+
))
752757

753758
def _makefile(
754759
self,

testing/test_assertrewrite.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,13 +1308,16 @@ def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> No
13081308
from _pytest.assertion import AssertionState
13091309

13101310
config = pytester.parseconfig()
1311-
monkeypatch.chdir(pytester.path)
13121311
state = AssertionState(config, "rewrite")
1313-
assert state.rootpath == str(pytester.path)
1314-
new_rootpath = str(pytester.path) + "/test"
1312+
assert state.rootpath == str(config.invocation_params.dir)
1313+
new_rootpath =str(pytester.path / "test")
13151314
if not os.path.exists(new_rootpath):
13161315
os.mkdir(new_rootpath)
1317-
monkeypatch.chdir(new_rootpath)
1316+
monkeypatch.setattr(config,"invocation_params", Config.InvocationParams(
1317+
args= (),
1318+
plugins=(),
1319+
dir=Path(new_rootpath),
1320+
))
13181321
state = AssertionState(config, "rewrite")
13191322
assert state.rootpath == new_rootpath
13201323

@@ -1324,20 +1327,6 @@ def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> No
13241327
@pytest.mark.skipif(
13251328
sys.platform.startswith("sunos5"), reason="cannot remove cwd on Solaris"
13261329
)
1327-
def test_rootpath_cwd_removed(
1328-
self, pytester: Pytester, monkeypatch: MonkeyPatch
1329-
) -> None:
1330-
# Setup conditions for py's trying to os.getcwd() on py34
1331-
# when current working directory doesn't exist (previously triggered via xdist only).
1332-
# Ref: https://github.com/pytest-dev/py/pull/207
1333-
from _pytest.assertion import AssertionState
1334-
1335-
config = pytester.parseconfig()
1336-
monkeypatch.setattr(
1337-
target=os, name="getcwd", value=Mock(side_effect=FileNotFoundError)
1338-
)
1339-
state = AssertionState(config, "rewrite")
1340-
assert state.rootpath == os.path.abspath(os.sep)
13411330

13421331
def test_write_pyc(self, pytester: Pytester, tmp_path) -> None:
13431332
from _pytest.assertion import AssertionState
@@ -2036,7 +2025,11 @@ def test_simple_failure():
20362025
rootpath = f"{os.getcwd()}/tests"
20372026
if not os.path.exists(rootpath):
20382027
mkdir(rootpath)
2039-
monkeypatch.chdir(rootpath)
2028+
monkeypatch.setattr(pytester._request.config,"invocation_params", Config.InvocationParams(
2029+
args= (),
2030+
plugins=(),
2031+
dir=Path(rootpath),
2032+
))
20402033
with mock.patch.object(hook, "fnpats", ["*.py"]):
20412034
assert hook.find_spec("file") is None
20422035

@@ -2057,8 +2050,15 @@ def fix(): return 1
20572050
rootpath = f"{os.getcwd()}/tests"
20582051
if not os.path.exists(rootpath):
20592052
mkdir(rootpath)
2060-
monkeypatch.chdir(rootpath)
2061-
2053+
monkeypatch.setattr(
2054+
pytester._request.config,
2055+
"invocation_params",
2056+
Config.InvocationParams(
2057+
args= (),
2058+
plugins=(),
2059+
dir=Path(rootpath),
2060+
)
2061+
)
20622062
with mock.patch.object(hook, "fnpats", ["*.py"]):
20632063
assert hook.find_spec("conftest") is not None
20642064

@@ -2082,7 +2082,15 @@ def test_assert_rewrite_correct_for_plugins(
20822082
rootpath = f"{os.getcwd()}/tests"
20832083
if not os.path.exists(rootpath):
20842084
mkdir(rootpath)
2085-
monkeypatch.chdir(rootpath)
2085+
monkeypatch.setattr(
2086+
pytester._request.config,
2087+
"invocation_params",
2088+
Config.InvocationParams(
2089+
args= (),
2090+
plugins=(),
2091+
dir=Path(rootpath),
2092+
)
2093+
)
20862094
with mock.patch.object(hook, "fnpats", ["*.py"]):
20872095
assert hook.find_spec("plugin") is not None
20882096

0 commit comments

Comments
 (0)