test_configuration.py 7.0 KB
Newer Older
P
Pradyun S. Gedam 已提交
1 2 3 4
"""Tests for all things related to the configuration
"""

import os
P
Pradyun S. Gedam 已提交
5

6
import pytest
P
Pradyun S. Gedam 已提交
7 8
from mock import MagicMock

9 10
from pip._internal.exceptions import ConfigurationError
from pip._internal.locations import (
P
Pradyun Gedam 已提交
11
    new_config_file, site_config_files, venv_config_file,
12
)
P
Pradyun S. Gedam 已提交
13
from tests.lib.configuration_helpers import ConfigurationMixin, kinds
P
Pradyun S. Gedam 已提交
14 15


P
Pradyun S. Gedam 已提交
16
class TestConfigurationLoading(ConfigurationMixin):
P
Pradyun S. Gedam 已提交
17 18

    def test_global_loading(self):
P
Pradyun S. Gedam 已提交
19
        self.patch_configuration(kinds.GLOBAL, {"test.hello": "1"})
P
Pradyun S. Gedam 已提交
20

P
Pradyun S. Gedam 已提交
21
        self.configuration.load()
P
Pradyun S. Gedam 已提交
22
        assert self.configuration.get_value("test.hello") == "1"
P
Pradyun S. Gedam 已提交
23 24

    def test_user_loading(self):
P
Pradyun S. Gedam 已提交
25
        self.patch_configuration(kinds.USER, {"test.hello": "2"})
P
Pradyun S. Gedam 已提交
26

P
Pradyun S. Gedam 已提交
27
        self.configuration.load()
P
Pradyun S. Gedam 已提交
28
        assert self.configuration.get_value("test.hello") == "2"
P
Pradyun S. Gedam 已提交
29 30

    def test_venv_loading(self):
P
Pradyun S. Gedam 已提交
31
        self.patch_configuration(kinds.VENV, {"test.hello": "3"})
P
Pradyun S. Gedam 已提交
32

P
Pradyun S. Gedam 已提交
33
        self.configuration.load()
P
Pradyun S. Gedam 已提交
34
        assert self.configuration.get_value("test.hello") == "3"
P
Pradyun S. Gedam 已提交
35

36
    def test_environment_config_loading(self):
P
Pradyun S. Gedam 已提交
37
        contents = """
38 39
            [test]
            hello = 4
P
Pradyun S. Gedam 已提交
40 41 42 43 44 45 46 47
        """

        with self.tmpfile(contents) as config_file:
            os.environ["PIP_CONFIG_FILE"] = config_file

            self.configuration.load()
            assert self.configuration.get_value("test.hello") == "4", \
                self.configuration._config
48

P
Pradyun S. Gedam 已提交
49 50
    def test_environment_var_loading(self):
        os.environ["PIP_HELLO"] = "5"
51 52

        self.configuration.load()
P
Pradyun S. Gedam 已提交
53
        assert self.configuration.get_value(":env:.hello") == "5"
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    @pytest.mark.skipif("sys.platform == 'win32'")
    def test_environment_var_does_not_load_lowercase(self):
        os.environ["pip_hello"] = "5"

        self.configuration.load()
        with pytest.raises(ConfigurationError):
            self.configuration.get_value(":env:.hello")

    def test_environment_var_does_not_load_version(self):
        os.environ["PIP_VERSION"] = "True"

        self.configuration.load()

        with pytest.raises(ConfigurationError):
            self.configuration.get_value(":env:.version")

P
Pradyun S. Gedam 已提交
71

P
Pradyun S. Gedam 已提交
72
class TestConfigurationPrecedence(ConfigurationMixin):
P
Pradyun S. Gedam 已提交
73 74 75
    # Tests for methods to that determine the order of precedence of
    # configuration options

P
Pradyun S. Gedam 已提交
76 77 78 79 80 81 82 83
    def test_env_overides_venv(self):
        self.patch_configuration(kinds.VENV, {"test.hello": "1"})
        self.patch_configuration(kinds.ENV, {"test.hello": "0"})
        self.configuration.load()

        assert self.configuration.get_value("test.hello") == "0"

    def test_env_overides_user(self):
P
Pradyun S. Gedam 已提交
84
        self.patch_configuration(kinds.USER, {"test.hello": "2"})
P
Pradyun S. Gedam 已提交
85
        self.patch_configuration(kinds.ENV, {"test.hello": "0"})
P
Pradyun S. Gedam 已提交
86 87
        self.configuration.load()

P
Pradyun S. Gedam 已提交
88
        assert self.configuration.get_value("test.hello") == "0"
P
Pradyun S. Gedam 已提交
89

P
Pradyun S. Gedam 已提交
90 91 92
    def test_env_overides_global(self):
        self.patch_configuration(kinds.GLOBAL, {"test.hello": "3"})
        self.patch_configuration(kinds.ENV, {"test.hello": "0"})
P
Pradyun S. Gedam 已提交
93 94
        self.configuration.load()

P
Pradyun S. Gedam 已提交
95
        assert self.configuration.get_value("test.hello") == "0"
P
Pradyun S. Gedam 已提交
96

P
Pradyun S. Gedam 已提交
97
    def test_venv_overides_user(self):
P
Pradyun S. Gedam 已提交
98
        self.patch_configuration(kinds.USER, {"test.hello": "2"})
P
Pradyun S. Gedam 已提交
99
        self.patch_configuration(kinds.VENV, {"test.hello": "1"})
P
Pradyun S. Gedam 已提交
100 101
        self.configuration.load()

