2017-05-13 12:13:14 +02:00
|
|
|
"""Helpers for tests that check configuration
|
|
|
|
"""
|
|
|
|
|
2017-05-14 21:20:34 +02:00
|
|
|
import contextlib
|
|
|
|
import functools
|
2017-05-13 12:16:32 +02:00
|
|
|
import os
|
|
|
|
import tempfile
|
2017-05-14 21:20:34 +02:00
|
|
|
import textwrap
|
2017-05-13 12:16:32 +02:00
|
|
|
|
2017-08-31 17:48:18 +02:00
|
|
|
import pip._internal.configuration
|
|
|
|
from pip._internal.utils.misc import ensure_dir
|
2017-05-13 12:16:32 +02:00
|
|
|
|
2019-02-02 20:43:49 +01:00
|
|
|
# This is so that tests don't need to import pip._internal.configuration.
|
2017-08-31 17:48:18 +02:00
|
|
|
kinds = pip._internal.configuration.kinds
|
2017-05-14 08:57:45 +02:00
|
|
|
|
2017-05-13 12:16:32 +02:00
|
|
|
|
2020-12-24 22:23:07 +01:00
|
|
|
class ConfigurationMixin:
|
2017-05-13 12:16:32 +02:00
|
|
|
|
|
|
|
def setup(self):
|
2017-08-31 17:48:18 +02:00
|
|
|
self.configuration = pip._internal.configuration.Configuration(
|
|
|
|
isolated=False,
|
|
|
|
)
|
2017-05-15 18:38:02 +02:00
|
|
|
self._files_to_clear = []
|
|
|
|
|
2017-05-13 12:16:32 +02:00
|
|
|
def teardown(self):
|
2017-06-01 08:08:20 +02:00
|
|
|
for fname in self._files_to_clear:
|
|
|
|
fname.stop()
|
2017-05-15 18:38:02 +02:00
|
|
|
|
|
|
|
def patch_configuration(self, variant, di):
|
|
|
|
old = self.configuration._load_config_files
|
|
|
|
|
|
|
|
@functools.wraps(old)
|
2018-10-09 16:58:50 +02:00
|
|
|
def overridden():
|
2017-05-15 18:38:02 +02:00
|
|
|
# Manual Overload
|
|
|
|
self.configuration._config[variant].update(di)
|
|
|
|
self.configuration._parsers[variant].append((None, None))
|
|
|
|
return old()
|
|
|
|
|
2018-10-09 16:58:50 +02:00
|
|
|
self.configuration._load_config_files = overridden
|
2017-05-13 12:16:32 +02:00
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2017-05-15 18:38:02 +02:00
|
|
|
def tmpfile(self, contents):
|
2017-05-13 12:16:32 +02:00
|
|
|
# Create a temporary file
|
2017-05-17 06:15:34 +02:00
|
|
|
fd, path = tempfile.mkstemp(
|
2017-05-15 18:38:02 +02:00
|
|
|
prefix="pip_", suffix="_config.ini", text=True
|
2017-05-13 12:16:32 +02:00
|
|
|
)
|
2017-05-17 06:15:34 +02:00
|
|
|
os.close(fd)
|
2017-05-13 12:16:32 +02:00
|
|
|
|
2017-05-15 18:38:02 +02:00
|
|
|
contents = textwrap.dedent(contents).lstrip()
|
2017-05-13 12:16:32 +02:00
|
|
|
ensure_dir(os.path.dirname(path))
|
|
|
|
with open(path, "w") as f:
|
|
|
|
f.write(contents)
|
|
|
|
|
2017-05-15 18:38:02 +02:00
|
|
|
yield path
|
2017-05-13 12:16:32 +02:00
|
|
|
|
|
|
|
os.remove(path)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_file_contents(path):
|
|
|
|
with open(path) as f:
|
|
|
|
return f.read()
|