Skip to content
Merged
6 changes: 4 additions & 2 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -3643,8 +3643,10 @@ inputs come from the environment for parity with those scripts:
### Install lifecycle (opt-in `@lifecycle` E2E)

The full install lifecycle — packaging, signature-verified install, tamper
rejection, reinstall, PATH handling, and uninstall — is expressed as
`@lifecycle` scenarios in the [E2E cucumber suite](../tests/e2e-cucumber/README.md)
rejection, reinstall, PATH handling, uninstall, and how download progress is
drawn (one bar per download that stays inside the terminal, a terminal too
narrow for a bar saying so rather than going silent, and the wget fallback) —
is expressed as `@lifecycle` scenarios in the [E2E cucumber suite](../tests/e2e-cucumber/README.md)
(`features/install_lifecycle.feature`). They drive `cargo xtask package`, the
real root installer (`install.sh` / `install.ps1`), and the installed binaries
end to end, replacing the former
Expand Down
81 changes: 79 additions & 2 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,85 @@ ok() {
printf '%s\n' "${C_OK}::${C_RESET} $*"
}

# Width to draw a download progress bar at, or empty when no bar should be
# drawn. curl sizes its bar from COLUMNS whenever that variable parses to a
# sane number, in preference to asking the terminal, and then draws a line
# exactly that wide. A COLUMNS left over from a wider window, routine when a
# terminal is resized after the shell starts, therefore makes every refresh run
# past the right margin and wrap; the carriage return ending each frame then
# returns to the start of the wrapped row rather than the bar's, so each redraw
# paints a row lower and one download arrives as a stack of partial bars with
# its percentage torn across two rows. Measuring the terminal here and passing
# that width down puts the real one back in charge, and reserving the last
# column keeps the line clear of the margin if the measurement is off by one.
progress_bar_width() {
width=""
if command -v stty >/dev/null 2>&1; then
# fd 2 is what fetch() gates the bar on, and the only descriptor still
# attached to the terminal under `curl ... | sh`, where stdin is the pipe.
width="$(stty size <&2 2>/dev/null | awk '{print $2}')"
fi
# A terminal whose window size was never set (a freshly opened pty, say)
# reports 0. That means "unmeasured", not "extremely narrow", so fall through
# to the other sources rather than treating it as a width and dropping the
# bar that curl would otherwise have drawn at its own default.
[ "${width}" = "0" ] && width=""
if [ -z "${width}" ] && command -v tput >/dev/null 2>&1; then
# Read fd 2 here too: tput otherwise sizes from its own stdin, which under
# `curl ... | sh` is the pipe, making ncurses report its 80-column default
# instead of the terminal. COLUMNS is blanked because ncurses consults it
# first, and would hand back the stale value this function exists to escape.
width="$(COLUMNS='' tput cols <&2 2>/dev/null || true)"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking (comment-accuracy only, confidence 75): the comment above says tput "sizes from its own stdin," which is why the <&2 redirect is said to be needed. Reproduced locally against ncurses tput (6.4): tput cols reads the winsize from fd 2 if it's a tty, else fd 1, regardless of any <&N redirect applied to the invocation — a simulated curl|sh layout (fd0=pipe, fd1/fd2=tty) returned the correct width with no <&2 redirect at all, and tput cols <&1 with fd2 as the tty still reported fd2's size. So the fix still works (this is harmless today), but the stated mechanism for why the redirect matters appears to be wrong for this tput implementation, and could mislead someone who tries to "simplify" based on the explanation, or assumes the same trick transfers to a different introspection tool. Might be worth softening the comment's claim or re-verifying against the busybox/non-ncurses tput variants this script may also run under.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking (standing from my earlier review at f0696b3, code here is unchanged): the comment on lines 97-101 justifies redirecting tput's stdin to fd 2 by saying tput otherwise sizes from its own stdin (the pipe, under curl|sh). Reproduced locally against ncurses tput 6.4: it reads winsize from fd 2 if that's a tty, else fd 1, independent of any <&N redirect on the invocation — a simulated curl|sh layout returned the correct width with no redirect at all in my testing. The fix still works and is harmless today, but the comment's stated rationale appears not to match this tput's actual behavior, and could mislead a future maintainer or someone assuming the trick transfers to a different tput variant (e.g. busybox). Confidence 75 · mechanical · Fix: soften or re-verify the comment's claim across tput implementations.

fi
[ "${width}" = "0" ] && width=""
if [ -z "${width}" ]; then
width="${COLUMNS:-80}"
fi
case "${width}" in
"" | *[!0-9]*) width=80 ;;
*) : ;;
esac
# Strip leading zeros. A zero-padded value such as 080 passes the digit check
# above but is an octal literal to POSIX arithmetic, which dash rejects
# outright ("Illegal number: 080"). bash's 10# prefix would force base 10 but
# is not POSIX, and dash rejects that too, so do it by hand.
while :; do
case "${width}" in
0[0-9]*) width="${width#0}" ;;
*) break ;;
esac
done
# A bare 0 reaching here came from the environment rather than a measurement,
# so treat it as unmeasured like the terminal sources above.
[ "${width}" = "0" ] && width=80
# curl ignores a COLUMNS of 20 or less and sizes the bar itself, which would
# reintroduce the overrun this function exists to prevent. Report no width so
# the caller downloads quietly instead.
if [ "${width}" -le 21 ]; then

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: a COLUMNS of 19 or more digits still aborts the install under dash when stty and tput are both unusable. I ran the function under set -eu with COLUMNS=99999999999999999999: dash reports Illegal number here and at the $((width - 1)) below (bash returns a garbage width instead). Very unlikely in practice, so fine as a follow-up as you proposed. A cheap guard would be to treat anything over ~5 digits as unmeasured before this test, e.g. case ${width} in ??????*) width=80 ;; esac.

