Skip to content

Commit b92530d

Browse files
authored
Merge pull request pytest-dev#4271 from blueyed/pytest_cache
cache_dir: use $TOX_ENV_DIR/ prefix if set
2 parents 6c06057 + a507f44 commit b92530d

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-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

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

1616

17+
@pytest.fixture(scope="module", autouse=True)
18+
def handle_env():
19+
"""Ensure env is like most of the tests expect it, i.e. not using tox."""
20+
orig_env = os.environ.pop("TOX_ENV_DIR", None)
21+
22+
yield
23+
24+
if orig_env is not None:
25+
os.environ["TOX_ENV_DIR"] = orig_env
26+
27+
1728
class TestNewAPI(object):
1829
def test_config_cache_makedir(self, testdir):
1930
testdir.makeini("[pytest]")
@@ -148,15 +159,17 @@ def test_custom_cache_dir_with_env_var(self, testdir, monkeypatch):
148159
assert testdir.tmpdir.join("custom_cache_dir").isdir()
149160

150161

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

161174

162175
def test_cache_reportheader_external_abspath(testdir, tmpdir_factory):

0 commit comments

Comments
 (0)