Skip to content

Commit 529286f

Browse files
committed
cache_dir: use $TOX_ENV_DIR/ prefix if set
Fixes #4270
1 parent 233c2a2 commit 529286f

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

changelog/4270.feature.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:confval:`cache_dir` 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

+10-8
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,17 @@ def test_custom_cache_dir_with_env_var(self, testdir, monkeypatch):
148148
assert testdir.tmpdir.join("custom_cache_dir").isdir()
149149

150150

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

161163

162164
def test_cache_reportheader_external_abspath(testdir, tmpdir_factory):

0 commit comments

Comments
 (0)