cache_dir: use $TOX_ENV_DIR/ prefix if set

Fixes https://github.com/pytest-dev/pytest/issues/4270
This commit is contained in:
Daniel Hahler 2018-10-30 22:35:09 +01:00
parent 6c06057242
commit a507f44465
3 changed files with 29 additions and 9 deletions

View File

@ -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.

View File

@ -9,6 +9,7 @@ from __future__ import division
from __future__ import print_function
import json
import os
from collections import OrderedDict
import attr
@ -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",

View File

@ -14,6 +14,17 @@ import pytest
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)
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]")
@ -148,15 +159,17 @@ class TestNewAPI(object):
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):