summaryrefslogtreecommitdiff
path: root/tests/testutils/test_testutils_utils.py
blob: f32f87ba3d8b143e6fbe5f32b9516ef8b1f5db2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt

import os
import sys
from pathlib import Path

import pytest

from pylint.testutils.utils import _test_cwd, _test_environ_pythonpath, _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


@pytest.mark.parametrize("old_pythonpath", ["./oldpath/:", None])
def test__test_environ_pythonpath_no_arg(old_pythonpath: str) -> None:
    real_pythonpath = os.environ.get("PYTHONPATH")
    with _test_environ_pythonpath(old_pythonpath):
        with _test_environ_pythonpath():
            assert os.environ.get("PYTHONPATH") is None
            new_pythonpath = "./whatever/:"
            os.environ["PYTHONPATH"] = new_pythonpath
            assert os.environ.get("PYTHONPATH") == new_pythonpath
        assert os.environ.get("PYTHONPATH") == old_pythonpath
    assert os.environ.get("PYTHONPATH") == real_pythonpath


@pytest.mark.parametrize("old_pythonpath", ["./oldpath/:", None])
def test__test_environ_pythonpath(old_pythonpath: str) -> None:
    real_pythonpath = os.environ.get("PYTHONPATH")
    with _test_environ_pythonpath(old_pythonpath):
        new_pythonpath = "./whatever/:"
        with _test_environ_pythonpath(new_pythonpath):
            assert os.environ.get("PYTHONPATH") == new_pythonpath
            newer_pythonpath = "./something_else/:"
            os.environ["PYTHONPATH"] = newer_pythonpath
            assert os.environ.get("PYTHONPATH") == newer_pythonpath
        assert os.environ.get("PYTHONPATH") == old_pythonpath
    assert os.environ.get("PYTHONPATH") == real_pythonpath