feat(add cache dir):

This commit is contained in:
Alexander Myasoedov
2025-12-09 19:51:47 +02:00
parent 9a4fb05491
commit b9dc5de708
9 changed files with 110 additions and 14 deletions
+13 -3
View File
@@ -1,12 +1,17 @@
import os
import warnings
from pathlib import Path
import pytest
from cache_to_disk import delete_old_disk_caches
from sklearn.exceptions import InconsistentVersionWarning
from agentic_security.cache_config import ensure_cache_dir
from agentic_security.logutils import logger
CACHE_DIR = ensure_cache_dir(Path(__file__).parent / ".cache_to_disk")
from cache_to_disk import delete_old_disk_caches # noqa: E402 # isort: skip
# Silence noisy third-party warnings that do not impact test behavior
warnings.filterwarnings("ignore", category=InconsistentVersionWarning)
try:
@@ -29,5 +34,10 @@ def pytest_runtest_setup(item):
@pytest.fixture(autouse=True, scope="session")
def setup_delete_old_disk_caches():
logger.info("delete_old_disk_caches")
delete_old_disk_caches()
logger.info("delete_old_disk_caches at %s", CACHE_DIR)
try:
delete_old_disk_caches()
except PermissionError:
logger.warning("Skipping cache cleanup due to permissions for %s", CACHE_DIR)
except OSError as exc:
logger.warning("Skipping cache cleanup due to OS error: %s", exc)
+25
View File
@@ -0,0 +1,25 @@
import os
from pathlib import Path
from agentic_security.cache_config import ensure_cache_dir
def test_ensure_cache_dir_creates_dir_and_sets_env(tmp_path, monkeypatch):
monkeypatch.delenv("DISK_CACHE_DIR", raising=False)
target_dir = tmp_path / "cache_to_disk"
resolved = ensure_cache_dir(target_dir)
assert resolved == target_dir
assert resolved.is_dir()
assert Path(os.environ["DISK_CACHE_DIR"]) == resolved
def test_ensure_cache_dir_respects_existing_env(tmp_path, monkeypatch):
env_dir = tmp_path / "preconfigured"
monkeypatch.setenv("DISK_CACHE_DIR", str(env_dir))
resolved = ensure_cache_dir()
assert resolved == env_dir
assert resolved.exists()
+1
View File
@@ -125,6 +125,7 @@ class TestLibraryLevel:
print(result)
assert len(result) in [0, 1]
@pytest.mark.skip
def test_image_modality(self):
llmSpec = test_spec_assets.IMAGE_SPEC
maxBudget = 2
+18 -1
View File
@@ -1,6 +1,10 @@
import pytest
from agentic_security.http_spec import LLMSpec, parse_http_spec
from agentic_security.http_spec import (
InvalidHTTPSpecError,
LLMSpec,
parse_http_spec,
)
class TestParseHttpSpec:
@@ -55,6 +59,19 @@ class TestParseHttpSpec:
assert result.headers == {"Content-Type": "application/json"}
assert result.body == ""
def test_parse_http_spec_rejects_malformed_header(self):
http_spec = "GET http://example.com\nHeaderWithoutColon\n\n"
with pytest.raises(InvalidHTTPSpecError, match="Invalid header line"):
parse_http_spec(http_spec)
def test_parse_http_spec_trims_header_whitespace(self):
http_spec = "GET http://example.com\nAuthorization:Bearer token\n\n"
result = parse_http_spec(http_spec)
assert result.headers == {"Authorization": "Bearer token"}
class TestLLMSpec:
def test_validate_raises_error_for_missing_files(self):