Skip to content

Commit 05e874b

Browse files
committed
normalize validator worker path comparisons
1 parent f63c809 commit 05e874b

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

scripts/validate_plugins/run.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ def build_worker_sys_path(*, astrbot_root: Path, astrbot_path: Path) -> list[str
282282
return [str(astrbot_root.resolve()), str(astrbot_path.resolve())]
283283

284284

285+
def normalize_path_for_comparison(path: str | os.PathLike[str]) -> str:
286+
path_str = os.fspath(path)
287+
return os.path.normcase(os.path.realpath(os.path.abspath(os.path.expanduser(path_str))))
288+
289+
285290
def configure_worker_install_target(*, temp_root: Path) -> Path:
286291
"""Configure process-global install/import state for a validator worker.
287292
@@ -292,16 +297,19 @@ def configure_worker_install_target(*, temp_root: Path) -> Path:
292297
site_packages = (temp_root / "site-packages").resolve()
293298
site_packages.mkdir(parents=True, exist_ok=True)
294299
site_packages_str = str(site_packages)
300+
site_packages_key = normalize_path_for_comparison(site_packages_str)
295301

296302
os.environ["PIP_TARGET"] = site_packages_str
297303
existing_pythonpath = [
298304
entry
299305
for entry in os.environ.get("PYTHONPATH", "").split(os.pathsep)
300-
if entry and entry != site_packages_str
306+
if entry and normalize_path_for_comparison(entry) != site_packages_key
301307
]
302308
os.environ["PYTHONPATH"] = os.pathsep.join([site_packages_str, *existing_pythonpath])
303309

304-
sys.path[:] = [entry for entry in sys.path if entry != site_packages_str]
310+
sys.path[:] = [
311+
entry for entry in sys.path if normalize_path_for_comparison(entry) != site_packages_key
312+
]
305313
sys.path.insert(0, site_packages_str)
306314
return site_packages
307315

tests/test_validate_plugins.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -781,26 +781,34 @@ def test_configure_worker_install_target_deduplicates_process_paths(self):
781781
with tempfile.TemporaryDirectory() as tmp_dir:
782782
temp_root = Path(tmp_dir)
783783
site_packages = (temp_root / "site-packages").resolve()
784+
site_packages_alias = os.path.join(str(site_packages.parent), ".", site_packages.name)
784785
extra_path = (temp_root / "extra-path").resolve()
785786
extra_path.mkdir()
786787
observed = {}
787788

788-
sys.path[:0] = [str(site_packages), str(site_packages)]
789+
sys.path[:0] = [str(site_packages), site_packages_alias]
789790
with mock.patch.dict(
790791
os.environ,
791-
{"PYTHONPATH": os.pathsep.join([str(site_packages), str(extra_path)])},
792+
{"PYTHONPATH": os.pathsep.join([site_packages_alias, str(extra_path)])},
792793
clear=True,
793794
):
794795
module.configure_worker_install_target(temp_root=temp_root)
795796
module.configure_worker_install_target(temp_root=temp_root)
796797

797798
observed["pip_target"] = os.environ["PIP_TARGET"]
798799
observed["pythonpath_entries"] = os.environ["PYTHONPATH"].split(os.pathsep)
799-
observed["sys_path_count"] = sys.path.count(str(site_packages))
800+
observed["resolved_pythonpath_count"] = sum(
801+
1
802+
for entry in observed["pythonpath_entries"]
803+
if Path(entry).resolve() == site_packages
804+
)
805+
observed["resolved_sys_path_count"] = sum(
806+
1 for entry in sys.path if Path(entry).resolve() == site_packages
807+
)
800808

801809
self.assertEqual(observed["pip_target"], str(site_packages))
802-
self.assertEqual(observed["pythonpath_entries"].count(str(site_packages)), 1)
803-
self.assertEqual(observed["sys_path_count"], 1)
810+
self.assertEqual(observed["resolved_pythonpath_count"], 1)
811+
self.assertEqual(observed["resolved_sys_path_count"], 1)
804812
self.assertIn(str(extra_path), observed["pythonpath_entries"])
805813

806814
sys.path[:] = original_sys_path

0 commit comments

Comments
 (0)