Merge pull request #4271 from blueyed/pytest_cache

cache_dir: use $TOX_ENV_DIR/ prefix if set
This commit is contained in:
Daniel Hahler 2018-11-09 09:09:45 +01:00 committed by GitHub
commit b92530de78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 from __future__ import print_function
import json import json
import os
from collections import OrderedDict from collections import OrderedDict
import attr import attr
@ -275,7 +276,10 @@ def pytest_addoption(parser):
dest="cacheclear", dest="cacheclear",
help="remove all cache contents at start of test run.", 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( group.addoption(
"--lfnf", "--lfnf",
"--last-failed-no-failures", "--last-failed-no-failures",

View File

@ -14,6 +14,17 @@ import pytest
pytest_plugins = ("pytester",) 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): class TestNewAPI(object):
def test_config_cache_makedir(self, testdir): def test_config_cache_makedir(self, testdir):
testdir.makeini("[pytest]") testdir.makeini("[pytest]")
@ -148,15 +159,17 @@ class TestNewAPI(object):
assert testdir.tmpdir.join("custom_cache_dir").isdir() assert testdir.tmpdir.join("custom_cache_dir").isdir()
def test_cache_reportheader(testdir): @pytest.mark.parametrize("env", ((), ("TOX_ENV_DIR", "/tox_env_dir")))
testdir.makepyfile( def test_cache_reportheader(env, testdir, monkeypatch):
""" testdir.makepyfile("""def test_foo(): pass""")
def test_hello(): if env:
pass 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 = 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): def test_cache_reportheader_external_abspath(testdir, tmpdir_factory):