66 lines
2.5 KiB
Python
66 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""util.runtime_paths:技能根、DB 路径、数据目录均落在隔离数据根下。"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import unittest
|
||
|
||
from _support import IsolatedDataRoot, get_skill_root
|
||
|
||
_FAKE_CLAW_ROOT = r"D:\jiangchang-data"
|
||
|
||
|
||
class TestRuntimePaths(unittest.TestCase):
|
||
def test_paths_under_isolated_root(self) -> None:
|
||
win_default = r"D:\jiangchang-data"
|
||
with IsolatedDataRoot() as tmp:
|
||
import importlib
|
||
|
||
import util.runtime_paths as rp
|
||
|
||
importlib.reload(rp)
|
||
|
||
skill_root = rp.get_skill_root()
|
||
self.assertTrue(os.path.isdir(skill_root))
|
||
self.assertTrue(os.path.isfile(os.path.join(skill_root, "SKILL.md")))
|
||
|
||
db_path = rp.get_db_path()
|
||
self.assertTrue(os.path.normpath(db_path).startswith(os.path.normpath(tmp)))
|
||
self.assertNotIn(win_default, db_path)
|
||
|
||
data_dir = rp.get_skill_data_dir()
|
||
self.assertTrue(os.path.normpath(data_dir).startswith(os.path.normpath(tmp)))
|
||
self.assertIn("_test", data_dir.replace("\\", "/"))
|
||
self.assertNotIn(win_default, data_dir)
|
||
|
||
self.assertEqual(get_skill_root(), skill_root)
|
||
|
||
def test_isolation_overrides_prefixed_claw_data_root(self) -> None:
|
||
"""先设假的 CLAW_DATA_ROOT,再进入隔离;get_db_path 必须落在 tempdir(防漏设 CLAW_*)。"""
|
||
saved = {k: os.environ.get(k) for k in ("CLAW_DATA_ROOT", "JIANGCHANG_DATA_ROOT", "CLAW_USER_ID", "JIANGCHANG_USER_ID")}
|
||
try:
|
||
os.environ["CLAW_DATA_ROOT"] = _FAKE_CLAW_ROOT
|
||
os.environ["JIANGCHANG_DATA_ROOT"] = _FAKE_CLAW_ROOT
|
||
with IsolatedDataRoot() as tmp:
|
||
import importlib
|
||
|
||
import util.runtime_paths as rp
|
||
|
||
importlib.reload(rp)
|
||
db_path = rp.get_db_path()
|
||
norm_db = os.path.normcase(os.path.normpath(db_path))
|
||
norm_tmp = os.path.normcase(os.path.normpath(tmp))
|
||
self.assertTrue(norm_db.startswith(norm_tmp), msg=f"db_path={db_path!r} tmp={tmp!r}")
|
||
norm_fake = os.path.normcase(os.path.normpath(_FAKE_CLAW_ROOT))
|
||
self.assertFalse(norm_db.startswith(norm_fake), msg=f"db_path must not be under fake CLAW root: {db_path!r}")
|
||
finally:
|
||
for k, v in saved.items():
|
||
if v is None:
|
||
os.environ.pop(k, None)
|
||
else:
|
||
os.environ[k] = v
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|