Skip to content

Commit

Permalink
Ensure tests pass without dependencies cached from previous tests
Browse files Browse the repository at this point in the history
Adds a --cache-dir arg to the compile cli and uses it for test isolation
via monkeypatching of consumed env var
  • Loading branch information
richafrank committed Dec 27, 2019
1 parent 0da08e5 commit 4408884
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
5 changes: 1 addition & 4 deletions piptools/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion piptools/repositories/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions piptools/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -95,7 +94,7 @@ def __init__(
self,
constraints,
repository,
cache=None,
cache,
prereleases=False,
clear_caches=False,
allow_unsafe=False,
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
)
Expand Down
1 change: 1 addition & 0 deletions piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"--upgrade",
"--upgrade-package",
"--verbose",
"--cache-dir",
}


Expand Down
9 changes: 7 additions & 2 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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)
"""
Expand Down Expand Up @@ -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.
"""
Expand Down

0 comments on commit 4408884

Please sign in to comment.