From a507f44465566c1aadbfcc0514dcd2bec8e8847b Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 30 Oct 2018 22:35:09 +0100 Subject: [PATCH] cache_dir: use $TOX_ENV_DIR/ prefix if set Fixes https://github.com/pytest-dev/pytest/issues/4270 --- changelog/4270.feature.rst | 3 +++ src/_pytest/cacheprovider.py | 6 +++++- testing/test_cacheprovider.py | 29 +++++++++++++++++++++-------- 3 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 changelog/4270.feature.rst diff --git a/changelog/4270.feature.rst b/changelog/4270.feature.rst new file mode 100644 index 000000000..2254937a2 --- /dev/null +++ b/changelog/4270.feature.rst @@ -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. diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index a78d857f6..d762d867d 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -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", diff --git a/testing/test_cacheprovider.py b/testing/test_cacheprovider.py index cd888dce1..55090a2f6 100644 --- a/testing/test_cacheprovider.py +++ b/testing/test_cacheprovider.py @@ -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):