Improve cache test and fix it in Docker (#8785)

* cache: Move repetitive code to fixture

* cache: Explicitly test for chmod result

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix lint

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Florian Bruhin 2021-06-23 20:28:09 +02:00 committed by GitHub
parent e44300de7e
commit f573b56bb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 38 deletions

View File

@ -1,7 +1,7 @@
import os import os
import shutil import shutil
import sys
from pathlib import Path from pathlib import Path
from typing import Generator
from typing import List from typing import List
import pytest import pytest
@ -44,52 +44,54 @@ class TestNewAPI:
assert cache is not None assert cache is not None
cache.set("test/broken", []) cache.set("test/broken", [])
@pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows") @pytest.fixture
def unwritable_cache_dir(self, pytester: Pytester) -> Generator[Path, None, None]:
cache_dir = pytester.path.joinpath(".pytest_cache")
cache_dir.mkdir()
mode = cache_dir.stat().st_mode
cache_dir.chmod(0)
if os.access(cache_dir, os.W_OK):
pytest.skip("Failed to make cache dir unwritable")
yield cache_dir
cache_dir.chmod(mode)
@pytest.mark.filterwarnings( @pytest.mark.filterwarnings(
"ignore:could not create cache path:pytest.PytestWarning" "ignore:could not create cache path:pytest.PytestWarning"
) )
def test_cache_writefail_permissions(self, pytester: Pytester) -> None: def test_cache_writefail_permissions(
self, unwritable_cache_dir: Path, pytester: Pytester
) -> None:
pytester.makeini("[pytest]") pytester.makeini("[pytest]")
cache_dir = pytester.path.joinpath(".pytest_cache") config = pytester.parseconfigure()
cache_dir.mkdir() cache = config.cache
mode = cache_dir.stat().st_mode assert cache is not None
cache_dir.chmod(0) cache.set("test/broken", [])
try:
config = pytester.parseconfigure()
cache = config.cache
assert cache is not None
cache.set("test/broken", [])
finally:
cache_dir.chmod(mode)
@pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows")
@pytest.mark.filterwarnings("default") @pytest.mark.filterwarnings("default")
def test_cache_failure_warns( def test_cache_failure_warns(
self, pytester: Pytester, monkeypatch: MonkeyPatch self,
pytester: Pytester,
monkeypatch: MonkeyPatch,
unwritable_cache_dir: Path,
) -> None: ) -> None:
monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1") monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
cache_dir = pytester.path.joinpath(".pytest_cache")
cache_dir.mkdir() pytester.makepyfile("def test_error(): raise Exception")
mode = cache_dir.stat().st_mode result = pytester.runpytest()
cache_dir.chmod(0) assert result.ret == 1
try: # warnings from nodeids, lastfailed, and stepwise
pytester.makepyfile("def test_error(): raise Exception") result.stdout.fnmatch_lines(
result = pytester.runpytest() [
assert result.ret == 1 # Validate location/stacklevel of warning from cacheprovider.
# warnings from nodeids, lastfailed, and stepwise "*= warnings summary =*",
result.stdout.fnmatch_lines( "*/cacheprovider.py:*",
[ " */cacheprovider.py:*: PytestCacheWarning: could not create cache path "
# Validate location/stacklevel of warning from cacheprovider. f"{unwritable_cache_dir}/v/cache/nodeids",
"*= warnings summary =*", ' config.cache.set("cache/nodeids", sorted(self.cached_nodeids))',
"*/cacheprovider.py:*", "*1 failed, 3 warnings in*",
" */cacheprovider.py:*: PytestCacheWarning: could not create cache path " ]
"{}/v/cache/nodeids".format(cache_dir), )
' config.cache.set("cache/nodeids", sorted(self.cached_nodeids))',
"*1 failed, 3 warnings in*",
]
)
finally:
cache_dir.chmod(mode)
def test_config_cache(self, pytester: Pytester) -> None: def test_config_cache(self, pytester: Pytester) -> None:
pytester.makeconftest( pytester.makeconftest(