ini option cache_dir

This commit is contained in:
V.Kuznetsov 2017-07-07 13:07:06 +03:00
parent ef62b86335
commit f471eef661
1 changed files with 15 additions and 1 deletions

View File

@ -8,13 +8,14 @@ from __future__ import absolute_import, division, print_function
import py import py
import pytest import pytest
import json import json
import os
from os.path import sep as _sep, altsep as _altsep from os.path import sep as _sep, altsep as _altsep
class Cache(object): class Cache(object):
def __init__(self, config): def __init__(self, config):
self.config = 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") self.trace = config.trace.root.get("cache")
if config.getvalue("cacheclear"): if config.getvalue("cacheclear"):
self.trace("clearing cachedir") self.trace("clearing cachedir")
@ -22,6 +23,16 @@ class Cache(object):
self._cachedir.remove() self._cachedir.remove()
self._cachedir.mkdir() 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): def makedir(self, name):
""" return a directory path object with the given name. If the """ return a directory path object with the given name. If the
directory does not yet exist, it will be created. You can use it directory does not yet exist, it will be created. You can use it
@ -171,6 +182,9 @@ def pytest_addoption(parser):
group.addoption( group.addoption(
'--cache-clear', action='store_true', dest="cacheclear", '--cache-clear', action='store_true', 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='.cache',
help="cache directory path.")
def pytest_cmdline_main(config): def pytest_cmdline_main(config):