From 31f089db6ade6e345f4cab23fe4fa0a838c34f47 Mon Sep 17 00:00:00 2001 From: avirlrma Date: Thu, 21 Jun 2018 13:10:42 +0530 Subject: [PATCH 1/8] add reamde for .pytest_cache method - `ensure_readme()` --- src/_pytest/cacheprovider.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index eb0fcc06f..ff2d1e285 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -103,6 +103,22 @@ class Cache(object): with f: self.trace("cache-write %s: %r" % (key, value)) json.dump(value, f, indent=2, sort_keys=True) + self._ensure_readme() + + def _ensure_readme(self): + content_readme = '''# pytest cache directory # + +This directory contains data from the pytest's cache plugin, \ +which provides the `--lf` and `--ff` options, as well as the `cache` fixture. + +**Do not** commit this to version control. + +See [the docs](https://docs.pytest.org/en/latest/cache.html) for more information. + ''' + if self._cachedir.check(dir=True): + readme_path = self._cachedir.join("README.md") + if not readme_path.check(file=True): + readme_path.write(content_readme) class LFPlugin(object): From 53d4710c6287fc3e78c2bc1704bd702d05a8b6de Mon Sep 17 00:00:00 2001 From: avirlrma Date: Thu, 21 Jun 2018 14:25:00 +0530 Subject: [PATCH 2/8] added tests for .pytest_cache README Helper class to check if readme exists in .pytest_cache directory Tests to check for readme when tests pass and when they fail --- src/_pytest/cacheprovider.py | 9 +++----- testing/test_cacheREADME.py | 42 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 testing/test_cacheREADME.py diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index ff2d1e285..b1904fbb1 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -18,7 +18,6 @@ from os.path import sep as _sep, altsep as _altsep class Cache(object): - def __init__(self, config): self.config = config self._cachedir = Cache.cache_dir_from_config(config) @@ -106,7 +105,7 @@ class Cache(object): self._ensure_readme() def _ensure_readme(self): - content_readme = '''# pytest cache directory # + content_readme = """# pytest cache directory # This directory contains data from the pytest's cache plugin, \ which provides the `--lf` and `--ff` options, as well as the `cache` fixture. @@ -114,7 +113,7 @@ which provides the `--lf` and `--ff` options, as well as the `cache` fixture. **Do not** commit this to version control. See [the docs](https://docs.pytest.org/en/latest/cache.html) for more information. - ''' + """ if self._cachedir.check(dir=True): readme_path = self._cachedir.join("README.md") if not readme_path.check(file=True): @@ -215,9 +214,7 @@ class NFPlugin(object): items[:] = self._get_increasing_order( six.itervalues(new_items) - ) + self._get_increasing_order( - six.itervalues(other_items) - ) + ) + self._get_increasing_order(six.itervalues(other_items)) self.cached_nodeids = [x.nodeid for x in items if isinstance(x, pytest.Item)] def _get_increasing_order(self, items): diff --git a/testing/test_cacheREADME.py b/testing/test_cacheREADME.py new file mode 100644 index 000000000..fe8879bd2 --- /dev/null +++ b/testing/test_cacheREADME.py @@ -0,0 +1,42 @@ +from __future__ import absolute_import, division, print_function +import sys +import py +import _pytest +import pytest +import os +import shutil + +pytest_plugins = ("pytester",) + + +class Helper(object): + def check_readme(self, testdir): + config = testdir.parseconfigure() + readme = config.cache._cachedir.join("README.md") + return readme.isfile() + + +class TestReadme(Helper): + def test_readme_passed(self, testdir): + testdir.tmpdir.join("test_a.py").write( + _pytest._code.Source( + """ + def test_always_passes(): + assert 1 + """ + ) + ) + result = testdir.runpytest() + assert self.check_readme(testdir) == True + + def test_readme_failed(self, testdir): + testdir.tmpdir.join("test_a.py").write( + _pytest._code.Source( + """ + def test_always_passes(): + assert 0 + """ + ) + ) + result = testdir.runpytest() + assert self.check_readme(testdir) == True From 8f1d8ac970efb1fb5c00af4a0c5575922577afda Mon Sep 17 00:00:00 2001 From: avirlrma Date: Thu, 21 Jun 2018 15:15:55 +0530 Subject: [PATCH 3/8] fixed linting errors ran black removed unused imports and variables --- src/_pytest/cacheprovider.py | 7 +++++-- testing/test_cacheREADME.py | 15 ++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index b1904fbb1..5d9b58a65 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -18,6 +18,7 @@ from os.path import sep as _sep, altsep as _altsep class Cache(object): + def __init__(self, config): self.config = config self._cachedir = Cache.cache_dir_from_config(config) @@ -108,7 +109,7 @@ class Cache(object): content_readme = """# pytest cache directory # This directory contains data from the pytest's cache plugin, \ -which provides the `--lf` and `--ff` options, as well as the `cache` fixture. +which provides the `--lf` and `--ff` options, as well as the `cache` fixture. **Do not** commit this to version control. @@ -214,7 +215,9 @@ class NFPlugin(object): items[:] = self._get_increasing_order( six.itervalues(new_items) - ) + self._get_increasing_order(six.itervalues(other_items)) + ) + self._get_increasing_order( + six.itervalues(other_items) + ) self.cached_nodeids = [x.nodeid for x in items if isinstance(x, pytest.Item)] def _get_increasing_order(self, items): diff --git a/testing/test_cacheREADME.py b/testing/test_cacheREADME.py index fe8879bd2..3e5673609 100644 --- a/testing/test_cacheREADME.py +++ b/testing/test_cacheREADME.py @@ -1,15 +1,11 @@ from __future__ import absolute_import, division, print_function -import sys -import py import _pytest -import pytest -import os -import shutil pytest_plugins = ("pytester",) class Helper(object): + def check_readme(self, testdir): config = testdir.parseconfigure() readme = config.cache._cachedir.join("README.md") @@ -17,6 +13,7 @@ class Helper(object): class TestReadme(Helper): + def test_readme_passed(self, testdir): testdir.tmpdir.join("test_a.py").write( _pytest._code.Source( @@ -26,8 +23,8 @@ class TestReadme(Helper): """ ) ) - result = testdir.runpytest() - assert self.check_readme(testdir) == True + testdir.runpytest() + assert self.check_readme(testdir) is True def test_readme_failed(self, testdir): testdir.tmpdir.join("test_a.py").write( @@ -38,5 +35,5 @@ class TestReadme(Helper): """ ) ) - result = testdir.runpytest() - assert self.check_readme(testdir) == True + testdir.runpytest() + assert self.check_readme(testdir) is True From c672bfa32ea9e3602225eb45b0ffdc4cdb1ce47a Mon Sep 17 00:00:00 2001 From: avirlrma Date: Thu, 21 Jun 2018 17:43:10 +0530 Subject: [PATCH 4/8] added changelog entry moved cache readme tests to test_cacheprovider.py --- changelog/3519.feature.rst | 1 + src/_pytest/cacheprovider.py | 15 +++++++++----- testing/test_cacheREADME.py | 39 ----------------------------------- testing/test_cacheprovider.py | 30 ++++++++++++++++++++++++++- 4 files changed, 40 insertions(+), 45 deletions(-) create mode 100644 changelog/3519.feature.rst delete mode 100644 testing/test_cacheREADME.py diff --git a/changelog/3519.feature.rst b/changelog/3519.feature.rst new file mode 100644 index 000000000..4526c63ad --- /dev/null +++ b/changelog/3519.feature.rst @@ -0,0 +1 @@ +now `.pytest_cache` is created with a README.md to indicate it's usage,options etc. \ No newline at end of file diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index 5d9b58a65..10564ab1d 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -15,6 +15,7 @@ import pytest import json import os from os.path import sep as _sep, altsep as _altsep +from textwrap import dedent class Cache(object): @@ -106,15 +107,19 @@ class Cache(object): self._ensure_readme() def _ensure_readme(self): - content_readme = """# pytest cache directory # -This directory contains data from the pytest's cache plugin, \ -which provides the `--lf` and `--ff` options, as well as the `cache` fixture. + content_readme = dedent( + """\ + # pytest cache directory # -**Do not** commit this to version control. + This directory contains data from the pytest's cache plugin,\ + which provides the `--lf` and `--ff` options, as well as the `cache` fixture. -See [the docs](https://docs.pytest.org/en/latest/cache.html) for more information. + **Do not** commit this to version control. + + See [the docs](https://docs.pytest.org/en/latest/cache.html) for more information. """ + ) if self._cachedir.check(dir=True): readme_path = self._cachedir.join("README.md") if not readme_path.check(file=True): diff --git a/testing/test_cacheREADME.py b/testing/test_cacheREADME.py deleted file mode 100644 index 3e5673609..000000000 --- a/testing/test_cacheREADME.py +++ /dev/null @@ -1,39 +0,0 @@ -from __future__ import absolute_import, division, print_function -import _pytest - -pytest_plugins = ("pytester",) - - -class Helper(object): - - def check_readme(self, testdir): - config = testdir.parseconfigure() - readme = config.cache._cachedir.join("README.md") - return readme.isfile() - - -class TestReadme(Helper): - - def test_readme_passed(self, testdir): - testdir.tmpdir.join("test_a.py").write( - _pytest._code.Source( - """ - def test_always_passes(): - assert 1 - """ - ) - ) - testdir.runpytest() - assert self.check_readme(testdir) is True - - def test_readme_failed(self, testdir): - testdir.tmpdir.join("test_a.py").write( - _pytest._code.Source( - """ - def test_always_passes(): - assert 0 - """ - ) - ) - testdir.runpytest() - assert self.check_readme(testdir) is True diff --git a/testing/test_cacheprovider.py b/testing/test_cacheprovider.py index 33d1dd844..4a7c25aa0 100644 --- a/testing/test_cacheprovider.py +++ b/testing/test_cacheprovider.py @@ -6,7 +6,7 @@ import pytest import os import shutil -pytest_plugins = "pytester", +pytest_plugins = ("pytester",) class TestNewAPI(object): @@ -818,3 +818,31 @@ class TestNewFirst(object): "*test_1/test_1.py::test_1[2*", ] ) + + +class TestReadme(object): + + def check_readme(self, testdir): + config = testdir.parseconfigure() + readme = config.cache._cachedir.join("README.md") + return readme.isfile() + + def test_readme_passed(self, testdir): + testdir.makepyfile( + """ + def test_always_passes(): + assert 1 + """ + ) + testdir.runpytest() + assert self.check_readme(testdir) is True + + def test_readme_failed(self, testdir): + testdir.makepyfile( + """ + def test_always_passes(): + assert 0 + """ + ) + testdir.runpytest() + assert self.check_readme(testdir) is True From 998d540b73df9a12975b1d3cacb13b540500c6d4 Mon Sep 17 00:00:00 2001 From: avirlrma Date: Thu, 21 Jun 2018 17:56:26 +0530 Subject: [PATCH 5/8] fixed changelog entry --- changelog/3519.feature.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/3519.feature.rst b/changelog/3519.feature.rst index 4526c63ad..0408b8aa7 100644 --- a/changelog/3519.feature.rst +++ b/changelog/3519.feature.rst @@ -1 +1 @@ -now `.pytest_cache` is created with a README.md to indicate it's usage,options etc. \ No newline at end of file +now ``.pytest_cache`` is created with a README.md to indicate it's usage,options etc. From b897008887efba5beea1b1aaa517abb09da13a7b Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 21 Jun 2018 20:09:32 -0300 Subject: [PATCH 6/8] Improve CHANGELOG grammar --- changelog/3519.feature.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/3519.feature.rst b/changelog/3519.feature.rst index 0408b8aa7..a36c4b492 100644 --- a/changelog/3519.feature.rst +++ b/changelog/3519.feature.rst @@ -1 +1 @@ -now ``.pytest_cache`` is created with a README.md to indicate it's usage,options etc. +Now a ``README.md`` file is created in ``.pytest_cache`` to make it clear why the directory exists. From eb94bce3e272bdfc45220208cb070ff908d532b0 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 21 Jun 2018 20:10:18 -0300 Subject: [PATCH 7/8] Change 3519 to trivial --- changelog/{3519.feature.rst => 3519.trivial.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog/{3519.feature.rst => 3519.trivial.rst} (100%) diff --git a/changelog/3519.feature.rst b/changelog/3519.trivial.rst similarity index 100% rename from changelog/3519.feature.rst rename to changelog/3519.trivial.rst From 0d3914b62691ef4dbb38a897b7b4a8513b2912e9 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 21 Jun 2018 20:12:17 -0300 Subject: [PATCH 8/8] Remove extra '\' left at the end of a line in cache's readme contents --- src/_pytest/cacheprovider.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index 10564ab1d..f15398383 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -112,7 +112,7 @@ class Cache(object): """\ # pytest cache directory # - This directory contains data from the pytest's cache plugin,\ + This directory contains data from the pytest's cache plugin, which provides the `--lf` and `--ff` options, as well as the `cache` fixture. **Do not** commit this to version control.