test: isolate VIGILAR_CONFIG via autouse session fixture (fixes #3) #5

Merged
alee merged 1 commits from fix/issue-3-test-isolation into main 2026-04-05 16:54:12 +00:00
Owner

Fixes #3.

Problem

Running the full pytest suite from a clean checkout rewrites the committed config/vigilar.toml via a Pydantic model_dump() round-trip (comments stripped, cameras/sensors/rules dropped, default fields materialized).

Root cause

vigilar/web/blueprints/system.py:24_get_config_path() defaults to the relative path "config/vigilar.toml" when VIGILAR_CONFIG is unset. test_reset_pin_correct_passphrase in tests/unit/test_system_pin.py exercises POST /system/api/reset-pin, whose handler calls _save_and_reload()save_config(new_cfg, _get_config_path()). With no env var, the real on-disk config is rewritten.

Reproduction:

git checkout -- config/vigilar.toml
pytest -q tests/unit/test_system_pin.py::test_reset_pin_correct_passphrase
git status --short  #  M config/vigilar.toml

Fix

Add an autouse session-scoped fixture in tests/conftest.py that points VIGILAR_CONFIG at a path inside pytest's session tmp dir. Every test inherits the safe default; no test can touch the real file. Previous env var value is restored on teardown.

Verification

  • pytest -q tests/unit/test_system_pin.py::test_reset_pin_correct_passphrase — passes, config/vigilar.toml unchanged.
  • pytest -q — 360/360 passing, config/vigilar.toml unchanged.
Fixes #3. ## Problem Running the full `pytest` suite from a clean checkout rewrites the committed `config/vigilar.toml` via a Pydantic `model_dump()` round-trip (comments stripped, cameras/sensors/rules dropped, default fields materialized). ## Root cause `vigilar/web/blueprints/system.py:24` — `_get_config_path()` defaults to the relative path `"config/vigilar.toml"` when `VIGILAR_CONFIG` is unset. `test_reset_pin_correct_passphrase` in `tests/unit/test_system_pin.py` exercises `POST /system/api/reset-pin`, whose handler calls `_save_and_reload()` → `save_config(new_cfg, _get_config_path())`. With no env var, the real on-disk config is rewritten. Reproduction: ``` git checkout -- config/vigilar.toml pytest -q tests/unit/test_system_pin.py::test_reset_pin_correct_passphrase git status --short # M config/vigilar.toml ``` ## Fix Add an autouse session-scoped fixture in `tests/conftest.py` that points `VIGILAR_CONFIG` at a path inside pytest's session tmp dir. Every test inherits the safe default; no test can touch the real file. Previous env var value is restored on teardown. ## Verification - `pytest -q tests/unit/test_system_pin.py::test_reset_pin_correct_passphrase` — passes, `config/vigilar.toml` unchanged. - `pytest -q` — 360/360 passing, `config/vigilar.toml` unchanged.
alee added 1 commit 2026-04-05 14:55:58 +00:00
Some web endpoint handlers call _save_and_reload(), which resolves the
target path via VIGILAR_CONFIG env var with a fallback to the relative
"config/vigilar.toml". Any test exercising such an endpoint without
setting the env var rewrites the repo's committed config file via a
Pydantic model_dump round-trip, stripping comments and non-default
fields. The culprit discovered was test_reset_pin_correct_passphrase
in tests/unit/test_system_pin.py.

Add an autouse session-scoped fixture in tests/conftest.py that points
VIGILAR_CONFIG at a path inside pytest's session tmp dir so no test
can touch the real file. Restore the previous env var value on teardown.

Fixes #3.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
alee merged commit 9f959f8c78 into main 2026-04-05 16:54:12 +00:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: alee/vigilar#5