2026-04-28 06:43:53 -07:00
|
|
|
"""Regression for #16746: install.sh /dev/tty gates must actually open /dev/tty.
|
2026-04-27 18:12:41 -07:00
|
|
|
|
2026-04-27 18:18:00 -07:00
|
|
|
In a Docker build, ``/dev/tty`` exists as a device node (so a bare ``-e``
|
|
|
|
|
existence test returns true) but opening it fails with ``ENXIO: No such
|
2026-04-28 06:43:53 -07:00
|
|
|
device or address``. Under the old gates the script proceeded past the "no
|
2026-04-27 18:18:00 -07:00
|
|
|
terminal available" skip and then crashed on the ``< /dev/tty`` redirect a
|
2026-04-28 06:43:53 -07:00
|
|
|
few lines later, aborting the entire image build. The fix replaces every
|
|
|
|
|
existence-based check that guards a subsequent ``< /dev/tty`` redirect with
|
|
|
|
|
an open-based probe so the skip kicks in correctly.
|
|
|
|
|
|
|
|
|
|
This module covers all three affected functions: ``run_setup_wizard()``
|
|
|
|
|
(the reproducer in #16746), ``install_system_packages()`` (the apt sudo
|
|
|
|
|
prompt fallback), and ``maybe_start_gateway()`` (the gateway-install gate).
|
2026-04-27 18:12:41 -07:00
|
|
|
"""
|
|
|
|
|
|
2026-04-27 18:18:00 -07:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import re
|
2026-04-27 18:12:41 -07:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
2026-04-28 06:43:53 -07:00
|
|
|
import pytest
|
|
|
|
|
|
2026-04-27 18:12:41 -07:00
|
|
|
REPO_ROOT = Path(__file__).resolve().parent.parent
|
|
|
|
|
INSTALL_SH = REPO_ROOT / "scripts" / "install.sh"
|
|
|
|
|
|
2026-04-28 06:43:53 -07:00
|
|
|
# Every function in scripts/install.sh that previously gated on a bare
|
|
|
|
|
# ``[ -e /dev/tty ]`` check before redirecting stdin from ``/dev/tty``.
|
|
|
|
|
GATED_FUNCTIONS = ("run_setup_wizard", "install_system_packages", "maybe_start_gateway")
|
2026-04-27 18:12:41 -07:00
|
|
|
|
2026-04-27 18:18:00 -07:00
|
|
|
|
2026-04-28 06:43:53 -07:00
|
|
|
def _extract_function_body(name: str) -> str:
|
|
|
|
|
"""Return the body of ``<name>()`` as a single string.
|
|
|
|
|
|
|
|
|
|
Anchored to ``<name>()`` and a top-of-line ``}`` so the helper keeps
|
|
|
|
|
working if neighbouring functions are renamed.
|
2026-04-27 18:18:00 -07:00
|
|
|
"""
|
2026-04-27 18:12:41 -07:00
|
|
|
text = INSTALL_SH.read_text()
|
2026-04-27 18:18:00 -07:00
|
|
|
match = re.search(
|
2026-04-28 06:43:53 -07:00
|
|
|
rf"^{re.escape(name)}\(\)\s*\{{\s*\n(?P<body>.*?)^\}}",
|
2026-04-27 18:18:00 -07:00
|
|
|
text,
|
|
|
|
|
re.MULTILINE | re.DOTALL,
|
|
|
|
|
)
|
2026-04-28 06:43:53 -07:00
|
|
|
assert match is not None, f"{name}() not found in scripts/install.sh"
|
2026-04-27 18:18:00 -07:00
|
|
|
return match["body"]
|
2026-04-27 18:12:41 -07:00
|
|
|
|
|
|
|
|
|
2026-04-28 06:43:53 -07:00
|
|
|
@pytest.mark.parametrize("fn_name", GATED_FUNCTIONS)
|
|
|
|
|
def test_tty_gate_does_not_use_existence_only_check(fn_name: str) -> None:
|
2026-04-27 18:18:00 -07:00
|
|
|
"""The bare ``-e`` test is the bug — no spelling of it should remain."""
|
2026-04-28 06:43:53 -07:00
|
|
|
body = _extract_function_body(fn_name)
|
2026-04-27 18:18:00 -07:00
|
|
|
# Cover ``[ -e /dev/tty ]``, ``[ -e "/dev/tty" ]``, ``test -e /dev/tty``
|
|
|
|
|
# and friends, with arbitrary surrounding whitespace.
|
|
|
|
|
pattern = re.compile(
|
|
|
|
|
r"""(
|
|
|
|
|
\[\s*-e\s+["']?/dev/tty["']?\s*\]
|
|
|
|
|
|
|
|
|
|
|
\btest\s+-e\s+["']?/dev/tty["']?
|
|
|
|
|
)""",
|
|
|
|
|
re.VERBOSE,
|
2026-04-27 18:12:41 -07:00
|
|
|
)
|
2026-04-27 18:18:00 -07:00
|
|
|
match = pattern.search(body)
|
|
|
|
|
assert match is None, (
|
2026-04-28 06:43:53 -07:00
|
|
|
f"{fn_name} contains an existence-only check on /dev/tty "
|
2026-04-27 18:18:00 -07:00
|
|
|
f"({match.group(0)!r}). Bare `-e` tests pass in Docker builds "
|
|
|
|
|
"where the device node is in the mount namespace but cannot be "
|
|
|
|
|
"opened (ENXIO). Use an open-based probe (e.g. "
|
|
|
|
|
"`(: </dev/tty) 2>/dev/null` or `exec 3</dev/tty`) so the skip "
|
2026-04-28 06:43:53 -07:00
|
|
|
"kicks in before the function tries to read from /dev/tty. "
|
2026-04-27 18:18:00 -07:00
|
|
|
"See #16746."
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-27 18:12:41 -07:00
|
|
|
|
2026-04-28 06:43:53 -07:00
|
|
|
@pytest.mark.parametrize("fn_name", GATED_FUNCTIONS)
|
|
|
|
|
def test_tty_gate_uses_open_based_probe(fn_name: str) -> None:
|
2026-04-27 18:18:00 -07:00
|
|
|
"""The gate must actually attempt to open ``/dev/tty``.
|
2026-04-27 18:12:41 -07:00
|
|
|
|
2026-04-28 06:43:53 -07:00
|
|
|
Any ``if``/``if !``/``elif`` whose condition opens ``/dev/tty`` for
|
|
|
|
|
input counts: ``(: </dev/tty)``, ``exec 3</dev/tty``,
|
|
|
|
|
``{ exec 3</dev/tty; }``, etc. Asserting the higher-level invariant
|
|
|
|
|
rather than a specific spelling so equivalent refactors stay green.
|
2026-04-27 18:18:00 -07:00
|
|
|
"""
|
2026-04-28 06:43:53 -07:00
|
|
|
body = _extract_function_body(fn_name)
|
|
|
|
|
gate = re.compile(
|
|
|
|
|
r"^\s*(?:if|elif)\s+!?\s*[^\n]*<\s*/dev/tty[^\n]*;\s*then",
|
|
|
|
|
re.MULTILINE,
|
|
|
|
|
)
|
2026-04-27 18:18:00 -07:00
|
|
|
assert gate.search(body), (
|
2026-04-28 06:43:53 -07:00
|
|
|
f"{fn_name} must gate on an open-based probe of /dev/tty "
|
|
|
|
|
"(an `if`/`if !`/`elif` whose test redirects stdin from /dev/tty), "
|
|
|
|
|
"not a mere existence check. See #16746."
|
2026-04-27 18:12:41 -07:00
|
|
|
)
|