|
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 | from _pytest.monkeypatch import MonkeyPatch
|
27 | 28 |
|
|
38 | 39 | from _pytest.assertion.rewrite import rewrite_asserts
|
39 | 40 | from _pytest.config import Config
|
40 | 41 | from _pytest.config import ExitCode
|
| 42 | +from _pytest.monkeypatch import MonkeyPatch |
41 | 43 | from _pytest.pathlib import make_numbered_dir
|
42 | 44 | from _pytest.pytester import Pytester
|
43 | 45 | import pytest
|
@@ -1298,6 +1300,41 @@ def test_meta_path():
|
1298 | 1300 | )
|
1299 | 1301 | assert pytester.runpytest().ret == 0
|
1300 | 1302 |
|
| 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 | + |
1301 | 1338 | def test_write_pyc(self, pytester: Pytester, tmp_path) -> None:
|
1302 | 1339 | from _pytest.assertion import AssertionState
|
1303 | 1340 | from _pytest.assertion.rewrite import _write_pyc
|
|
0 commit comments