Skip to content

Commit 7da1166

Browse files
committed
13403: Disable assertion rewriting for external modules - eliminate os.getcwd
1 parent 96f6846 commit 7da1166

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-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: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,13 +1306,16 @@ def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> No
13061306
from _pytest.assertion import AssertionState
13071307

13081308
config = pytester.parseconfig()
1309-
monkeypatch.chdir(pytester.path)
13101309
state = AssertionState(config, "rewrite")
1311-
assert state.rootpath == str(pytester.path)
1312-
new_rootpath = str(pytester.path) + "/test"
1310+
assert state.rootpath == str(config.invocation_params.dir)
1311+
new_rootpath =str(pytester.path / "test")
13131312
if not os.path.exists(new_rootpath):
13141313
os.mkdir(new_rootpath)
1315-
monkeypatch.chdir(new_rootpath)
1314+
monkeypatch.setattr(config,"invocation_params", Config.InvocationParams(
1315+
args= (),
1316+
plugins=(),
1317+
dir=Path(new_rootpath),
1318+
))
13161319
state = AssertionState(config, "rewrite")
13171320
assert state.rootpath == new_rootpath
13181321

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

13401329
def test_write_pyc(self, pytester: Pytester, tmp_path) -> None:
13411330
from _pytest.assertion import AssertionState
@@ -2034,7 +2023,11 @@ def test_simple_failure():
20342023
rootpath = f"{os.getcwd()}/tests"
20352024
if not os.path.exists(rootpath):
20362025
mkdir(rootpath)
2037-
monkeypatch.chdir(rootpath)
2026+
monkeypatch.setattr(pytester._request.config,"invocation_params", Config.InvocationParams(
2027+
args= (),
2028+
plugins=(),
2029+
dir=Path(rootpath),
2030+
))
20382031
with mock.patch.object(hook, "fnpats", ["*.py"]):
20392032
assert hook.find_spec("file") is None
20402033

@@ -2055,8 +2048,11 @@ def fix(): return 1
20552048
rootpath = f"{os.getcwd()}/tests"
20562049
if not os.path.exists(rootpath):
20572050
mkdir(rootpath)
2058-
monkeypatch.chdir(rootpath)
2059-
2051+
monkeypatch.setattr(pytester._request.config,"invocation_params", Config.InvocationParams(
2052+
args= (),
2053+
plugins=(),
2054+
dir=Path(rootpath),
2055+
))
20602056
with mock.patch.object(hook, "fnpats", ["*.py"]):
20612057
assert hook.find_spec("conftest") is not None
20622058

@@ -2080,7 +2076,11 @@ def test_assert_rewrite_correct_for_plugins(
20802076
rootpath = f"{os.getcwd()}/tests"
20812077
if not os.path.exists(rootpath):
20822078
mkdir(rootpath)
2083-
monkeypatch.chdir(rootpath)
2079+
monkeypatch.setattr(pytester._request.config,"invocation_params", Config.InvocationParams(
2080+
args= (),
2081+
plugins=(),
2082+
dir=Path(rootpath),
2083+
))
20842084
with mock.patch.object(hook, "fnpats", ["*.py"]):
20852085
assert hook.find_spec("plugin") is not None
20862086

0 commit comments

Comments
 (0)