Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-driver): keep the uninstaller parseable by bash 3.2

umutcagand25c6cb16eb9a
diff --git a/libs/cua-driver/scripts/tests/test_installer_bash32_syntax.py b/libs/cua-driver/scripts/tests/test_installer_bash32_syntax.py
new file mode 100644
index 0000000000..992c5c5333
--- /dev/null
+++ b/libs/cua-driver/scripts/tests/test_installer_bash32_syntax.py
@@ -0,0 +1,77 @@
+"""Guard the installer scripts against the bash 3.2 parser on macOS.
+
+macOS ships bash 3.2 as ``/bin/bash``. While scanning for the closing paren of
+a command substitution, that parser tracks single quotes even inside a quoted
+heredoc body, so a lone apostrophe in an embedded Python comment makes the
+whole script fail to parse with ``unexpected EOF while looking for matching
+')'``. Newer bash parses the same file cleanly, so ``bash -n`` on a Linux CI
+runner does not catch it.
+
+These tests therefore pair a syntax check against the system bash (bash 3.2 on
+macOS) with a portable structural check that holds on any host: apostrophes in
+a heredoc body must be balanced.
+"""
+
+from __future__ import annotations
+
+import re
+import subprocess
+from pathlib import Path
+
+import pytest
+
+
+REPO_ROOT = Path(__file__).resolve().parents[4]
+SCRIPTS = sorted((REPO_ROOT / "libs/cua-driver/scripts").glob("*.sh"))
+
+HEREDOC_START = re.compile(
+ r"<<-?\s*(?:'([A-Za-z_][A-Za-z0-9_]*)'"
+ r"|\"([A-Za-z_][A-Za-z0-9_]*)\""
+ r"|([A-Za-z_][A-Za-z0-9_]*))"
+)
+
+
+def _heredoc_bodies(text: str) -> list[tuple[int, str, str]]:
+ """Yield ``(line number, delimiter, body)`` for each heredoc in ``text``."""
+ lines = text.splitlines()
+ found: list[tuple[int, str, str]] = []
+ index = 0
+ while index < len(lines):
+ line = lines[index]
+ match = None if line.lstrip().startswith("#") else HEREDOC_START.search(line)
+ if match:
+ delimiter = match.group(1) or match.group(2) or match.group(3)
+ body: list[str] = []
+ cursor = index + 1
+ while cursor < len(lines) and lines[cursor].strip() != delimiter:
+ body.append(lines[cursor])
+ cursor += 1
+ found.append((index + 1, delimiter, "\n".join(body)))
+ index = cursor
+ index += 1
+ return found
+
+
[email protected]("script", SCRIPTS, ids=lambda path: path.name)
+def test_installer_scripts_parse_with_the_system_bash(script: Path) -> None:
+ completed = subprocess.run(
+ ["/bin/bash", "-n", str(script)],
+ capture_output=True,
+ text=True,
+ check=False,
+ )
+ assert completed.returncode == 0, completed.stderr
+
+
[email protected]("script", SCRIPTS, ids=lambda path: path.name)
+def test_heredoc_apostrophes_are_balanced_for_bash32(script: Path) -> None:
+ unbalanced = [
+ (line, delimiter)
+ for line, delimiter, body in _heredoc_bodies(script.read_text(encoding="utf-8"))
+ if body.count("'") % 2
+ ]
+ assert not unbalanced, (
+ f"{script.name}: heredoc(s) with an unbalanced apostrophe "
+ f"{unbalanced}; bash 3.2 cannot parse these inside a command "
+ "substitution. Reword the text to avoid the apostrophe."
+ )
diff --git a/libs/cua-driver/scripts/uninstall.sh b/libs/cua-driver/scripts/uninstall.sh
index 220b8bd95d..74585aea7e 100755
--- a/libs/cua-driver/scripts/uninstall.sh
+++ b/libs/cua-driver/scripts/uninstall.sh
@@ -647,10 +647,14 @@ def normalize(value):
"""Absolute path for a registered command, or None when it is relative.
Claude runs a registration from its own working directory, which is not
- the uninstaller's. Resolving a relative command here would attribute an
- unrelated project's ./cua-driver to this release whenever the uninstaller
- happens to run from the release package tree, so a command that is still
- relative after home expansion is never ownership evidence.
+ the one this uninstaller runs in. Resolving a relative command here would
+ attribute a ./cua-driver in an unrelated project to this release whenever
+ the uninstaller happens to run from the release package tree, so a command
+ that is still relative after home expansion is never ownership evidence.
+
+ Apostrophes are avoided in this heredoc on purpose: bash 3.2, the system
+ bash on macOS, tracks quotes while scanning for the closing paren of the
+ surrounding command substitution and fails to parse the script otherwise.
"""
expanded = os.path.expanduser(value)
if not os.path.isabs(expanded):
@@ -705,7 +709,7 @@ def release_owned_command(server):
# The canonical launcher is the only safe path-only fallback, and only
# when no filesystem entry exists at that path. A symlink, including a
- # dangling one, still identifies another installation's chosen target.
+ # dangling one, still identifies the chosen target of another installation.
return command_path == canonical_launcher and not os.path.lexists(command_path)