fix: stabilize Polygon config loading and sync

This commit is contained in:
Repinoid
2026-10-25 10:35:26 +03:00
parent ee35088dae
commit 3df89ec9e7
135 changed files with 6994 additions and 2381 deletions
+42
View File
@@ -180,3 +180,45 @@ class TestFileRoundtrip:
assert len(loaded["cfsParams"]) == 2
finally:
os.unlink(fname)
class TestGenerationSync:
"""The CLI output directory should match its source after a clean run."""
def test_main_removes_yaml_missing_from_source(self, tmp_path, monkeypatch, dummy_yaml):
source_dir = tmp_path / "source"
output_dir = tmp_path / "output"
source_dir.mkdir()
output_dir.mkdir()
with open(source_dir / "1_dummy.yaml", "w", encoding="utf-8") as source_file:
yaml.safe_dump(dummy_yaml, source_file, allow_unicode=True)
(output_dir / "27_obsolete.yaml").write_text("old: true\n", encoding="utf-8")
(output_dir / "keep.txt").write_text("not YAML\n", encoding="utf-8")
monkeypatch.setattr(
sys,
"argv",
["from_stands.py", str(source_dir), str(output_dir)],
)
from_stands.main()
assert (output_dir / "1_dummy.yaml").is_file()
assert not (output_dir / "27_obsolete.yaml").exists()
assert (output_dir / "keep.txt").is_file()
def test_main_keeps_existing_yaml_when_conversion_fails(self, tmp_path, monkeypatch):
source_dir = tmp_path / "source"
output_dir = tmp_path / "output"
source_dir.mkdir()
output_dir.mkdir()
(source_dir / "broken.yaml").write_text("broken: [", encoding="utf-8")
(output_dir / "27_obsolete.yaml").write_text("old: true\n", encoding="utf-8")
monkeypatch.setattr(
sys,
"argv",
["from_stands.py", str(source_dir), str(output_dir)],
)
from_stands.main()
assert (output_dir / "27_obsolete.yaml").is_file()
+71
View File
@@ -0,0 +1,71 @@
"""Regression tests for startup configuration path resolution."""
import os
import subprocess
import sys
from pathlib import Path
import pytest
POLYGON_ROOT = Path(__file__).resolve().parents[1]
SITE_DIR = POLYGON_ROOT / "site"
@pytest.mark.parametrize("working_dir", [POLYGON_ROOT, SITE_DIR])
def test_loader_finds_stand_yaml_independent_of_working_directory(working_dir):
"""Exercise a fresh import from both documented launch directories.
A subprocess isolates module-level environment parsing and import-time YAML
loading, which is the exact behavior used when the WSGI process starts.
"""
env = os.environ.copy()
env["POLYGON_STANDS"] = (
"dev:services/dev,test:services/test,prod:services/prod"
)
python_path = str(SITE_DIR)
if env.get("PYTHONPATH"):
python_path += os.pathsep + env["PYTHONPATH"]
env["PYTHONPATH"] = python_path
subprocess.run(
[
sys.executable,
"-c",
(
"import app; "
"assert set(app._cfg.STANDS) == {'dev', 'test', 'prod'}; "
"assert app.app.test_client().get('/health').status_code == 200"
),
],
cwd=working_dir,
env=env,
check=True,
timeout=10,
)
def test_loader_finds_default_services_from_repository_root():
"""The no-env default must also work with the platform's root CWD."""
env = os.environ.copy()
env.pop("POLYGON_STANDS", None)
python_path = str(SITE_DIR)
if env.get("PYTHONPATH"):
python_path += os.pathsep + env["PYTHONPATH"]
env["PYTHONPATH"] = python_path
subprocess.run(
[
sys.executable,
"-c",
(
"import app; "
"assert set(app._cfg.STANDS) == {'default'}; "
"assert app.app.test_client().get('/health').status_code == 200"
),
],
cwd=POLYGON_ROOT,
env=env,
check=True,
timeout=10,
)