Skip to content

Commit 392a01d

Browse files
committed
13403: Disable assertion rewriting for external modules - add test for plugins
1 parent d5eb2a6 commit 392a01d

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
from _pytest.monkeypatch import MonkeyPatch
2728

@@ -38,6 +39,7 @@
3839
from _pytest.assertion.rewrite import rewrite_asserts
3940
from _pytest.config import Config
4041
from _pytest.config import ExitCode
42+
from _pytest.monkeypatch import MonkeyPatch
4143
from _pytest.pathlib import make_numbered_dir
4244
from _pytest.pytester import Pytester
4345
import pytest
@@ -1298,6 +1300,41 @@ def test_meta_path():
12981300
)
12991301
assert pytester.runpytest().ret == 0
13001302

1303+
1304+
def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
1305+
"""
1306+
Base cases for get rootpath from AssertionState
1307+
"""
1308+
from _pytest.assertion import AssertionState
1309+
config = pytester.parseconfig()
1310+
monkeypatch.chdir(pytester.path)
1311+
state = AssertionState(config, "rewrite")
1312+
assert state.rootpath == str(pytester.path)
1313+
new_rootpath = pytester.path + "/test"
1314+
if not os.path.exists(new_rootpath):
1315+
os.mkdir(new_rootpath)
1316+
monkeypatch.chdir(new_rootpath)
1317+
state = AssertionState(config, "rewrite")
1318+
assert state.rootpath == new_rootpath
1319+
1320+
1321+
@pytest.mark.skipif(
1322+
sys.platform.startswith("win32"), reason="cannot remove cwd on Windows"
1323+
)
1324+
@pytest.mark.skipif(
1325+
sys.platform.startswith("sunos5"), reason="cannot remove cwd on Solaris"
1326+
)
1327+
def test_rootpath_cwd_removed(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> 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+
config = pytester.parseconfig()
1333+
monkeypatch.setattr(target=os, name="getcwd", value=Mock(side_effect=FileNotFoundError))
1334+
state = AssertionState(config, "rewrite")
1335+
assert state.rootpath == os.path.abspath(os.sep)
1336+
1337+
13011338
def test_write_pyc(self, pytester: Pytester, tmp_path) -> None:
13021339
from _pytest.assertion import AssertionState
13031340
from _pytest.assertion.rewrite import _write_pyc

0 commit comments

Comments
 (0)