Skip to content

Commit d4561ec

Browse files
committed
cache_dir: use $TOX_ENV_DIR/ prefix if set
Fixes #4270
1 parent 6c06057 commit d4561ec

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

changelog/4270.feature.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The ``cache_dir`` option uses ``$TOX_ENV_DIR`` as prefix (if set in the environment).
2+
3+
This uses a different cache per tox environment by default.

src/_pytest/cacheprovider.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from __future__ import print_function
1010

1111
import json
12+
import os
1213
from collections import OrderedDict
1314

1415
import attr
@@ -275,7 +276,10 @@ def pytest_addoption(parser):
275276
dest="cacheclear",
276277
help="remove all cache contents at start of test run.",
277278
)
278-
parser.addini("cache_dir", default=".pytest_cache", help="cache directory path.")
279+
cache_dir_default = ".pytest_cache"
280+
if "TOX_ENV_DIR" in os.environ:
281+
cache_dir_default = os.path.join(os.environ["TOX_ENV_DIR"], cache_dir_default)
282+
parser.addini("cache_dir", default=cache_dir_default, help="cache directory path.")
279283
group.addoption(
280284
"--lfnf",
281285
"--last-failed-no-failures",

testing/test_cacheprovider.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
pytest_plugins = ("pytester",)
1515

1616

17+
@pytest.fixture(scope="function", autouse=True)
18+
def handle_env(monkeypatch):
19+
"""Ensure env is like most of the tests expect it, i.e. not using tox."""
20+
monkeypatch.delenv("TOX_ENV_DIR", raising=False)
21+
22+
1723
class TestNewAPI(object):
1824
def test_config_cache_makedir(self, testdir):
1925
testdir.makeini("[pytest]")
@@ -148,15 +154,17 @@ def test_custom_cache_dir_with_env_var(self, testdir, monkeypatch):
148154
assert testdir.tmpdir.join("custom_cache_dir").isdir()
149155

150156

151-
def test_cache_reportheader(testdir):
152-
testdir.makepyfile(
153-
"""
154-
def test_hello():
155-
pass
156-
"""
157-
)
157+
@pytest.mark.parametrize("env", ((), ("TOX_ENV_DIR", "/tox_env_dir")))
158+
def test_cache_reportheader(env, testdir, monkeypatch):
159+
testdir.makepyfile("""def test_foo(): pass""")
160+
if env:
161+
monkeypatch.setenv(*env)
162+
expected = os.path.join(env[1], ".pytest_cache")
163+
else:
164+
monkeypatch.delenv("TOX_ENV_DIR", raising=False)
165+
expected = ".pytest_cache"
158166
result = testdir.runpytest("-v")
159-
result.stdout.fnmatch_lines(["cachedir: .pytest_cache"])
167+
result.stdout.fnmatch_lines(["cachedir: %s" % expected])
160168

161169

162170
def test_cache_reportheader_external_abspath(testdir, tmpdir_factory):

0 commit comments

Comments
 (0)