Skip to content

Commit db7dcd4

Browse files
committed
13403: Disable assertion rewriting for external modules - add test for plugins
1 parent ba4263d commit db7dcd4

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/_pytest/assertion/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def rootpath(self):
120120
# Fixes for py's trying to os.getcwd() on py34
121121
# when current working directory doesn't exist (previously triggered via xdist only).
122122
# Ref: https://github.com/pytest-dev/py/pull/207
123-
return os.path.dirname(os.path.abspath(sys.argv[0]))
123+
return os.path.abspath(os.sep)
124124

125125

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

testing/test_assertrewrite.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from typing import cast
2323
from unittest import mock
2424
import zipfile
25+
from mock.mock import Mock
2526

2627
import _pytest._code
2728
from _pytest._io.saferepr import DEFAULT_REPR_MAX_SIZE
@@ -36,6 +37,7 @@
3637
from _pytest.assertion.rewrite import rewrite_asserts
3738
from _pytest.config import Config
3839
from _pytest.config import ExitCode
40+
from _pytest.monkeypatch import MonkeyPatch
3941
from _pytest.pathlib import make_numbered_dir
4042
from _pytest.pytester import Pytester
4143
import pytest
@@ -1280,6 +1282,41 @@ def test_meta_path():
12801282
)
12811283
assert pytester.runpytest().ret == 0
12821284

1285+
1286+
def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
1287+
"""
1288+
Base cases for get rootpath from AssertionState
1289+
"""
1290+
from _pytest.assertion import AssertionState
1291+
config = pytester.parseconfig()
1292+
monkeypatch.chdir(pytester.path)
1293+
state = AssertionState(config, "rewrite")
1294+
assert state.rootpath == str(pytester.path)
1295+
new_rootpath = pytester.path + "/test"
1296+
if not os.path.exists(new_rootpath):
1297+
os.mkdir(new_rootpath)
1298+
monkeypatch.chdir(new_rootpath)
1299+
state = AssertionState(config, "rewrite")
1300+
assert state.rootpath == new_rootpath
1301+
1302+
1303+
@pytest.mark.skipif(
1304+
sys.platform.startswith("win32"), reason="cannot remove cwd on Windows"
1305+
)
1306+
@pytest.mark.skipif(
1307+
sys.platform.startswith("sunos5"), reason="cannot remove cwd on Solaris"
1308+
)
1309+
def test_rootpath_cwd_removed(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
1310+
# Setup conditions for py's trying to os.getcwd() on py34
1311+
# when current working directory doesn't exist (previously triggered via xdist only).
1312+
# Ref: https://github.com/pytest-dev/py/pull/207
1313+
from _pytest.assertion import AssertionState
1314+
config = pytester.parseconfig()
1315+
monkeypatch.setattr(target=os, name="getcwd", value=Mock(side_effect=FileNotFoundError))
1316+
state = AssertionState(config, "rewrite")
1317+
assert state.rootpath == os.path.abspath(os.sep)
1318+
1319+
12831320
def test_write_pyc(self, pytester: Pytester, tmp_path) -> None:
12841321
from _pytest.assertion import AssertionState
12851322
from _pytest.assertion.rewrite import _write_pyc

0 commit comments

Comments
 (0)