1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/tests/lib/configuration_helpers.py

64 lines
1.5 KiB
Python
Raw Normal View History

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
import os
import tempfile
2017-05-14 21:20:34 +02:00
import textwrap
2017-05-13 12:13:14 +02:00
import pip.configuration
from pip.utils import ensure_dir
# This is so that tests don't need to import pip.configuration
kinds = pip.configuration.kinds
2017-05-15 18:38:02 +02:00
class ConfigurationMixin(object):
def setup(self):
2017-05-15 18:38:02 +02:00
self.configuration = pip.configuration.Configuration(isolated=False)
self._files_to_clear = []
self._old_environ = os.environ.copy()
def teardown(self):
2017-05-15 18:38:02 +02:00
for file_ in self._files_to_clear:
file_.stop()
os.environ = self._old_environ
def patch_configuration(self, variant, di):
old = self.configuration._load_config_files
@functools.wraps(old)
def overidden():
# Manual Overload
self.configuration._config[variant].update(di)
self.configuration._parsers[variant].append((None, None))
return old()
self.configuration._load_config_files = overidden
@contextlib.contextmanager
2017-05-15 18:38:02 +02:00
def tmpfile(self, contents):
# 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-17 06:15:34 +02:00
os.close(fd)
2017-05-15 18:38:02 +02:00
contents = textwrap.dedent(contents).lstrip()
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
os.remove(path)
@staticmethod
def get_file_contents(path):
with open(path) as f:
return f.read()