"""TST-6: tests for ``loosecanvas.config`` — ALLOWED_BASE_DIR resolution. ``config.py`` resolves ``ALLOWED_BASE_DIR`` at **import time** from ``LOOSECANVAS_ALLOWED_BASE``. These tests use ``monkeypatch.setenv`` / ``monkeypatch.delenv`` followed by ``importlib.reload`` so each test exercises the real resolution path without mutating the process-wide value seen by other modules. Strict teardown: the fixture restores both the env var and the module after each test so that downstream files (e.g. ``test_file_walker.py``) see the real boundary if they run in the same process. """ from __future__ import annotations import importlib from collections.abc import Iterator from pathlib import Path import loosecanvas.config as _config_module import pytest @pytest.fixture def _restore_config(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: """Reload ``loosecanvas.config`` after each test, restoring the original value.""" original_allowed_base = _config_module.ALLOWED_BASE_DIR yield # Reload without any patched env — restores the import-time value. importlib.reload(_config_module) # Belt-and-suspenders: put the pre-test value back on the module object in # case other already-imported modules hold a reference. _config_module.ALLOWED_BASE_DIR = original_allowed_base def test_env_set_uses_that_path( monkeypatch: pytest.MonkeyPatch, tmp_path: Path, _restore_config: None, ) -> None: """When LOOSECANVAS_ALLOWED_BASE is set, ALLOWED_BASE_DIR == Path(tmp).resolve().""" monkeypatch.setenv("LOOSECANVAS_ALLOWED_BASE", str(tmp_path)) importlib.reload(_config_module) assert tmp_path.resolve() == _config_module.ALLOWED_BASE_DIR def test_env_unset_uses_workspace_parent_default( monkeypatch: pytest.MonkeyPatch, _restore_config: None, ) -> None: """When LOOSECANVAS_ALLOWED_BASE is absent, ALLOWED_BASE_DIR defaults to _WORKSPACE_ROOT.parent.""" monkeypatch.delenv("LOOSECANVAS_ALLOWED_BASE", raising=False) importlib.reload(_config_module) expected = _config_module._WORKSPACE_ROOT.parent.resolve() assert expected == _config_module.ALLOWED_BASE_DIR def test_allowed_base_dir_is_absolute(_restore_config: None) -> None: """ALLOWED_BASE_DIR is always an absolute (resolved) path.""" assert _config_module.ALLOWED_BASE_DIR.is_absolute()