Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/fast-gate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ jobs:
# origin` against the same remote, through the shared diff-base resolver
# script.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
# Full history so resolve_base() can merge-base(COMMENT_HISTORY_BASE_REF,
# HEAD). With the default depth-1 checkout the resolver still fetches
# the base SHA, but it arrives as an isolated tip and merge-base falls
# back to measuring against the base TIP instead of the divergence point.
fetch-depth: 0
- uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
with:
python-version: "3.12"
Expand Down
44 changes: 28 additions & 16 deletions scripts/smoke-linux-packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ RPM_NAME="$(resolve_one rpm)"
ABS_DIST="$(cd "$DIST_DIR" && pwd)"

# Shared assertions, run inside the container after the package manager has
# installed the package. `$1` is the command that lists the package's own files
# (`dpkg -L <pkg>` / `rpm -ql <pkg>`).
# installed the package. The list-files command is the only caller-controlled
# fragment (dpkg -L "$pkg" / rpm -ql "$pkg"); everything else is fixed.
#
# EVERY identity is DERIVED from that list rather than written here, because the
# nightly channel deliberately ships different ones so it can sit beside stable:
Expand All @@ -68,36 +68,48 @@ ABS_DIST="$(cd "$DIST_DIR" && pwd)"
# that would silently skip nightly's entire Linux publication. What is asserted
# is the INVARIANT, not one channel's names.
common_asserts() {
cat <<ASSERTS
files=\$($1)
entry=\$(printf '%s\\n' "\$files" | grep -E '^/usr/share/applications/[^/]+[.]desktop\$' | head -1)
stamp=\$(printf '%s\\n' "\$files" | grep -E '/kiro_crew/_build_info[.]py\$' | head -1)
test -n "\$entry" && test -f "\$entry"
test -n "\$stamp"
local list_cmd="$1"
case "$list_cmd" in
'dpkg -L "$pkg"' | 'rpm -ql "$pkg"') ;;
*)
echo "ERROR: unsupported list command for smoke asserts: $list_cmd" >&2
return 1
;;
esac
local body
body=$(cat <<'ASSERTS'
files=$(__LIST_CMD__)
entry=$(printf '%s\n' "$files" | grep -E '^/usr/share/applications/[^/]+[.]desktop$' | head -1)
stamp=$(printf '%s\n' "$files" | grep -E '/kiro_crew/_build_info[.]py$' | head -1)
test -n "$entry" && test -f "$entry"
test -n "$stamp"
# The identity comes from the desktop entry's FILENAME, not from parsing its
# Exec value. linux.syncDesktopName ties four things to one name -- the entry
# filename, executableName, Electron's app_id and StartupWMClass -- so the
# filename IS the identity, and reading it needs no string parsing at all.
#
# Exec is deliberately NOT the source: electron-builder quotes that path when
# it contains a character outside [/0-9A-Za-z._-], which the nightly channel's
# `/opt/KiroCrew Nightly/...` does. Parsing it would work on stable and return
# /opt/KiroCrew Nightly/... does. Parsing it would work on stable and return
# empty on nightly -- the third time this gate assumed stable's shape.
exe=\$(basename "\$entry" .desktop)
launcher=/usr/bin/\$exe
test -x "\$launcher"
exe=$(basename "$entry" .desktop)
launcher=/usr/bin/$exe
test -x "$launcher"
# Window association: StartupWMClass must equal Electron's app_id, which
# electron-builder derives from desktopName -- the same value the launcher
# name follows. A mismatch leaves the running window unassociated with its
# launcher icon, silently, at runtime.
grep -q "^StartupWMClass=\${exe}\$" "\$entry"
grep -q "^StartupWMClass=${exe}$" "$entry"
# Installed under the fixed /opt prefix, which is what makes the AppArmor
# profile, the PATH launcher and in-place updates durable.
grep -qE "^Exec=\\"?/opt/.*/\${exe}" "\$entry"
grep -qE "^Exec=\"?/opt/.*/${exe}" "$entry"
# The bundled CLI that fixed prefix makes reachable -- what an AppImage
# cannot offer, and what makes \`kirocrew service install\` usable.
printf '%s\\n' "\$files" | grep -qE '/backend-dist/kirocrew-backend/bin/kirocrew\$'
# cannot offer, and what makes `kirocrew service install` usable.
printf '%s\n' "$files" | grep -qE '/backend-dist/kirocrew-backend/bin/kirocrew$'
ASSERTS
)
body="${body//__LIST_CMD__/$list_cmd}"
printf '%s' "$body"
}

echo "▶ Installing '${DEB_NAME}' on Ubuntu 24.04 (the t64 release)…"
Expand Down
7 changes: 7 additions & 0 deletions test/test_check_comment_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def test_the_gate_runs_the_self_test_first(self) -> None:
# silently disables a rule fails in CI instead of shipping green.
assert "--test" in str(_gate_step().get("run", ""))

def test_source_gates_checkout_fetches_full_history_for_merge_base(self) -> None:
job = yaml.safe_load(FAST_GATE.read_text(encoding="utf-8"))["jobs"]["source-gates"]
checkout = next(
step for step in job["steps"] if "actions/checkout" in str(step.get("uses", ""))
)
assert (checkout.get("with") or {}).get("fetch-depth") == 0

