2017-05-12 12:42:35 +02:00
|
|
|
"""Tests for all things related to the configuration
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
from mock import MagicMock
|
|
|
|
|
|
|
|
from pip.locations import venv_config_file, new_config_file, site_config_files
|
|
|
|
from pip.exceptions import ConfigurationError
|
|
|
|
|
2017-05-14 08:57:45 +02:00
|
|
|
from tests.lib.configuration_helpers import kinds, ConfigurationPatchingMixin
|
2017-05-12 12:42:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestConfigurationLoading(ConfigurationPatchingMixin):
|
|
|
|
|
|
|
|
def test_global_loading(self):
|
2017-05-14 08:57:45 +02:00
|
|
|
self.patch_configuration(kinds.GLOBAL, {"test.hello": 1})
|
2017-05-12 12:42:35 +02:00
|
|
|
self.configuration.load()
|
|
|
|
assert self.configuration.get_value("test.hello") == 1
|
|
|
|
|
|
|
|
def test_user_loading(self):
|
2017-05-14 08:57:45 +02:00
|
|
|
self.patch_configuration(kinds.USER, {"test.hello": 2})
|
2017-05-12 12:42:35 +02:00
|
|
|
self.configuration.load()
|
|
|
|
assert self.configuration.get_value("test.hello") == 2
|
|
|
|
|
|
|
|
def test_venv_loading(self):
|
2017-05-14 08:57:45 +02:00
|
|
|
self.patch_configuration(kinds.VENV, {"test.hello": 3})
|
2017-05-12 12:42:35 +02:00
|
|
|
self.configuration.load()
|
|
|
|
assert self.configuration.get_value("test.hello") == 3
|
|
|
|
|
|
|
|
|
|
|
|
class TestConfigurationPrecedence(ConfigurationPatchingMixin):
|
|
|
|
# Tests for methods to that determine the order of precedence of
|
|
|
|
# configuration options
|
|
|
|
|
|
|
|
def test_global_overriden_by_user(self):
|
2017-05-14 08:57:45 +02:00
|
|
|
self.patch_configuration(kinds.GLOBAL, {"test.hello": 1})
|
|
|
|
self.patch_configuration(kinds.USER, {"test.hello": 2})
|
2017-05-12 12:42:35 +02:00
|
|
|
self.configuration.load()
|
|
|
|
|
|
|
|
assert self.configuration.get_value("test.hello") == 2
|
|
|
|
|
|
|
|
def test_global_overriden_by_venv(self):
|
2017-05-14 08:57:45 +02:00
|
|
|
self.patch_configuration(kinds.GLOBAL, {"test.hello": 1})
|
|
|
|
self.patch_configuration(kinds.VENV, {"test.hello": 3})
|
2017-05-12 12:42:35 +02:00
|
|
|
self.configuration.load()
|
|
|
|
|
|
|
|
assert self.configuration.get_value("test.hello") == 3
|
|
|
|
|
|
|
|
def test_user_overriden_by_venv(self):
|
2017-05-14 08:57:45 +02:00
|
|
|
self.patch_configuration(kinds.USER, {"test.hello": 2})
|
|
|
|
self.patch_configuration(kinds.VENV, {"test.hello": 3})
|
2017-05-12 12:42:35 +02:00
|
|
|
self.configuration.load()
|
|
|
|
|
|
|
|
assert self.configuration.get_value("test.hello") == 3
|
|
|
|
|
|
|
|
def test_global_not_overriden_by_environment(self):
|
2017-05-14 08:57:45 +02:00
|
|
|
self.patch_configuration(kinds.GLOBAL, {"test.hello": 1})
|
2017-05-12 12:42:35 +02:00
|
|
|
os.environ["PIP_HELLO"] = "4"
|
|
|
|
self.configuration.load()
|
|
|
|
|
|
|
|
assert self.configuration.get_value("test.hello") == 1
|
|
|
|
assert self.configuration.get_value(":env:.hello") == "4"
|
|
|
|
|
|
|
|
def test_user_not_overriden_by_environment(self):
|
2017-05-14 08:57:45 +02:00
|
|
|
self.patch_configuration(kinds.USER, {"test.hello": 2})
|
2017-05-12 12:42:35 +02:00
|
|
|
os.environ["PIP_HELLO"] = "4"
|
|
|
|
self.configuration.load()
|
|
|
|
|
|
|
|
assert self.configuration.get_value("test.hello") == 2
|
|
|
|
assert self.configuration.get_value(":env:.hello") == "4"
|
|
|
|
|
|
|
|
def test_venv_not_overriden_by_environment(self):
|
2017-05-14 08:57:45 +02:00
|
|
|
self.patch_configuration(kinds.VENV, {"test.hello": 3})
|
2017-05-12 12:42:35 +02:00
|
|
|
os.environ["PIP_HELLO"] = "4"
|
|
|
|
self.configuration.load()
|
|
|
|
|
|
|
|
assert self.configuration.get_value("test.hello") == 3
|
|
|
|
assert self.configuration.get_value(":env:.hello") == "4"
|
|
|
|
|
|
|
|
|
|
|
|
class TestConfigurationModification(ConfigurationPatchingMixin):
|
|
|
|
# Tests for methods to that modify the state of a Configuration
|
|
|
|
|
|
|
|
def test_no_specific_given_modification(self):
|
|
|
|
self.configuration.load()
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.configuration.set_value("test.hello", 10)
|
|
|
|
except ConfigurationError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
assert False, "Should have raised an error."
|
|
|
|
|
|
|
|
def test_venv_modification(self):
|
2017-05-14 08:57:45 +02:00
|
|
|
self.configuration.load_only = kinds.VENV
|
2017-05-12 12:42:35 +02:00
|
|
|
self.configuration.load()
|
|
|
|
|
|
|
|
# Mock out the method
|
|
|
|
mymock = MagicMock(spec=self.configuration._mark_as_modified)
|
|
|
|
self.configuration._mark_as_modified = mymock
|
|
|
|
|
|
|
|
self.configuration.set_value("test.hello", 10)
|
|
|
|
|
|
|
|
# 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
|
2017-05-14 08:57:45 +02:00
|
|
|
self.configuration.load_only = kinds.USER
|
2017-05-12 12:42:35 +02:00
|
|
|
self.configuration.load()
|
|
|
|
|
|
|
|
# Mock out the method
|
|
|
|
mymock = MagicMock(spec=self.configuration._mark_as_modified)
|
|
|
|
self.configuration._mark_as_modified = mymock
|
|
|
|
|
|
|
|
self.configuration.set_value("test.hello", 10)
|
|
|
|
|
|
|
|
# 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
|
2017-05-14 08:57:45 +02:00
|
|
|
self.configuration.load_only = kinds.GLOBAL
|
2017-05-12 12:42:35 +02:00
|
|
|
self.configuration.load()
|
|
|
|
|
|
|
|
# Mock out the method
|
|
|
|
mymock = MagicMock(spec=self.configuration._mark_as_modified)
|
|
|
|
self.configuration._mark_as_modified = mymock
|
|
|
|
|
|
|
|
self.configuration.set_value("test.hello", 10)
|
|
|
|
|
|
|
|
# get the path to user config file
|
|
|
|
assert mymock.call_count == 1
|
|
|
|
assert mymock.call_args[0][0] == site_config_files[-1]
|