diff --git a/tests/test_tsn_ref.py b/tests/test_tsn_ref.py index 3dd941b..b54793a 100644 --- a/tests/test_tsn_ref.py +++ b/tests/test_tsn_ref.py @@ -239,3 +239,16 @@ def test_apply_refuses_build_that_failed_its_own_audit(tmp_path, monkeypatch): got = build_mod.apply(staging, target) assert got["ok"] is False and "аудит" in " ".join(got["problems"]).lower() assert target.read_bytes() == b"old db content" + + +def test_tsn_ref_imports_without_ext_state_when_path_given(tmp_path): + """В контейнере агента ext_state.py нет: модуль обязан импортироваться, когда путь + к базе задан переменной TSN_REF_PATH, а не выводится через ext_state.""" + import shutil + import subprocess + shutil.copy(os.path.join(ROOT, "tsn_ref.py"), tmp_path / "tsn_ref.py") + env = dict(os.environ, TSN_REF_PATH=str(tmp_path / "x.sqlite"), PYTHONPATH=str(tmp_path)) + r = subprocess.run([sys.executable, "-c", "import tsn_ref; print(tsn_ref.db_path())"], + cwd=tmp_path, env=env, capture_output=True, text=True) + assert r.returncode == 0, r.stderr + assert str(tmp_path / "x.sqlite") in r.stdout diff --git a/tsn_ref.py b/tsn_ref.py index 99b7773..7b13ee5 100644 --- a/tsn_ref.py +++ b/tsn_ref.py @@ -15,15 +15,17 @@ import os import sqlite3 from pathlib import Path -import ext_state SCHEMA_VERSION = 2 # 2 — добавлены цены ресурсов .1/.2 (price_items) def db_path() -> Path: + """Путь к базе. В контейнере агента ext_state.py нет, поэтому модуль тянет его + только когда путь не задан переменной.""" override = os.environ.get("TSN_REF_PATH") if override: return Path(override) + import ext_state return ext_state.ext_dir() / "derived" / "tsn" / "tsn_ref.sqlite"