From 963d22cde6816eeed3d931211810969b175aa78b Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Thu, 28 May 2026 02:24:03 +0530 Subject: [PATCH] test(install): harden uv-python-path regression test against future drift MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Self-review follow-ups on the salvage of #22494: W2 — Added encoding="utf-8" to read_text() calls. scripts/install.sh contains 48 em-dash ("—") characters and ~1500 non-ASCII bytes total; on Windows with cp1252 default locale, bare read_text() would raise UnicodeDecodeError. Project-wide cleanup of the other 11 similar sites across 5 install_sh test files is deferred to a separate follow-up. W3 — Bound the branch-containment check by the function body (head "resolve_install_layout() {" / tail "\n}\n") instead of by "next `return 0` after the marker". scripts/install.sh has 5 additional `return 0` statements between resolve_install_layout's first one and EOF; if a future maintainer hoists the export above another conditional with its own early-return or inserts an early-return between the marker and the export, the old assertion still passes while the export is unreachable. The body-bounded slice makes that class of regression visible. Also added more specific assertion messages and a guard for the body extraction to fail loudly if the function signature ever changes. --- ...test_install_sh_root_fhs_uv_python_path.py | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/tests/test_install_sh_root_fhs_uv_python_path.py b/tests/test_install_sh_root_fhs_uv_python_path.py index de7d337a952..0f1c5fa725a 100644 --- a/tests/test_install_sh_root_fhs_uv_python_path.py +++ b/tests/test_install_sh_root_fhs_uv_python_path.py @@ -14,8 +14,26 @@ REPO_ROOT = Path(__file__).resolve().parent.parent INSTALL_SH = REPO_ROOT / "scripts" / "install.sh" +def _resolve_install_layout_body() -> str: + """Return just the body of resolve_install_layout(), bounded by its + opening signature and the next top-level ``}`` close brace. + + Using the function body (not "first ``return 0`` after a marker") guards + the tests below against future refactors that hoist the export above + another conditional with its own early-return, or that insert an early- + return between the marker and the export — both of which would leave the + export unreachable while a less-strict assertion still passed. + """ + text = INSTALL_SH.read_text(encoding="utf-8") + head, _, rest = text.partition("resolve_install_layout() {\n") + assert rest, "Could not find resolve_install_layout() in scripts/install.sh" + body, _, _ = rest.partition("\n}\n") + assert body, "Could not find resolve_install_layout() closing brace" + return body + + def test_root_fhs_layout_exports_world_readable_uv_python_dirs() -> None: - text = INSTALL_SH.read_text() + text = INSTALL_SH.read_text(encoding="utf-8") assert 'export UV_PYTHON_INSTALL_DIR="${UV_PYTHON_INSTALL_DIR:-/usr/local/share/uv/python}"' in text assert 'export UV_PYTHON_BIN_DIR="${UV_PYTHON_BIN_DIR:-/usr/local/share/uv/bin}"' in text @@ -23,13 +41,19 @@ def test_root_fhs_layout_exports_world_readable_uv_python_dirs() -> None: def test_root_fhs_uv_python_export_is_inside_root_branch() -> None: """The export must live in the root-FHS branch of resolve_install_layout, - above its `return 0`, so non-root and Termux installs are unaffected.""" - text = INSTALL_SH.read_text() + after ``ROOT_FHS_LAYOUT=true`` and before the branch's ``return 0``, so + non-root and Termux installs are unaffected. Bound the slice by the + function body (not "next return 0" in the whole file) so the assertion + can't accept an unreachable export.""" + body = _resolve_install_layout_body() marker = 'ROOT_FHS_LAYOUT=true' - assert marker in text - after_marker = text.split(marker, 1)[1] + assert marker in body + after_marker = body.split(marker, 1)[1] return_idx = after_marker.find('return 0') export_idx = after_marker.find('UV_PYTHON_INSTALL_DIR') - assert export_idx != -1 - assert export_idx < return_idx + assert export_idx != -1, "UV_PYTHON_INSTALL_DIR export missing from root-FHS branch" + assert return_idx != -1, "root-FHS branch must end with `return 0`" + assert export_idx < return_idx, ( + "Export must precede the branch's `return 0` — otherwise unreachable" + )