From aa2b5d3fcda887f69eb81724187e9ff5904067bd Mon Sep 17 00:00:00 2001 From: John Poole Date: Mon, 18 May 2026 15:48:20 -0700 Subject: [PATCH] Test issue fix, had problems on zerodev1 which ChatGPT helped me finesse. This is the fix Codex was instructed to make --- .../tests/test_fragmentation_backend_shim.py | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/migration/tests/test_fragmentation_backend_shim.py b/migration/tests/test_fragmentation_backend_shim.py index f5b2ea1..9497adb 100644 --- a/migration/tests/test_fragmentation_backend_shim.py +++ b/migration/tests/test_fragmentation_backend_shim.py @@ -62,14 +62,35 @@ print(json.dumps({ """ +def _pythonpath_entry_path(entry): + if not entry: + # Empty PYTHONPATH entries mean the subprocess cwd. + return os.path.realpath(REPO_ROOT) + if os.path.isabs(entry): + return os.path.realpath(entry) + return os.path.realpath(os.path.join(REPO_ROOT, entry)) + + +def _is_cpp_backend_path(entry): + path = _pythonpath_entry_path(entry) + cpp_dir = os.path.realpath(CPP_BUILD_DIR) + return path == cpp_dir or path.startswith(cpp_dir + os.sep) + + +def _parent_pythonpath_entries(include_cpp): + entries = os.environ.get("PYTHONPATH", "").split(os.pathsep) + if include_cpp: + return [entry for entry in entries if entry] + return [entry for entry in entries if entry and not _is_cpp_backend_path(entry)] + + def run_probe(backend, include_cpp): env = os.environ.copy() env["BLE_RETICULUM_FRAGMENTATION_BACKEND"] = backend pythonpath = [SRC_DIR] if include_cpp: pythonpath.append(CPP_BUILD_DIR) - if env.get("PYTHONPATH"): - pythonpath.append(env["PYTHONPATH"]) + pythonpath.extend(_parent_pythonpath_entries(include_cpp)) env["PYTHONPATH"] = os.pathsep.join(pythonpath) completed = subprocess.run( @@ -105,6 +126,22 @@ def test_python_backend_still_works_when_cpp_backend_is_unavailable(): assert result["deframed"] == "616263" +def test_include_cpp_false_filters_parent_pythonpath(monkeypatch): + parent_pythonpath = os.pathsep.join( + [ + "migration/protocol_core", + SRC_DIR, + os.path.join(CPP_BUILD_DIR, "build", "lib.fake-platform"), + ] + ) + monkeypatch.setenv("PYTHONPATH", parent_pythonpath) + + result = run_probe("auto", include_cpp=False) + + assert result["backend"] == "python" + assert result["fragmenter_module"] == "ble_reticulum.BLEFragmentation" + + def test_auto_backend_prefers_cpp_when_available(): result = run_probe("auto", include_cpp=True)