From 19e522ad698ead426d79f2e7dbd90ff573250b3e Mon Sep 17 00:00:00 2001 From: RockBomber Date: Tue, 4 Jul 2017 20:32:59 +0300 Subject: [PATCH 1/9] Added cache_dirname ini option --- _pytest/cacheprovider.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/_pytest/cacheprovider.py b/_pytest/cacheprovider.py index 7fc08fff368..dd1ffa00808 100755 --- a/_pytest/cacheprovider.py +++ b/_pytest/cacheprovider.py @@ -14,7 +14,8 @@ class Cache(object): def __init__(self, config): self.config = config - self._cachedir = config.rootdir.join(".cache") + cache_dirname = config.getini("cache_dirname") + self._cachedir = config.rootdir.join(cache_dirname) self.trace = config.trace.root.get("cache") if config.getvalue("cacheclear"): self.trace("clearing cachedir") @@ -171,6 +172,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_dirname", default='.cache', + help="directory name for cache content.") def pytest_cmdline_main(config): From 36a7f41e212ac185bfc6bde2bc2079ce49ae7a2b Mon Sep 17 00:00:00 2001 From: RockBomber Date: Tue, 4 Jul 2017 20:33:53 +0300 Subject: [PATCH 2/9] Added test for custom cache dirname --- testing/test_cache.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/testing/test_cache.py b/testing/test_cache.py index 600b5e6d9fc..b508329a848 100755 --- a/testing/test_cache.py +++ b/testing/test_cache.py @@ -87,6 +87,14 @@ def test_cachefuncarg(cache): assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed*"]) + def test_custom_cache_dirname(self, testdir): + testdir.makeini(""" + [pytest] + cache_dirname = custom_cache_dirname + """) + testdir.makepyfile(test_errored='def test_error():\n assert False') + testdir.runpytest() + assert os.path.exists('custom_cache_dirname') def test_cache_reportheader(testdir): From 76b8e323282511bcf7603dec6eba31b0ab9a80d9 Mon Sep 17 00:00:00 2001 From: RockBomber Date: Tue, 4 Jul 2017 20:35:06 +0300 Subject: [PATCH 3/9] Added docs for cache_dirname ini option --- doc/en/cache.rst | 2 ++ doc/en/customize.rst | 8 ++++++++ 2 files changed, 10 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..ef2d5f29d54 100644 --- a/doc/en/customize.rst +++ b/doc/en/customize.rst @@ -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_dirname + + .. 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`. From db359e7547b2b89886089e1de4dd4591d6caca80 Mon Sep 17 00:00:00 2001 From: "V.Kuznetsov" Date: Wed, 5 Jul 2017 11:29:38 +0300 Subject: [PATCH 4/9] Added fail if cache_dirname is absolute path or home dir --- _pytest/cacheprovider.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/_pytest/cacheprovider.py b/_pytest/cacheprovider.py index dd1ffa00808..55d0c3c4c33 100755 --- a/_pytest/cacheprovider.py +++ b/_pytest/cacheprovider.py @@ -8,13 +8,16 @@ 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 cache_dirname = config.getini("cache_dirname") + if _isabs(_expanduser(cache_dirname)): + raise ValueError("cache dirname must be relative path, not absolute") self._cachedir = config.rootdir.join(cache_dirname) self.trace = config.trace.root.get("cache") if config.getvalue("cacheclear"): From cac1892b0caa6a211b373f035fcbe58285cc8b45 Mon Sep 17 00:00:00 2001 From: "V.Kuznetsov" Date: Wed, 5 Jul 2017 11:30:30 +0300 Subject: [PATCH 5/9] Added test for fail if cache_dirname is absolute path or home dir --- testing/test_cache.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/testing/test_cache.py b/testing/test_cache.py index b508329a848..6e4d31e8908 100755 --- a/testing/test_cache.py +++ b/testing/test_cache.py @@ -96,6 +96,21 @@ def test_custom_cache_dirname(self, testdir): testdir.runpytest() assert os.path.exists('custom_cache_dirname') + @pytest.mark.parametrize('custom_cache_dirname', [ + pytest.param('/tmp/mycache', marks=pytest.mark.skipif(sys.platform == 'win32', + reason='test for linux pathes')), + pytest.param('~/mycache', marks=pytest.mark.skipif(sys.platform == 'win32', + reason='test for linux pathes')), + pytest.param('C:\mycache', marks=pytest.mark.skipif(sys.platform != 'win32', + reason='test for win32 pathes')), + ]) + def test_cache_dirname_fail_on_abs(self, testdir, custom_cache_dirname): + testdir.makeini(""" + [pytest] + cache_dirname = {} + """.format(custom_cache_dirname)) + pytest.raises(ValueError, "testdir.parseconfigure()", + message="cache dirname must be relative path, not absolute") def test_cache_reportheader(testdir): testdir.makepyfile(""" From 123c139ad2b12cd88b78652bcaad079f2a7173d9 Mon Sep 17 00:00:00 2001 From: "V.Kuznetsov" Date: Wed, 5 Jul 2017 12:20:32 +0300 Subject: [PATCH 6/9] Fix zero length field name in format --- testing/test_cache.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testing/test_cache.py b/testing/test_cache.py index 6e4d31e8908..8038c25bc99 100755 --- a/testing/test_cache.py +++ b/testing/test_cache.py @@ -107,8 +107,8 @@ def test_custom_cache_dirname(self, testdir): def test_cache_dirname_fail_on_abs(self, testdir, custom_cache_dirname): testdir.makeini(""" [pytest] - cache_dirname = {} - """.format(custom_cache_dirname)) + cache_dirname = {custom_cache_dirname} + """.format(custom_cache_dirname=custom_cache_dirname)) pytest.raises(ValueError, "testdir.parseconfigure()", message="cache dirname must be relative path, not absolute") From 3e7bf54ec8f41fa0ef49d9e438087ae049200316 Mon Sep 17 00:00:00 2001 From: "V.Kuznetsov" Date: Wed, 5 Jul 2017 14:28:39 +0300 Subject: [PATCH 7/9] Renamed ini option cache_dirname to cache_dir. Creating of cache dir moved to static method. --- _pytest/cacheprovider.py | 14 +++++++++----- doc/en/customize.rst | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/_pytest/cacheprovider.py b/_pytest/cacheprovider.py index 55d0c3c4c33..cf28f657ef4 100755 --- a/_pytest/cacheprovider.py +++ b/_pytest/cacheprovider.py @@ -15,10 +15,7 @@ class Cache(object): def __init__(self, config): self.config = config - cache_dirname = config.getini("cache_dirname") - if _isabs(_expanduser(cache_dirname)): - raise ValueError("cache dirname must be relative path, not absolute") - self._cachedir = config.rootdir.join(cache_dirname) + self._cachedir = Cache.cache_dir_from_config(config) self.trace = config.trace.root.get("cache") if config.getvalue("cacheclear"): self.trace("clearing cachedir") @@ -26,6 +23,13 @@ def __init__(self, config): 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 @@ -176,7 +180,7 @@ def pytest_addoption(parser): '--cache-clear', action='store_true', dest="cacheclear", help="remove all cache contents at start of test run.") parser.addini( - "cache_dirname", default='.cache', + "cache_dir", default='.cache', help="directory name for cache content.") diff --git a/doc/en/customize.rst b/doc/en/customize.rst index ef2d5f29d54..7b5148b09f1 100644 --- a/doc/en/customize.rst +++ b/doc/en/customize.rst @@ -263,7 +263,7 @@ 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_dirname +.. confval:: cache_dir .. versionadded:: 3.2 From 6cc52c1cb0f4bdec046dc6e4cea3b94ccfe38f38 Mon Sep 17 00:00:00 2001 From: "V.Kuznetsov" Date: Wed, 5 Jul 2017 14:31:27 +0300 Subject: [PATCH 8/9] Fixed cache dir tests for requested changes --- testing/test_cache.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/testing/test_cache.py b/testing/test_cache.py index 8038c25bc99..e6bea8c41cc 100755 --- a/testing/test_cache.py +++ b/testing/test_cache.py @@ -88,27 +88,23 @@ def test_cachefuncarg(cache): result.stdout.fnmatch_lines(["*1 passed*"]) def test_custom_cache_dirname(self, testdir): + cache_dir = 'custom_cache_dirname' testdir.makeini(""" [pytest] - cache_dirname = custom_cache_dirname - """) + cache_dir = {cache_dir} + """.format(cache_dir=cache_dir)) testdir.makepyfile(test_errored='def test_error():\n assert False') testdir.runpytest() - assert os.path.exists('custom_cache_dirname') - - @pytest.mark.parametrize('custom_cache_dirname', [ - pytest.param('/tmp/mycache', marks=pytest.mark.skipif(sys.platform == 'win32', - reason='test for linux pathes')), - pytest.param('~/mycache', marks=pytest.mark.skipif(sys.platform == 'win32', - reason='test for linux pathes')), - pytest.param('C:\mycache', marks=pytest.mark.skipif(sys.platform != 'win32', - reason='test for win32 pathes')), - ]) - def test_cache_dirname_fail_on_abs(self, testdir, custom_cache_dirname): + assert testdir.tmpdir.join(cache_dir).isdir() + + @pytest.mark.parametrize('cache_dir', [ + os.path.abspath('tmp'), + os.path.join('~', 'tmp')]) + def test_cache_dirname_fail_on_abs(self, testdir, cache_dir): testdir.makeini(""" [pytest] - cache_dirname = {custom_cache_dirname} - """.format(custom_cache_dirname=custom_cache_dirname)) + cache_dir = {cache_dir} + """.format(cache_dir=cache_dir)) pytest.raises(ValueError, "testdir.parseconfigure()", message="cache dirname must be relative path, not absolute") From 867aaeb79b1c9939d5454d13002110997865211f Mon Sep 17 00:00:00 2001 From: "V.Kuznetsov" Date: Wed, 5 Jul 2017 15:50:24 +0300 Subject: [PATCH 9/9] Test with tilde as home dir failed on win32 --- testing/test_cache.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/testing/test_cache.py b/testing/test_cache.py index e6bea8c41cc..33afc4899db 100755 --- a/testing/test_cache.py +++ b/testing/test_cache.py @@ -98,8 +98,10 @@ def test_custom_cache_dirname(self, testdir): assert testdir.tmpdir.join(cache_dir).isdir() @pytest.mark.parametrize('cache_dir', [ - os.path.abspath('tmp'), - os.path.join('~', 'tmp')]) + 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]