Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache_dir: use $TOX_ENV_DIR/ prefix if set #4271

Merged
merged 1 commit into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/4270.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The ``cache_dir`` option uses ``$TOX_ENV_DIR`` as prefix (if set in the environment).

This uses a different cache per tox environment by default.
6 changes: 5 additions & 1 deletion src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from __future__ import print_function

import json
import os
from collections import OrderedDict

import attr
Expand Down Expand Up @@ -275,7 +276,10 @@ def pytest_addoption(parser):
dest="cacheclear",
help="remove all cache contents at start of test run.",
)
parser.addini("cache_dir", default=".pytest_cache", help="cache directory path.")
cache_dir_default = ".pytest_cache"
if "TOX_ENV_DIR" in os.environ:
cache_dir_default = os.path.join(os.environ["TOX_ENV_DIR"], cache_dir_default)
parser.addini("cache_dir", default=cache_dir_default, help="cache directory path.")
group.addoption(
"--lfnf",
"--last-failed-no-failures",
Expand Down
29 changes: 21 additions & 8 deletions testing/test_cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
pytest_plugins = ("pytester",)


@pytest.fixture(scope="module", autouse=True)
def handle_env():
"""Ensure env is like most of the tests expect it, i.e. not using tox."""
orig_env = os.environ.pop("TOX_ENV_DIR", None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also easily be written as:

@pytest.fixture(scope="module", autouse=True)
def handle_env(monkeypatch):
    monkeypatch.delenv('TOX_ENV_DIR', raising=False)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, ScopeMismatch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh true, but in that case I would use "function" scope as the cost of patching for each test is negligible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, and used that initially IIRC, but then figured that it is at least something, if only for when stepping through hook callers via pdb.. ;)


yield

if orig_env is not None:
os.environ["TOX_ENV_DIR"] = orig_env


class TestNewAPI(object):
def test_config_cache_makedir(self, testdir):
testdir.makeini("[pytest]")
Expand Down Expand Up @@ -148,15 +159,17 @@ def test_custom_cache_dir_with_env_var(self, testdir, monkeypatch):
assert testdir.tmpdir.join("custom_cache_dir").isdir()


def test_cache_reportheader(testdir):
testdir.makepyfile(
"""
def test_hello():
pass
"""
)
@pytest.mark.parametrize("env", ((), ("TOX_ENV_DIR", "/tox_env_dir")))
def test_cache_reportheader(env, testdir, monkeypatch):
testdir.makepyfile("""def test_foo(): pass""")
if env:
monkeypatch.setenv(*env)
expected = os.path.join(env[1], ".pytest_cache")
else:
monkeypatch.delenv("TOX_ENV_DIR", raising=False)
expected = ".pytest_cache"
result = testdir.runpytest("-v")
result.stdout.fnmatch_lines(["cachedir: .pytest_cache"])
result.stdout.fnmatch_lines(["cachedir: %s" % expected])


def test_cache_reportheader_external_abspath(testdir, tmpdir_factory):
Expand Down