diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-07-10 17:36:56 +0200 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-07-11 06:49:50 +0200 |
commit | e2b7b2a7436979808adc5a3da0e46766bba9a900 (patch) | |
tree | 7f7e30d85565f1b5ec360e61a5ca2ac2d7e6f3a3 /tests/testutils | |
parent | 46abb7246baac1effc68cb73852fb2cead959726 (diff) | |
download | pylint-git-e2b7b2a7436979808adc5a3da0e46766bba9a900.tar.gz |
[testutils] Add a parameter to context manager changing sys.path or cwd
Diffstat (limited to 'tests/testutils')
-rw-r--r-- | tests/testutils/test_testutils_utils.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/testutils/test_testutils_utils.py b/tests/testutils/test_testutils_utils.py new file mode 100644 index 000000000..68d1739c2 --- /dev/null +++ b/tests/testutils/test_testutils_utils.py @@ -0,0 +1,50 @@ +# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html +# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt + +import os +import sys +from pathlib import Path + +from pylint.testutils.utils import _test_cwd, _test_sys_path + + +def test__test_sys_path_no_arg() -> None: + sys_path = sys.path + with _test_sys_path(): + assert sys.path == sys_path + new_sys_path = ["new_sys_path"] + sys.path = new_sys_path + assert sys.path == new_sys_path + assert sys.path == sys_path + + +def test__test_sys_path() -> None: + sys_path = sys.path + new_sys_path = [".", "/home"] + with _test_sys_path(new_sys_path): + assert sys.path == new_sys_path + newer_sys_path = ["new_sys_path"] + sys.path = newer_sys_path + assert sys.path == newer_sys_path + assert sys.path == sys_path + + +def test__test_cwd_no_arg(tmp_path: Path) -> None: + cwd = os.getcwd() + with _test_cwd(): + assert os.getcwd() == cwd + os.chdir(tmp_path) + assert os.getcwd() == str(tmp_path) + assert os.getcwd() == cwd + + +def test__test_cwd(tmp_path: Path) -> None: + cwd = os.getcwd() + with _test_cwd(tmp_path): + assert os.getcwd() == str(tmp_path) + new_path = tmp_path / "another_dir" + new_path.mkdir() + os.chdir(new_path) + assert os.getcwd() == str(new_path) + assert os.getcwd() == cwd |