def test_the_gate_is_diff_scoped_via_the_base_ref_env(self) -> None:
# Without the env the script only reports. A step that forgot to export
# it would print counts and pass every PR.
Expand Down
142 changes: 142 additions & 0 deletions test/test_smoke_linux_packages_common_asserts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
r"""``common_asserts`` must not execute host commands while building container script.

The docker wrapper expands ``$(common_asserts …)`` on the HOST. An unquoted
heredoc there treats backticks and ``$()`` as shell syntax on the host before
``docker run`` starts — not inside the container.
"""

from __future__ import annotations

import shutil
import subprocess
from pathlib import Path

import pytest

ROOT = Path(__file__).resolve().parents[1]
SCRIPT = ROOT / "scripts" / "smoke-linux-packages.sh"

_ALLOWED_LIST_CMDS = ('dpkg -L "$pkg"', 'rpm -ql "$pkg"')


def _bash_executable(posix_shell: str) -> str:
"""Bash that can parse this script, never Windows' WSL launcher.

On a Windows runner ``shutil.which("bash")`` resolves to
``C:\\Windows\\System32\\bash.exe``. That launcher writes a UTF-16
"Windows Subsystem for Linux has no installed distributions" line and
exits 1, so a PATH lookup fails every assertion before the script runs.
``posix_test_shell`` is Git Bash on that host. A path whose file name is
already bash is the interpreter to run; looking it up again finds the
launcher.
"""
if Path(posix_shell).stem.lower() == "bash":
return posix_shell
resolved = shutil.which("bash")
assert resolved is not None, f"{posix_shell} is not bash and no bash is on PATH"
return resolved


def test_a_bash_path_is_not_looked_up_again() -> None:
"""The Windows choice, checked without a Windows host.

Git Bash's path is what the shared fixture returns there. Re-resolving
``bash`` from PATH selects the WSL launcher instead.
"""
git_bash = "C:/Program Files/Git/bin/bash.exe"
assert _bash_executable(git_bash) == git_bash


@pytest.fixture
def bash_exe(posix_test_shell: str) -> str:
return _bash_executable(posix_test_shell)


def _common_asserts_definition() -> str:
text = SCRIPT.read_text(encoding="utf-8")
start = text.index("common_asserts() {")
depth = 0
for i in range(start, len(text)):
ch = text[i]
if ch == "{":
depth += 1
elif ch == "}":
depth -= 1
if depth == 0:
return text[start : i + 1]
raise AssertionError("common_asserts() body not closed in smoke-linux-packages.sh")


def _bash(bash_exe: str, script: str) -> subprocess.CompletedProcess[str]:
return subprocess.run(
[bash_exe, "-c", script],
cwd=ROOT,
check=False,
capture_output=True,
text=True,
encoding="utf-8",
)


def test_common_asserts_rejects_unknown_list_command(bash_exe: str) -> None:
proc = _bash(bash_exe, f"{_common_asserts_definition()}\ncommon_asserts 'id'")
assert proc.returncode != 0
assert "unsupported list command" in (proc.stderr or "")


def test_common_asserts_quoted_heredoc_has_no_host_expansion(bash_exe: str) -> None:
for list_cmd in _ALLOWED_LIST_CMDS:
proc = _bash(bash_exe, f"{_common_asserts_definition()}\ncommon_asserts {list_cmd!r}")
assert proc.returncode == 0, proc.stderr
assert "No such file or directory" not in (proc.stderr or ""), proc.stderr
assert list_cmd in proc.stdout
assert 'exe=$(basename "$entry" .desktop)' in proc.stdout


def test_unquoted_heredoc_with_sentinel_runs_on_host(bash_exe: str, tmp_path: Path) -> None:
"""Documents the failure mode the quoted heredoc removes."""
marker = tmp_path / "host-expanded"
# The path is inside the script body, so Git Bash does not convert
# backslashes the way it converts an argv path. ``as_posix`` is a path
# both Git Bash and a POSIX bash can ``touch``.
marker_shell = marker.as_posix().replace("'", "'\\''")
probe = f"""
common_asserts_old() {{
cat <<ASSERTS
host_probe=$(touch '{marker_shell}')
ASSERTS
}}
common_asserts_old
"""
proc = _bash(bash_exe, probe)
assert proc.returncode == 0, proc.stderr
assert marker.is_file(), "unquoted heredoc command substitution must execute on the host"


def test_common_asserts_emits_nightly_path_in_comment_without_executing(bash_exe: str) -> None:
proc = _bash(
bash_exe,
f"{_common_asserts_definition()}\n" f"common_asserts {_ALLOWED_LIST_CMDS[0]!r}",
)
assert "/opt/KiroCrew Nightly" in proc.stdout
assert proc.returncode == 0
assert not (proc.stderr or "").strip()


def test_common_asserts_uses_quoted_heredoc_delimiter() -> None:
text = SCRIPT.read_text(encoding="utf-8")
start = text.index("common_asserts() {")
chunk = text[start:]
assert "<<'ASSERTS'" in chunk, "common_asserts must expand body with a quoted heredoc delimiter"
assert "cat <<ASSERTS" not in chunk.replace("<<'ASSERTS'", "")


def test_smoke_linux_packages_shell_syntax(bash_exe: str) -> None:
proc = subprocess.run(
[bash_exe, "-n", str(SCRIPT)],
check=False,
capture_output=True,
text=True,
encoding="utf-8",
)
assert proc.returncode == 0, proc.stderr
Loading