acupedia-ops/tests/local/test_workspace.py

102 lines
2.5 KiB
Python

from pathlib import Path
import pytest
from acupedia_ops.local.workspace import load_local_workspace
@pytest.fixture()
def workspace_environment(tmp_path: Path, monkeypatch):
base = tmp_path / "workspace"
base.mkdir()
names = {
"ACUPEDIA_API_PATH": "api",
"ACUPEDIA_MOBILE_PATH": "mobile",
"ACUPEDIA_WEB_PORTAL_PATH": "web",
"ACUPEDIA_ADMIN_PORTAL_PATH": "admin",
"ACUPEDIA_CONTENT_PATH": "content",
"ACUPEDIA_OPS_PATH": "ops",
}
monkeypatch.setenv("ACUPEDIA_WORKSPACE_BASE_PATH", str(base))
resolved = {}
for env_name, relative in names.items():
path = base / relative
path.mkdir()
monkeypatch.setenv(env_name, relative)
resolved[env_name] = path
(resolved["ACUPEDIA_API_PATH"] / "app").mkdir()
(resolved["ACUPEDIA_API_PATH"] / "pyproject.toml").write_text(
"",
encoding="utf-8",
)
(resolved["ACUPEDIA_CONTENT_PATH"] / "config.json").write_text(
"{}",
encoding="utf-8",
)
config = tmp_path / "local.yaml"
config.write_text(
"""
components:
api:
required_markers: [pyproject.toml, app]
setup_command: [uv, sync]
run_command: [uv, run, uvicorn]
mobile: {}
web: {}
admin: {}
infrastructure:
compose_file: docker-compose.local.yml
bootstrap:
content_config: config.json
modules_root: .
database:
reset_command: []
""".strip(),
encoding="utf-8",
)
return base, resolved, config
def test_workspace_loads_relative_paths_from_base(
workspace_environment,
) -> None:
base, resolved, config = workspace_environment
workspace = load_local_workspace(
env_file=Path("/nonexistent"),
config_file=config,
)
assert workspace.base_path == base.resolve()
assert workspace.path("api") == resolved["ACUPEDIA_API_PATH"].resolve()
assert workspace.path("mobile") == resolved["ACUPEDIA_MOBILE_PATH"].resolve()
assert workspace.bootstrap.content_config == (
resolved["ACUPEDIA_CONTENT_PATH"] / "config.json"
).resolve()
def test_compose_env_override_resolves_from_workspace_base(
workspace_environment,
monkeypatch,
) -> None:
base, _, config = workspace_environment
monkeypatch.setenv(
"ACUPEDIA_LOCAL_COMPOSE_FILE",
"ops/docker-compose.local.yml",
)
workspace = load_local_workspace(
env_file=Path("/nonexistent"),
config_file=config,
)
assert workspace.infra.compose_file == (
base / "ops" / "docker-compose.local.yml"
).resolve()