Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providing custom cache directory name through ini option (issue #2543) #2551

Closed
wants to merge 9 commits into from
15 changes: 13 additions & 2 deletions _pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@
import py
import pytest
import json
from os.path import sep as _sep, altsep as _altsep
from os.path import sep as _sep, altsep as _altsep, \
isabs as _isabs, expanduser as _expanduser


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")
if self._cachedir.check():
self._cachedir.remove()
self._cachedir.mkdir()

@staticmethod
def cache_dir_from_config(config):
cache_dir = config.getini("cache_dir")
if _isabs(_expanduser(cache_dir)):
raise ValueError("cache dirname must be relative path, not absolute")
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
Expand Down Expand Up @@ -171,6 +179,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="directory name for cache content.")


def pytest_cmdline_main(config):
Expand Down
2 changes: 2 additions & 0 deletions doc/en/cache.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _`cache_provider`:

Cache: working with cross-testrun state
=======================================

Expand Down
8 changes: 8 additions & 0 deletions doc/en/customize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,11 @@ 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 name where stores content of cache plugin. The directory is
created in rootdir. Default name is ``.cache``. For more information about
cache please refer to :ref:`cache_provider`.
21 changes: 21 additions & 0 deletions testing/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,28 @@ def test_cachefuncarg(cache):
assert result.ret == 0
result.stdout.fnmatch_lines(["*1 passed*"])

def test_custom_cache_dirname(self, testdir):
cache_dir = 'custom_cache_dirname'
testdir.makeini("""
[pytest]
cache_dir = {cache_dir}
""".format(cache_dir=cache_dir))
testdir.makepyfile(test_errored='def test_error():\n assert False')
testdir.runpytest()
assert testdir.tmpdir.join(cache_dir).isdir()

@pytest.mark.parametrize('cache_dir', [
os.path.abspath('tmp'),
pytest.param(os.path.join('~', 'tmp'), marks=pytest.mark.skipif(sys.platform == 'win32',
reason='test for linux pathes')),
])
def test_cache_dirname_fail_on_abs(self, testdir, cache_dir):
testdir.makeini("""
[pytest]
cache_dir = {cache_dir}
""".format(cache_dir=cache_dir))
pytest.raises(ValueError, "testdir.parseconfigure()",
message="cache dirname must be relative path, not absolute")

def test_cache_reportheader(testdir):
testdir.makepyfile("""
Expand Down