return 0
fi
printf '%s\n' "$((width - 1))"
}

# Set once the narrow-terminal notice below has been printed, so three
# downloads do not repeat it.
narrow_terminal_reported=0

fetch() {
url="$1"
output="$2"
failure_message="${3:-failed to download ${url}}"
if command -v curl >/dev/null 2>&1; then
bar_width=""
if [ -t 2 ]; then
curl -fL --progress-bar "${url}" -o "${output}" || {
bar_width="$(progress_bar_width)"
if [ -z "${bar_width}" ] && [ "${narrow_terminal_reported}" -eq 0 ]; then
info "terminal too narrow for a progress bar, downloading quietly"
narrow_terminal_reported=1
fi
fi
if [ -n "${bar_width}" ]; then
COLUMNS="${bar_width}" curl -fL --progress-bar "${url}" -o "${output}" || {
rm -f "${output}"
fail "${failure_message}"
}
Expand All @@ -88,7 +160,12 @@ fetch() {
fi
elif command -v wget >/dev/null 2>&1; then
if [ -t 2 ]; then
wget --show-progress -O "${output}" "${url}" || {
# --show-progress alone leaves wget's full request logging in place, so
# the bar is interleaved with resolver and redirect chatter. -nv drops
# that chatter and keeps the bar, which wget already holds inside the
# terminal width. Not -q, which would also swallow wget's reason for a
# failure and leave only this script's generic message.
wget -nv --show-progress -O "${output}" "${url}" || {
rm -f "${output}"
fail "${failure_message}"
}
Expand Down
60 changes: 60 additions & 0 deletions tests/e2e-cucumber/features/install_lifecycle.feature
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,63 @@ Feature: Release install lifecycle
And the signed bundle is installed with no public key supplied
Then the install fails reporting the required signature is missing
And no binaries are activated in the target directory

# ── Download progress rendering ────────────────────────────────────────

@id:lifecycle-linux-progress-single-line @lifecycle @requires-os:linux
Scenario: lifecycle-38 - Linux - a download draws one progress bar that stays inside the terminal
Given a freshly built release tree
And a generated signing keypair
When the release is packaged and signed with the private key file
And the signed bundle is installed through a pseudo-terminal narrower than COLUMNS
Then every download progress frame stays within the terminal
And the finished screen shows one completed bar per download
And the installed rocm binary is present

@id:lifecycle-linux-progress-narrow-terminal @lifecycle @requires-os:linux
Scenario: lifecycle-39 - Linux - a terminal too narrow for a bar downloads quietly and says why
Given a freshly built release tree
And a generated signing keypair
When the release is packaged and signed with the private key file
And the signed bundle is installed through a terminal too narrow for a progress bar
Then the installer reports that the terminal is too narrow for a progress bar
And the installed rocm binary is present

@id:lifecycle-linux-progress-wget-fallback @lifecycle @requires-os:linux
Scenario: lifecycle-40 - Linux - the wget fallback keeps its progress bar inside the terminal
Given a freshly built release tree
And a generated signing keypair
When the release is packaged and signed with the private key file
And the signed bundle is installed through a pseudo-terminal with curl unavailable
Then every download progress frame stays within the terminal
And the download prints no request logging around the bar
And the installed rocm binary is present

@id:lifecycle-linux-progress-piped-into-shell @lifecycle @requires-os:linux
Scenario: lifecycle-41 - Linux - piping the installer into a shell still sizes the bar from the terminal
Given a freshly built release tree
And a generated signing keypair
When the release is packaged and signed with the private key file
And the signed bundle is installed by piping the installer into a shell
Then every download progress frame stays within the terminal
And each completed bar spans the measured terminal width
And the installed rocm binary is present

@id:lifecycle-linux-progress-unsized-terminal @lifecycle @requires-os:linux
Scenario: lifecycle-42 - Linux - a terminal reporting no size still gets a progress bar
Given a freshly built release tree
And a generated signing keypair
When the release is packaged and signed with the private key file
And the signed bundle is installed where the terminal reports no size
Then the installer still draws a progress bar
And each completed bar spans the measured terminal width
And the installed rocm binary is present

@id:lifecycle-linux-progress-zero-padded-columns @lifecycle @requires-os:linux
Scenario: lifecycle-43 - Linux - a zero-padded COLUMNS does not abort the install
Given a freshly built release tree
And a generated signing keypair
When the release is packaged and signed with the private key file
And the signed bundle is installed with a zero-padded COLUMNS and no way to measure
Then every download progress frame stays within the terminal
And the installed rocm binary is present
7 changes: 5 additions & 2 deletions tests/e2e-cucumber/src/loopback_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ use tower_http::services::ServeDir;

use crate::http_server::{self, ServerHandle};

/// A minimal loopback HTTP file server serving one directory, for the Windows
/// native-HTTP install scenario. Shuts down on drop.
/// A minimal loopback HTTP file server serving one directory.
///
/// Backs the install lifecycle scenarios: the Windows native-HTTP install, and
/// the Linux progress-rendering runs, which need a real HTTP download to draw a
/// bar for. Shuts down on drop.
#[derive(Debug)]
pub struct LoopbackServer {
server: ServerHandle,
Expand Down
Loading
Loading