Merge pull request #2559 from RockBomber/features
cache_dir ini option for setting cache directory
This commit is contained in:
commit
a87f6f84cc
|
@ -8,13 +8,14 @@ from __future__ import absolute_import, division, print_function
|
|||
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 @@ class Cache(object):
|
|||
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):
|
||||
|
|
|
@ -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.
|
|
@ -1,3 +1,5 @@
|
|||
.. _`cache_provider`:
|
||||
|
||||
Cache: working with cross-testrun state
|
||||
=======================================
|
||||
|
||||
|
|
|
@ -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 <rootdir>`. Directory may be
|
||||
relative or absolute path. If setting relative path, then directory is created
|
||||
relative to :ref:`rootdir <rootdir>`. Additionally path may contain environment
|
||||
variables, that will be expanded. For more information about cache plugin
|
||||
please refer to :ref:`cache_provider`.
|
||||
|
|
|
@ -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 @@ class TestNewAPI(object):
|
|||
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("""
|
||||
|
|
Loading…
Reference in New Issue