92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
"""Shared pytest fixtures for drone tests."""
|
|
|
|
import os
|
|
import tempfile
|
|
|
|
# Redirect prax logs to temp directory during tests
|
|
# Must be set before any prax imports to catch logger initialization
|
|
if "AIPASS_TEST_LOG_DIR" not in os.environ:
|
|
os.environ["AIPASS_TEST_LOG_DIR"] = tempfile.mkdtemp(prefix="aipass_test_logs_")
|
|
|
|
import json
|
|
import logging
|
|
import shutil
|
|
from pathlib import Path
|
|
from typing import Generator
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def temp_test_dir() -> Generator[Path, None, None]:
|
|
"""Creates temporary directory for testing, cleans up after."""
|
|
test_dir = Path(tempfile.mkdtemp())
|
|
yield test_dir
|
|
if test_dir.exists():
|
|
shutil.rmtree(test_dir)
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_registry(temp_test_dir: Path) -> Path:
|
|
"""Create a sample AIPASS_REGISTRY.json for testing."""
|
|
registry = {
|
|
"metadata": {"version": "1.0.0"},
|
|
"branches": [
|
|
{
|
|
"name": "TEST_BRANCH",
|
|
"path": str(temp_test_dir / "test_branch"),
|
|
"profile": "library",
|
|
"description": "Test branch",
|
|
"email": "@test_branch",
|
|
"status": "active",
|
|
}
|
|
],
|
|
}
|
|
registry_path = temp_test_dir / "AIPASS_REGISTRY.json"
|
|
registry_path.write_text(json.dumps(registry, indent=2))
|
|
return registry_path
|
|
|
|
|
|
@pytest.fixture()
|
|
def sample_data() -> dict:
|
|
"""Provide reusable sample data dict with required keys."""
|
|
return {
|
|
"created": "2026-01-01",
|
|
"last_updated": "2026-01-15",
|
|
"entries": [
|
|
{"id": 1, "name": "alpha", "status": "active"},
|
|
{"id": 2, "name": "beta", "status": "pending"},
|
|
],
|
|
"metadata": {
|
|
"source": "test_fixture",
|
|
"version": "1.0.0",
|
|
},
|
|
}
|
|
|
|
|
|
@pytest.fixture()
|
|
def mock_logger() -> MagicMock:
|
|
"""Standalone mock logger for testing logging calls."""
|
|
mock = MagicMock(spec=logging.Logger)
|
|
mock.debug = MagicMock()
|
|
mock.info = MagicMock()
|
|
mock.warning = MagicMock()
|
|
mock.error = MagicMock()
|
|
mock.critical = MagicMock()
|
|
return mock
|
|
|
|
|
|
@pytest.fixture()
|
|
def mock_json_handler() -> MagicMock:
|
|
"""Standalone mock json_handler for isolation tests."""
|
|
handler = MagicMock()
|
|
handler.load_json = MagicMock(return_value={})
|
|
handler.save_json = MagicMock(return_value=True)
|
|
handler.ensure_json_exists = MagicMock(return_value=True)
|
|
handler.ensure_module_jsons = MagicMock(return_value=True)
|
|
handler.get_json_path = MagicMock(return_value=Path("/tmp/mock.json"))
|
|
handler.validate_json_structure = MagicMock(return_value=True)
|
|
handler.log_operation = MagicMock(return_value=True)
|
|
return handler
|