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,35 +44,39 @@ 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
@pytest.mark.filterwarnings( def unwritable_cache_dir(self, pytester: Pytester) -> Generator[Path, None, None]:
"ignore:could not create cache path:pytest.PytestWarning"
)
def test_cache_writefail_permissions(self, pytester: Pytester) -> None:
pytester.makeini("[pytest]")
cache_dir = pytester.path.joinpath(".pytest_cache") cache_dir = pytester.path.joinpath(".pytest_cache")
cache_dir.mkdir() cache_dir.mkdir()
mode = cache_dir.stat().st_mode mode = cache_dir.stat().st_mode
cache_dir.chmod(0) cache_dir.chmod(0)
try: 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(
"ignore:could not create cache path:pytest.PytestWarning"
)
def test_cache_writefail_permissions(
self, unwritable_cache_dir: Path, pytester: Pytester
) -> None:
pytester.makeini("[pytest]")
config = pytester.parseconfigure() config = pytester.parseconfigure()
cache = config.cache cache = config.cache
assert cache is not None assert cache is not None
cache.set("test/broken", []) 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()
mode = cache_dir.stat().st_mode
cache_dir.chmod(0)
try:
pytester.makepyfile("def test_error(): raise Exception") pytester.makepyfile("def test_error(): raise Exception")
result = pytester.runpytest() result = pytester.runpytest()
assert result.ret == 1 assert result.ret == 1
@ -83,13 +87,11 @@ class TestNewAPI:
"*= warnings summary =*", "*= warnings summary =*",
"*/cacheprovider.py:*", "*/cacheprovider.py:*",
" */cacheprovider.py:*: PytestCacheWarning: could not create cache path " " */cacheprovider.py:*: PytestCacheWarning: could not create cache path "
"{}/v/cache/nodeids".format(cache_dir), f"{unwritable_cache_dir}/v/cache/nodeids",
' config.cache.set("cache/nodeids", sorted(self.cached_nodeids))', ' config.cache.set("cache/nodeids", sorted(self.cached_nodeids))',
"*1 failed, 3 warnings in*", "*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(