From f471eef6613d7408faa3e57964c043342a4330a9 Mon Sep 17 00:00:00 2001 From: "V.Kuznetsov" Date: Fri, 7 Jul 2017 13:07:06 +0300 Subject: [PATCH 1/4] ini option cache_dir --- _pytest/cacheprovider.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/_pytest/cacheprovider.py b/_pytest/cacheprovider.py index 7fc08fff368..8e64f5b8dc9 100755 --- a/_pytest/cacheprovider.py +++ b/_pytest/cacheprovider.py @@ -8,13 +8,14 @@ import py import pytest import json +import os from os.path import sep as _sep, altsep as _altsep class Cache(object): def __init__(self, config): self.config = config - self._cachedir = config.rootdir.join(".cache") + self._cachedir = Cache.cache_dir_from_config(config) self.trace = config.trace.root.get("cache") if config.getvalue("cacheclear"): self.trace("clearing cachedir") @@ -22,6 +23,16 @@ def __init__(self, config): self._cachedir.remove() self._cachedir.mkdir() + @staticmethod + def cache_dir_from_config(config): + cache_dir = config.getini("cache_dir") + cache_dir = os.path.expanduser(cache_dir) + cache_dir = os.path.expandvars(cache_dir) + if os.path.isabs(cache_dir): + return py.path.local(cache_dir) + else: + return config.rootdir.join(cache_dir) + def makedir(self, name): """ return a directory path object with the given name. If the directory does not yet exist, it will be created. You can use it @@ -171,6 +182,9 @@ def pytest_addoption(parser): group.addoption( '--cache-clear', action='store_true', dest="cacheclear", help="remove all cache contents at start of test run.") + parser.addini( + "cache_dir", default='.cache', + help="cache directory path.") def pytest_cmdline_main(config): From 7a9fc694358f3d6958de0c7785bcbf67c6784076 Mon Sep 17 00:00:00 2001 From: "V.Kuznetsov" Date: Fri, 7 Jul 2017 13:07:33 +0300 Subject: [PATCH 2/4] tests for ini option cache_dir --- testing/test_cache.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/testing/test_cache.py b/testing/test_cache.py index 600b5e6d9fc..c7e92063b1a 100755 --- a/testing/test_cache.py +++ b/testing/test_cache.py @@ -1,6 +1,6 @@ from __future__ import absolute_import, division, print_function import sys - +import py import _pytest import pytest import os @@ -87,7 +87,36 @@ def test_cachefuncarg(cache): assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed*"]) - + def test_custom_rel_cache_dir(self, testdir): + rel_cache_dir = os.path.join('custom_cache_dir', 'subdir') + testdir.makeini(""" + [pytest] + cache_dir = {cache_dir} + """.format(cache_dir=rel_cache_dir)) + testdir.makepyfile(test_errored='def test_error():\n assert False') + testdir.runpytest() + assert testdir.tmpdir.join(rel_cache_dir).isdir() + + def test_custom_abs_cache_dir(self, testdir, tmpdir_factory): + tmp = str(tmpdir_factory.mktemp('tmp')) + abs_cache_dir = os.path.join(tmp, 'custom_cache_dir') + testdir.makeini(""" + [pytest] + cache_dir = {cache_dir} + """.format(cache_dir=abs_cache_dir)) + testdir.makepyfile(test_errored='def test_error():\n assert False') + testdir.runpytest() + assert py.path.local(abs_cache_dir).isdir() + + def test_custom_cache_dir_with_env_var(self, testdir, monkeypatch): + monkeypatch.setenv('env_var', 'custom_cache_dir') + testdir.makeini(""" + [pytest] + cache_dir = {cache_dir} + """.format(cache_dir='$env_var')) + testdir.makepyfile(test_errored='def test_error():\n assert False') + testdir.runpytest() + assert testdir.tmpdir.join('custom_cache_dir').isdir() def test_cache_reportheader(testdir): testdir.makepyfile(""" From 91418eda3b47504c930d8f46a5f96309a7e6909f Mon Sep 17 00:00:00 2001 From: "V.Kuznetsov" Date: Fri, 7 Jul 2017 13:08:12 +0300 Subject: [PATCH 3/4] docs for ini option cache_dir --- doc/en/cache.rst | 2 ++ doc/en/customize.rst | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/doc/en/cache.rst b/doc/en/cache.rst index 688b6dd0468..e4071a8f8ab 100644 --- a/doc/en/cache.rst +++ b/doc/en/cache.rst @@ -1,3 +1,5 @@ +.. _`cache_provider`: + Cache: working with cross-testrun state ======================================= diff --git a/doc/en/customize.rst b/doc/en/customize.rst index ce0a36c11a2..b0c48f0e392 100644 --- a/doc/en/customize.rst +++ b/doc/en/customize.rst @@ -262,3 +262,14 @@ Builtin configuration file options This tells pytest to ignore deprecation warnings and turn all other warnings into errors. For more information please refer to :ref:`warnings`. + +.. confval:: cache_dir + + .. versionadded:: 3.2 + + Sets a directory where stores content of cache plugin. Default directory is + ``.cache`` which is created in :ref:`rootdir `. Directory may be + relative or absolute path. If setting relative path, then directory is created + relative to :ref:`rootdir `. Additionally path may contain environment + variables, that will be expanded. For more information about cache plugin + please refer to :ref:`cache_provider`. From bd52eebab4fb840992fc3f2ca99a40920421b221 Mon Sep 17 00:00:00 2001 From: "V.Kuznetsov" Date: Fri, 7 Jul 2017 13:20:39 +0300 Subject: [PATCH 4/4] changelog for ini option cache_dir --- changelog/2543.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/2543.feature diff --git a/changelog/2543.feature b/changelog/2543.feature new file mode 100644 index 00000000000..6d65a376f6e --- /dev/null +++ b/changelog/2543.feature @@ -0,0 +1 @@ +New ``cache_dir`` ini option: sets a directory where stores content of cache plugin. Default directory is ``.cache`` which is created in ``rootdir``. Directory may be relative or absolute path. If setting relative path, then directory is created relative to ``rootdir``. Additionally path may contain environment variables, that will be expanded.