Skip to content

Commit

Permalink
test: remove cachy dependency and cache compatiblity tests (#7437)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Mokry <martin-kokos@users.noreply.github.com>
  • Loading branch information
martin-kokos and martin-kokos authored May 23, 2023
1 parent 2d86ebf commit 776a917
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 143 deletions.
18 changes: 1 addition & 17 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ urllib3 = "^1.26.0"
pre-commit = "^2.6"

[tool.poetry.group.test.dependencies]
# Cachy frozen to test backwards compatibility for `poetry.utils.cache`.
cachy = "0.3.0"
deepdiff = "^6.3"
httpretty = "^1.0"
pytest = "^7.1"
Expand Down Expand Up @@ -184,7 +182,6 @@ warn_unused_ignores = false
[[tool.mypy.overrides]]
module = [
'cachecontrol.*',
'cachy.*',
'deepdiff.*',
'httpretty.*',
'keyring.*',
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from poetry.utils.env import Env


# Used by Cachy for items that do not expire.
# Used by FileCache for items that do not expire.
MAX_DATE = 9999999999
T = TypeVar("T")

Expand Down
27 changes: 12 additions & 15 deletions tests/console/commands/cache/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import uuid

from typing import TYPE_CHECKING
from typing import TypeVar

import pytest

from cachy import CacheManager
from poetry.utils.cache import FileCache


if TYPE_CHECKING:
Expand All @@ -16,6 +17,8 @@

from tests.conftest import Config

T = TypeVar("T")


@pytest.fixture
def repository_cache_dir(monkeypatch: MonkeyPatch, config: Config) -> Path:
Expand Down Expand Up @@ -47,19 +50,13 @@ def cache(
repository_cache_dir: Path,
repository_one: str,
mock_caches: None,
) -> CacheManager:
cache = CacheManager(
{
"default": repository_one,
"serializer": "json",
"stores": {
repository_one: {
"driver": "file",
"path": str(repository_cache_dir / repository_one),
}
},
}
) -> FileCache[dict[str, str]]:
cache: FileCache[dict[str, str]] = FileCache(
path=repository_cache_dir / repository_one
)

cache.remember(
"cachy:0.1", lambda: {"name": "cachy", "version": "0.1"}, minutes=None
)
cache.remember_forever("cachy:0.1", lambda: {"name": "cachy", "version": "0.1"})
cache.remember_forever("cleo:0.2", lambda: {"name": "cleo", "version": "0.2"})
cache.remember("cleo:0.2", lambda: {"name": "cleo", "version": "0.2"}, minutes=None)
return cache
13 changes: 8 additions & 5 deletions tests/console/commands/cache/test_clear.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TypeVar

import pytest

Expand All @@ -12,7 +13,9 @@
if TYPE_CHECKING:
from pathlib import Path

from cachy import CacheManager
from poetry.utils.cache import FileCache

T = TypeVar("T")


@pytest.fixture
Expand All @@ -27,7 +30,7 @@ def test_cache_clear_all(
tester: ApplicationTester,
repository_one: str,
repository_cache_dir: Path,
cache: CacheManager,
cache: FileCache[T],
) -> None:
exit_code = tester.execute(f"cache clear {repository_one} --all", inputs="yes")
repository_one_dir = repository_cache_dir / repository_one
Expand All @@ -44,7 +47,7 @@ def test_cache_clear_all_no(
tester: ApplicationTester,
repository_one: str,
repository_cache_dir: Path,
cache: CacheManager,
cache: FileCache[T],
) -> None:
exit_code = tester.execute(f"cache clear {repository_one} --all", inputs="no")

Expand All @@ -60,7 +63,7 @@ def test_cache_clear_all_no(
def test_cache_clear_pkg(
tester: ApplicationTester,
repository_one: str,
cache: CacheManager,
cache: FileCache[T],
package_name: str,
) -> None:
exit_code = tester.execute(
Expand All @@ -76,7 +79,7 @@ def test_cache_clear_pkg(
def test_cache_clear_pkg_no(
tester: ApplicationTester,
repository_one: str,
cache: CacheManager,
cache: FileCache[T],
) -> None:
exit_code = tester.execute(f"cache clear {repository_one}:cachy:0.1", inputs="no")

Expand Down
120 changes: 18 additions & 102 deletions tests/utils/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import TypeVar
from typing import Union
from unittest.mock import Mock

import pytest

from cachy import CacheManager
from packaging.tags import Tag
from poetry.core.packages.utils.link import Link

Expand All @@ -21,85 +17,35 @@


if TYPE_CHECKING:
from _pytest.monkeypatch import MonkeyPatch
from pytest import FixtureRequest
from typing import Any

from pytest_mock import MockerFixture

from tests.conftest import Config
from tests.types import FixtureDirGetter


FILE_CACHE = Union[FileCache, CacheManager]
T = TypeVar("T")


@pytest.fixture
def repository_cache_dir(monkeypatch: MonkeyPatch, config: Config) -> Path:
def repository_cache_dir(config: Config) -> Path:
return config.repository_cache_directory


def patch_cachy(cache: CacheManager) -> CacheManager:
old_put = cache.put
old_remember = cache.remember

def new_put(key: str, value: Any, minutes: int | None = None) -> Any:
if minutes is not None:
return old_put(key, value, minutes=minutes)
else:
return cache.forever(key, value)

cache.put = new_put

def new_remember(key: str, value: Any, minutes: int | None = None) -> Any:
if minutes is not None:
return old_remember(key, value, minutes=minutes)
else:
return cache.remember_forever(key, value)

cache.remember = new_remember
return cache


@pytest.fixture
def cachy_file_cache(repository_cache_dir: Path) -> CacheManager:
cache = CacheManager(
{
"default": "cache",
"serializer": "json",
"stores": {
"cache": {"driver": "file", "path": str(repository_cache_dir / "cache")}
},
}
)
return patch_cachy(cache)


@pytest.fixture
def poetry_file_cache(repository_cache_dir: Path) -> FileCache[T]:
def poetry_file_cache(repository_cache_dir: Path) -> FileCache[Any]:
return FileCache(repository_cache_dir / "cache")


@pytest.fixture
def cachy_dict_cache() -> CacheManager:
cache = CacheManager(
{
"default": "cache",
"serializer": "json",
"stores": {"cache": {"driver": "dict"}},
}
)
return patch_cachy(cache)


def test_cache_validates(repository_cache_dir: Path) -> None:
with pytest.raises(ValueError) as e:
FileCache(repository_cache_dir / "cache", hash_type="unknown")
assert str(e.value) == "FileCache.hash_type is unknown value: 'unknown'."


@pytest.mark.parametrize("cache_name", ["cachy_file_cache", "poetry_file_cache"])
def test_cache_get_put_has(cache_name: str, request: FixtureRequest) -> None:
cache = request.getfixturevalue(cache_name)
def test_cache_get_put_has(repository_cache_dir: Path) -> None:
cache: FileCache[Any] = FileCache(repository_cache_dir / "cache")
cache.put("key1", "value")
cache.put("key2", {"a": ["json-encoded", "value"]})

Expand All @@ -110,9 +56,8 @@ def test_cache_get_put_has(cache_name: str, request: FixtureRequest) -> None:
assert not cache.has("key3")


@pytest.mark.parametrize("cache_name", ["cachy_file_cache", "poetry_file_cache"])
def test_cache_forget(cache_name: str, request: FixtureRequest) -> None:
cache = request.getfixturevalue(cache_name)
def test_cache_forget(repository_cache_dir: Path) -> None:
cache: FileCache[Any] = FileCache(repository_cache_dir / "cache")
cache.put("key1", "value")
cache.put("key2", "value")

Expand All @@ -125,9 +70,8 @@ def test_cache_forget(cache_name: str, request: FixtureRequest) -> None:
assert cache.has("key2")


@pytest.mark.parametrize("cache_name", ["cachy_file_cache", "poetry_file_cache"])
def test_cache_flush(cache_name: str, request: FixtureRequest) -> None:
cache = request.getfixturevalue(cache_name)
def test_cache_flush(repository_cache_dir: Path) -> None:
cache: FileCache[Any] = FileCache(repository_cache_dir / "cache")
cache.put("key1", "value")
cache.put("key2", "value")

Expand All @@ -140,13 +84,10 @@ def test_cache_flush(cache_name: str, request: FixtureRequest) -> None:
assert not cache.has("key2")


@pytest.mark.parametrize("cache_name", ["cachy_file_cache", "poetry_file_cache"])
def test_cache_remember(
cache_name: str, request: FixtureRequest, mocker: MockerFixture
) -> None:
cache = request.getfixturevalue(cache_name)
def test_cache_remember(repository_cache_dir: Path, mocker: MockerFixture) -> None:
cache: FileCache[Any] = FileCache(repository_cache_dir / "cache")

method = Mock(return_value="value2")
method = mocker.Mock(return_value="value2")
cache.put("key1", "value1")
assert cache.remember("key1", method) == "value1"
method.assert_not_called()
Expand All @@ -155,15 +96,11 @@ def test_cache_remember(
method.assert_called()


@pytest.mark.parametrize("cache_name", ["cachy_file_cache", "poetry_file_cache"])
def test_cache_get_limited_minutes(
mocker: MockerFixture,
cache_name: str,
request: FixtureRequest,
repository_cache_dir: Path, mocker: MockerFixture
) -> None:
cache = request.getfixturevalue(cache_name)
cache: FileCache[Any] = FileCache(repository_cache_dir / "cache")

# needs to be 10 digits because cachy assumes it's a 10-digit int.
start_time = 1111111111

mocker.patch("time.time", return_value=start_time)
Expand All @@ -179,28 +116,7 @@ def test_cache_get_limited_minutes(
assert cache.get("key2") is None


def test_cachy_compatibility(
cachy_file_cache: CacheManager, poetry_file_cache: FileCache[T]
) -> None:
"""
The new file cache should be able to support reading legacy caches.
"""
test_str = "value"
test_obj = {"a": ["json", "object"]}
cachy_file_cache.put("key1", test_str)
cachy_file_cache.put("key2", test_obj)

assert poetry_file_cache.get("key1") == test_str
assert poetry_file_cache.get("key2") == test_obj

poetry_file_cache.put("key3", test_str)
poetry_file_cache.put("key4", test_obj)

assert cachy_file_cache.get("key3") == test_str
assert cachy_file_cache.get("key4") == test_obj


def test_missing_cache_file(poetry_file_cache: FileCache[T]) -> None:
def test_missing_cache_file(poetry_file_cache: FileCache[Any]) -> None:
poetry_file_cache.put("key1", "value")

key1_path = (
Expand All @@ -213,7 +129,7 @@ def test_missing_cache_file(poetry_file_cache: FileCache[T]) -> None:
assert poetry_file_cache.get("key1") is None


def test_missing_cache_path(poetry_file_cache: FileCache[T]) -> None:
def test_missing_cache_path(poetry_file_cache: FileCache[Any]) -> None:
poetry_file_cache.put("key1", "value")

key1_partial_path = poetry_file_cache.path / "81/74/09/96/87/a2/"
Expand All @@ -237,7 +153,7 @@ def test_missing_cache_path(poetry_file_cache: FileCache[T]) -> None:
],
)
def test_detect_corrupted_cache_key_file(
corrupt_payload: str | bytes, poetry_file_cache: FileCache[T]
corrupt_payload: str | bytes, poetry_file_cache: FileCache[Any]
) -> None:
poetry_file_cache.put("key1", "value")

Expand Down

0 comments on commit 776a917

Please sign in to comment.