P
Pradyun S. Gedam 已提交
102
        assert self.configuration.get_value("test.hello") == "1"
P
Pradyun S. Gedam 已提交
103

P
Pradyun S. Gedam 已提交
104 105 106
    def test_venv_overides_global(self):
        self.patch_configuration(kinds.GLOBAL, {"test.hello": "3"})
        self.patch_configuration(kinds.VENV, {"test.hello": "1"})
P
Pradyun S. Gedam 已提交
107 108
        self.configuration.load()

P
Pradyun S. Gedam 已提交
109
        assert self.configuration.get_value("test.hello") == "1"
P
Pradyun S. Gedam 已提交
110

P
Pradyun S. Gedam 已提交
111 112
    def test_user_overides_global(self):
        self.patch_configuration(kinds.GLOBAL, {"test.hello": "3"})
P
Pradyun S. Gedam 已提交
113
        self.patch_configuration(kinds.USER, {"test.hello": "2"})
P
Pradyun S. Gedam 已提交
114 115
        self.configuration.load()

P
Pradyun S. Gedam 已提交
116
        assert self.configuration.get_value("test.hello") == "2"
P
Pradyun S. Gedam 已提交
117 118 119 120 121 122 123 124 125

    def test_env_not_overriden_by_environment_var(self):
        self.patch_configuration(kinds.ENV, {"test.hello": "1"})
        os.environ["PIP_HELLO"] = "5"

        self.configuration.load()

        assert self.configuration.get_value("test.hello") == "1"
        assert self.configuration.get_value(":env:.hello") == "5"
P
Pradyun S. Gedam 已提交
126

P
Pradyun S. Gedam 已提交
127
    def test_venv_not_overriden_by_environment_var(self):
P
Pradyun S. Gedam 已提交
128 129 130 131 132 133 134 135 136 137 138 139
        self.patch_configuration(kinds.VENV, {"test.hello": "2"})
        os.environ["PIP_HELLO"] = "5"

        self.configuration.load()

        assert self.configuration.get_value("test.hello") == "2"
        assert self.configuration.get_value(":env:.hello") == "5"

    def test_user_not_overriden_by_environment_var(self):
        self.patch_configuration(kinds.USER, {"test.hello": "3"})
        os.environ["PIP_HELLO"] = "5"

P
Pradyun S. Gedam 已提交
140 141
        self.configuration.load()

P
Pradyun S. Gedam 已提交
142
        assert self.configuration.get_value("test.hello") == "3"
P
Pradyun S. Gedam 已提交
143 144 145 146 147 148 149 150 151 152
        assert self.configuration.get_value(":env:.hello") == "5"

    def test_global_not_overriden_by_environment_var(self):
        self.patch_configuration(kinds.GLOBAL, {"test.hello": "4"})
        os.environ["PIP_HELLO"] = "5"

        self.configuration.load()

        assert self.configuration.get_value("test.hello") == "4"
        assert self.configuration.get_value(":env:.hello") == "5"
P
Pradyun S. Gedam 已提交
153 154


P
Pradyun S. Gedam 已提交
155
class TestConfigurationModification(ConfigurationMixin):
P
Pradyun S. Gedam 已提交
156 157 158 159 160 161
    # Tests for methods to that modify the state of a Configuration

    def test_no_specific_given_modification(self):
        self.configuration.load()

        try:
P
Pradyun S. Gedam 已提交
162
            self.configuration.set_value("test.hello", "10")
P
Pradyun S. Gedam 已提交
163 164 165 166 167 168
        except ConfigurationError:
            pass
        else:
            assert False, "Should have raised an error."

    def test_venv_modification(self):
169
        self.configuration.load_only = kinds.VENV
P
Pradyun S. Gedam 已提交
170 171 172 173 174 175
        self.configuration.load()

        # Mock out the method
        mymock = MagicMock(spec=self.configuration._mark_as_modified)
        self.configuration._mark_as_modified = mymock

P
Pradyun S. Gedam 已提交
176
        self.configuration.set_value("test.hello", "10")
P
Pradyun S. Gedam 已提交
177 178 179 180 181 182 183

        # get the path to venv config file
        assert mymock.call_count == 1
        assert mymock.call_args[0][0] == venv_config_file

    def test_user_modification(self):
        # get the path to local config file
184
        self.configuration.load_only = kinds.USER
P
Pradyun S. Gedam 已提交
185 186 187 188 189 190
        self.configuration.load()

        # Mock out the method
        mymock = MagicMock(spec=self.configuration._mark_as_modified)
        self.configuration._mark_as_modified = mymock

P
Pradyun S. Gedam 已提交
191
        self.configuration.set_value("test.hello", "10")
P
Pradyun S. Gedam 已提交
192 193 194 195 196 197 198

        # get the path to user config file
        assert mymock.call_count == 1
        assert mymock.call_args[0][0] == new_config_file

    def test_global_modification(self):
        # get the path to local config file
199
        self.configuration.load_only = kinds.GLOBAL
P
Pradyun S. Gedam 已提交
200 201 202 203 204 205
        self.configuration.load()

        # Mock out the method
        mymock = MagicMock(spec=self.configuration._mark_as_modified)
        self.configuration._mark_as_modified = mymock

P
Pradyun S. Gedam 已提交
206
        self.configuration.set_value("test.hello", "10")
P
Pradyun S. Gedam 已提交
207 208 209 210

        # get the path to user config file
        assert mymock.call_count == 1
        assert mymock.call_args[0][0] == site_config_files[-1]