From 4408884bfcafe283eddeb70881a303c36b700195 Mon Sep 17 00:00:00 2001 From: Richard Frank Date: Sat, 6 Jul 2019 17:24:49 -0400 Subject: [PATCH] Ensure tests pass without dependencies cached from previous tests Adds a --cache-dir arg to the compile cli and uses it for test isolation via monkeypatching of consumed env var --- piptools/cache.py | 5 +---- piptools/repositories/pypi.py | 2 +- piptools/resolver.py | 5 +---- piptools/scripts/compile.py | 16 ++++++++++++++++ piptools/utils.py | 1 + tests/test_cli_compile.py | 9 +++++++-- 6 files changed, 27 insertions(+), 11 deletions(-) diff --git a/piptools/cache.py b/piptools/cache.py index f31f74250..b1b40d553 100644 --- a/piptools/cache.py +++ b/piptools/cache.py @@ -8,7 +8,6 @@ from pip._vendor.packaging.requirements import Requirement from .exceptions import PipToolsError -from .locations import CACHE_DIR from .utils import as_tuple, key_from_req, lookup_table @@ -49,9 +48,7 @@ class DependencyCache(object): Where X.Y indicates the Python version. """ - def __init__(self, cache_dir=None): - if cache_dir is None: - cache_dir = CACHE_DIR + def __init__(self, cache_dir): if not os.path.isdir(cache_dir): os.makedirs(cache_dir) py_version = ".".join(str(digit) for digit in sys.version_info[:2]) diff --git a/piptools/repositories/pypi.py b/piptools/repositories/pypi.py index acbd6809d..281abecbd 100644 --- a/piptools/repositories/pypi.py +++ b/piptools/repositories/pypi.py @@ -26,9 +26,9 @@ path_to_url, url_to_path, ) -from ..cache import CACHE_DIR from ..click import progressbar from ..exceptions import NoCandidateFound +from ..locations import CACHE_DIR from ..logging import log from ..utils import ( create_install_command, diff --git a/piptools/resolver.py b/piptools/resolver.py index fc53f184a..7473d4bd6 100644 --- a/piptools/resolver.py +++ b/piptools/resolver.py @@ -8,7 +8,6 @@ from . import click from ._compat import install_req_from_line -from .cache import DependencyCache from .logging import log from .utils import ( UNSAFE_PACKAGES, @@ -95,7 +94,7 @@ def __init__( self, constraints, repository, - cache=None, + cache, prereleases=False, clear_caches=False, allow_unsafe=False, @@ -108,8 +107,6 @@ def __init__( self.our_constraints = set(constraints) self.their_constraints = set() self.repository = repository - if cache is None: - cache = DependencyCache() # pragma: no cover self.dependency_cache = cache self.prereleases = prereleases self.clear_caches = clear_caches diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index 177c732b7..5213646a6 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -9,7 +9,9 @@ from .. import click from .._compat import install_req_from_line, parse_requirements +from ..cache import DependencyCache from ..exceptions import PipToolsError +from ..locations import CACHE_DIR from ..logging import log from ..repositories import LocalRequirementsRepository, PyPIRepository from ..resolver import Resolver @@ -172,6 +174,15 @@ default=True, help="Add the find-links option to generated file", ) +@click.option( + "--cache-dir", + help="Specify a directory to cache dependency information (defaults to {})".format( + CACHE_DIR + ), + envvar="PIP_TOOLS_CACHE_DIR", + show_envvar=True, + type=click.Path(file_okay=False, writable=True), +) def cli( ctx, verbose, @@ -198,6 +209,7 @@ def cli( max_rounds, build_isolation, emit_find_links, + cache_dir, ): """Compiles requirements.txt from requirements.in specs.""" log.verbosity = verbose - quiet @@ -348,11 +360,15 @@ def cli( for find_link in dedup(repository.finder.find_links): log.debug(" -f {}".format(find_link)) + if cache_dir is None: + cache_dir = CACHE_DIR + try: resolver = Resolver( constraints, repository, prereleases=repository.finder.allow_all_prereleases or pre, + cache=DependencyCache(cache_dir), clear_caches=rebuild, allow_unsafe=allow_unsafe, ) diff --git a/piptools/utils.py b/piptools/utils.py index 8727f1e2a..1d807d90e 100644 --- a/piptools/utils.py +++ b/piptools/utils.py @@ -20,6 +20,7 @@ "--upgrade", "--upgrade-package", "--verbose", + "--cache-dir", } diff --git a/tests/test_cli_compile.py b/tests/test_cli_compile.py index e84703e76..a81f681b6 100644 --- a/tests/test_cli_compile.py +++ b/tests/test_cli_compile.py @@ -15,6 +15,11 @@ from piptools.scripts.compile import cli +@pytest.fixture(autouse=True) +def temp_dep_cache(tmpdir, monkeypatch): + monkeypatch.setenv("PIP_TOOLS_CACHE_DIR", str(tmpdir / "cache")) + + def test_default_pip_conf_read(pip_with_index_conf, runner): # preconditions with open("requirements.in", "w"): @@ -519,7 +524,7 @@ def test_generate_hashes_verbose(pip_conf, runner): @pytest.mark.skipif(PIP_VERSION < (9,), reason="needs pip 9 or greater") -def test_filter_pip_markers(runner): +def test_filter_pip_markers(pip_conf, runner): """ Check that pip-compile works with pip environment markers (PEP496) """ @@ -592,7 +597,7 @@ def test_not_specified_input_file(runner): assert out.exit_code == 2 -def test_stdin(runner): +def test_stdin(pip_conf, runner): """ Test compile requirements from STDIN. """