From 5b7e7bea2f85767efece5fed6240144c6720d1d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=99=BD=E5=8F=94?= <11794527+absfan@user.noreply.gitee.com> Date: Tue, 17 Nov 2026 00:39:29 +0800 Subject: [PATCH] test: keep subprocess imports on the active worktree --- tests/conftest.py | 10 ++++++++++ tests/test_management_probe_guards.py | 3 ++- tests/test_worktree_imports.py | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/conftest.py create mode 100644 tests/test_worktree_imports.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..d7f734f --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,10 @@ +"""Keep subprocess imports on the worktree being tested.""" +import os +from pathlib import Path + + +def pytest_configure(): + source = str(Path(__file__).resolve().parents[1] / 'src') + existing = os.environ.get('PYTHONPATH', '').split(os.pathsep) + os.environ['PYTHONPATH'] = os.pathsep.join( + [source, *(entry for entry in existing if entry and entry != source)]) diff --git a/tests/test_management_probe_guards.py b/tests/test_management_probe_guards.py index f5a6a35..340e914 100644 --- a/tests/test_management_probe_guards.py +++ b/tests/test_management_probe_guards.py @@ -147,7 +147,8 @@ def test_mapping_context_is_serialized_and_restored_before_catalog(tmp_path, mon order.append(('mapping', drive, remote))) monkeypatch.setattr(native, '_catalog_native', lambda job, root: (order.append(('catalog', job['library'])) or {'success': True, 'safe_to_continue': True})) - assert native.management_worker_main(tmp_path/'catalog/request.json') == 0 + assert captured[0]['worker_role'] == 'catalog_readonly' + assert native.catalog_worker_main(tmp_path/'catalog/request.json') == 0 assert order == [('mapping', 'S:', r'\\server\share'), ('catalog', str(library))] diff --git a/tests/test_worktree_imports.py b/tests/test_worktree_imports.py new file mode 100644 index 0000000..115fded --- /dev/null +++ b/tests/test_worktree_imports.py @@ -0,0 +1,14 @@ +"""A spawned Python must test the same worktree as its pytest parent.""" +import json +from pathlib import Path +import subprocess +import sys + + +def test_spawned_python_imports_active_worktree_from_another_directory(tmp_path): + import cbb_refresh_verify + child = subprocess.run( + [sys.executable, '-c', 'import json,cbb_refresh_verify; ' + 'print(json.dumps(cbb_refresh_verify.__file__))'], + cwd=tmp_path, capture_output=True, text=True, check=True, timeout=10) + assert Path(json.loads(child.stdout)).resolve() == Path(cbb_refresh_verify.__file__).resolve()