|
22 | 22 | from typing import cast
|
23 | 23 | from unittest import mock
|
24 | 24 | import zipfile
|
| 25 | +from mock.mock import Mock |
25 | 26 |
|
26 | 27 | import _pytest._code
|
27 | 28 | from _pytest._io.saferepr import DEFAULT_REPR_MAX_SIZE
|
|
36 | 37 | from _pytest.assertion.rewrite import rewrite_asserts
|
37 | 38 | from _pytest.config import Config
|
38 | 39 | from _pytest.config import ExitCode
|
| 40 | +from _pytest.monkeypatch import MonkeyPatch |
39 | 41 | from _pytest.pathlib import make_numbered_dir
|
40 | 42 | from _pytest.pytester import Pytester
|
41 | 43 | import pytest
|
@@ -1280,6 +1282,41 @@ def test_meta_path():
|
1280 | 1282 | )
|
1281 | 1283 | assert pytester.runpytest().ret == 0
|
1282 | 1284 |
|
| 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 | + |
1283 | 1320 | def test_write_pyc(self, pytester: Pytester, tmp_path) -> None:
|
1284 | 1321 | from _pytest.assertion import AssertionState
|
1285 | 1322 | from _pytest.assertion.rewrite import _write_pyc
|
|
0 commit comments