|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import shutil |
| 4 | + |
3 | 5 | from typing import TYPE_CHECKING
|
4 | 6 | from typing import Any
|
5 | 7 | from typing import TypeVar
|
@@ -192,3 +194,63 @@ def test_cachy_compatibility(
|
192 | 194 |
|
193 | 195 | assert cachy_file_cache.get("key3") == test_str
|
194 | 196 | assert cachy_file_cache.get("key4") == test_obj
|
| 197 | + |
| 198 | + |
| 199 | +def test_missing_cache_file( |
| 200 | + poetry_file_cache: FileCache, mocker: MockerFixture |
| 201 | +) -> None: |
| 202 | + poetry_file_cache.put("key1", "value") |
| 203 | + |
| 204 | + key1_path = ( |
| 205 | + poetry_file_cache.path |
| 206 | + / "81/74/09/96/87/a2/66/21/8174099687a26621f4e2cdd7cc03b3dacedb3fb962255b1aafd033cabe831530" # noqa: E501 |
| 207 | + ) |
| 208 | + assert key1_path.exists() |
| 209 | + key1_path.unlink() # corrupt cache by removing a key file |
| 210 | + |
| 211 | + assert poetry_file_cache.get("key1") is None |
| 212 | + |
| 213 | + |
| 214 | +def test_missing_cache_path( |
| 215 | + poetry_file_cache: FileCache, mocker: MockerFixture |
| 216 | +) -> None: |
| 217 | + poetry_file_cache.put("key1", "value") |
| 218 | + |
| 219 | + key1_partial_path = poetry_file_cache.path / "81/74/09/96/87/a2/" |
| 220 | + assert key1_partial_path.exists() |
| 221 | + shutil.rmtree( |
| 222 | + key1_partial_path |
| 223 | + ) # corrupt cache by removing a subdirectory containting a key file |
| 224 | + |
| 225 | + assert poetry_file_cache.get("key1") is None |
| 226 | + |
| 227 | + |
| 228 | +@pytest.mark.parametrize( |
| 229 | + "corrupt_payload", |
| 230 | + [ |
| 231 | + "", # empty file |
| 232 | + b"\x00", # null |
| 233 | + "99999999", # truncated file |
| 234 | + '999999a999"value"', # corrupt lifetime |
| 235 | + b'9999999999"va\xd8\x00"', # invalid unicode |
| 236 | + "fil3systemFa!led", # garbage file |
| 237 | + ], |
| 238 | +) |
| 239 | +def test_detect_corrupted_cache_key_file( |
| 240 | + corrupt_payload: str | bytes, poetry_file_cache: FileCache, mocker: MockerFixture |
| 241 | +) -> None: |
| 242 | + poetry_file_cache.put("key1", "value") |
| 243 | + |
| 244 | + key1_path = ( |
| 245 | + poetry_file_cache.path |
| 246 | + / "81/74/09/96/87/a2/66/21/8174099687a26621f4e2cdd7cc03b3dacedb3fb962255b1aafd033cabe831530" # noqa: E501 |
| 247 | + ) |
| 248 | + assert key1_path.exists() |
| 249 | + |
| 250 | + # original content: 9999999999"value" |
| 251 | + |
| 252 | + write_modes = {str: "w", bytes: "wb"} |
| 253 | + with open(key1_path, write_modes[type(corrupt_payload)]) as f: |
| 254 | + f.write(corrupt_payload) # write corrupt data |
| 255 | + |
| 256 | + assert poetry_file_cache.get("key1") is None |
0 commit comments