|
8 | 8 |
|
9 | 9 | setup() { |
10 | 10 | # The installer contract keys off these; a leaked value would skew results. |
11 | | - unset BASECAMP_SKIP_SETUP BASECAMP_SETUP_AGENT |
| 11 | + unset BASECAMP_SKIP_SETUP BASECAMP_NONINTERACTIVE BASECAMP_SETUP_AGENT |
12 | 12 |
|
13 | 13 | INSTALL_SH="${BATS_TEST_DIRNAME}/../scripts/install.sh" |
14 | 14 | INSTALL_PS1="${BATS_TEST_DIRNAME}/../scripts/install.ps1" |
@@ -95,6 +95,68 @@ run_post_install_setup() { |
95 | 95 | [[ "$output" != *"BASH_SOURCE[0]: unbound variable"* ]] |
96 | 96 | } |
97 | 97 |
|
| 98 | +@test "install.sh gives piped first-time setup the controlling terminal" { |
| 99 | + run grep -F 'run_first_time_setup "$BIN_DIR/$binary_name" </dev/tty' "$INSTALL_SH" |
| 100 | + [[ "$status" -eq 0 ]] |
| 101 | + run grep -F '[[ -t 1 ]] && [[ -t 2 ]] && { : </dev/tty; } 2>/dev/null' "$INSTALL_SH" |
| 102 | + [[ "$status" -eq 0 ]] |
| 103 | +} |
| 104 | + |
| 105 | +@test "install.sh matches the CLI non-interactive truthy values" { |
| 106 | + run bash -c " |
| 107 | + set -euo pipefail |
| 108 | + source '$INSTALL_SH' |
| 109 | + for value in 1 true TRUE True; do |
| 110 | + env_value_is_true \"\$value\" || exit 1 |
| 111 | + done |
| 112 | + for value in 0 false FALSE yes ''; do |
| 113 | + if env_value_is_true \"\$value\"; then exit 1; fi |
| 114 | + done |
| 115 | + " |
| 116 | + [[ "$status" -eq 0 ]] |
| 117 | +} |
| 118 | + |
| 119 | +@test "install.sh rejects a present but unusable controlling terminal" { |
| 120 | + [[ "$(uname -s)" == "Linux" ]] || skip "setsid reproduction requires Linux" |
| 121 | + command -v setsid >/dev/null 2>&1 || skip "setsid is required" |
| 122 | + command -v script >/dev/null 2>&1 || skip "script is required" |
| 123 | + |
| 124 | + cat > "$STUB_DIR/tty-probe" <<EOF |
| 125 | +#!/usr/bin/env bash |
| 126 | +source '$INSTALL_SH' |
| 127 | +if can_run_first_time_setup; then |
| 128 | + echo SETUP_CAN_RUN |
| 129 | +else |
| 130 | + echo SETUP_CANNOT_RUN |
| 131 | +fi |
| 132 | +EOF |
| 133 | + chmod +x "$STUB_DIR/tty-probe" |
| 134 | + |
| 135 | + run script -qec "setsid -f -w '$STUB_DIR/tty-probe'" /dev/null |
| 136 | + [[ "$status" -eq 0 ]] |
| 137 | + [[ "$output" == *"SETUP_CANNOT_RUN"* ]] |
| 138 | + [[ "$output" != *"SETUP_CAN_RUN"* ]] |
| 139 | +} |
| 140 | + |
| 141 | +@test "install.sh keeps installation successful when first-time setup fails" { |
| 142 | + cat > "$STUB_DIR/setup-fails" <<'EOF' |
| 143 | +#!/usr/bin/env bash |
| 144 | +exit 23 |
| 145 | +EOF |
| 146 | + chmod +x "$STUB_DIR/setup-fails" |
| 147 | + |
| 148 | + run bash -c " |
| 149 | + set -euo pipefail |
| 150 | + source '$INSTALL_SH' |
| 151 | + run_first_time_setup '$STUB_DIR/setup-fails' |
| 152 | + echo install-survived |
| 153 | + " |
| 154 | + [[ "$status" -eq 0 ]] |
| 155 | + [[ "$output" == *"First-time setup did not finish"* ]] |
| 156 | + [[ "$output" == *"basecamp setup"* ]] |
| 157 | + [[ "$output" == *"install-survived"* ]] |
| 158 | +} |
| 159 | + |
98 | 160 | @test "new binary: post_install_setup dispatches to 'setup agents', never 'setup claude'" { |
99 | 161 | run_post_install_setup |
100 | 162 | [[ "$status" -eq 0 ]] |
@@ -218,6 +280,114 @@ run_post_install_setup() { |
218 | 280 | [[ "$output" == *"nk=1 skill install"* ]] |
219 | 281 | } |
220 | 282 |
|
| 283 | +@test "install.ps1 honors non-interactive values and tolerates setup failure" { |
| 284 | + if ! command -v pwsh >/dev/null 2>&1; then |
| 285 | + if [[ -n "${CI:-}" ]]; then |
| 286 | + echo "pwsh is required in CI for install.ps1 setup coverage" >&2 |
| 287 | + return 1 |
| 288 | + fi |
| 289 | + skip "pwsh not installed" |
| 290 | + fi |
| 291 | + |
| 292 | + cat > "$STUB_DIR/ps-setup-fails" <<'EOF' |
| 293 | +#!/usr/bin/env bash |
| 294 | +exit 23 |
| 295 | +EOF |
| 296 | + chmod +x "$STUB_DIR/ps-setup-fails" |
| 297 | + |
| 298 | + cat > "$STUB_DIR/first-time-driver.ps1" <<'EOF' |
| 299 | +$ErrorActionPreference = 'Stop' |
| 300 | +$tokens = $null; $parseErrors = $null |
| 301 | +$ast = [System.Management.Automation.Language.Parser]::ParseFile($env:INSTALL_PS1_PATH, [ref]$tokens, [ref]$parseErrors) |
| 302 | +if ($parseErrors.Count -gt 0) { throw "install.ps1 parse errors: $($parseErrors -join '; ')" } |
| 303 | +foreach ($name in @('Test-TruthyEnvironmentValue', 'Invoke-FirstTimeSetup')) { |
| 304 | + $fn = $ast.Find({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name }, $true) |
| 305 | + if (-not $fn) { throw "$name not found in install.ps1" } |
| 306 | + . ([scriptblock]::Create($fn.Extent.Text)) |
| 307 | +} |
| 308 | +function Warn([string]$Message) { $script:WarningMessage = $Message } |
| 309 | +foreach ($value in @('1', 'true', 'TRUE', 'True')) { |
| 310 | + if (-not (Test-TruthyEnvironmentValue $value)) { throw "truthy value rejected: $value" } |
| 311 | +} |
| 312 | +foreach ($value in @('0', 'false', 'yes', '')) { |
| 313 | + if (Test-TruthyEnvironmentValue $value) { throw "falsey value accepted: $value" } |
| 314 | +} |
| 315 | +Invoke-FirstTimeSetup $env:PS_SETUP_STUB |
| 316 | +"WARN:$script:WarningMessage" |
| 317 | +'install-survived' |
| 318 | +EOF |
| 319 | + |
| 320 | + run bash -c " |
| 321 | + set -euo pipefail |
| 322 | + export INSTALL_PS1_PATH='$INSTALL_PS1' PS_SETUP_STUB='$STUB_DIR/ps-setup-fails' |
| 323 | + pwsh -NoProfile -File '$STUB_DIR/first-time-driver.ps1' |
| 324 | + " |
| 325 | + [[ "$status" -eq 0 ]] |
| 326 | + [[ "$output" == *"WARN:First-time setup did not finish"* ]] |
| 327 | + [[ "$output" == *"install-survived"* ]] |
| 328 | + grep -qF 'Test-TruthyEnvironmentValue $env:BASECAMP_NONINTERACTIVE' "$INSTALL_PS1" |
| 329 | +} |
| 330 | + |
| 331 | +@test "install.ps1 first-time setup preserves terminal streams and visible output" { |
| 332 | + [[ "$(uname -s)" == "Linux" ]] || skip "PTY stream reproduction requires Linux" |
| 333 | + command -v pwsh >/dev/null 2>&1 || skip "pwsh not installed" |
| 334 | + command -v script >/dev/null 2>&1 || skip "script is required" |
| 335 | + |
| 336 | + PS_TTY_LOG="$STUB_DIR/ps-setup-tty.log" |
| 337 | + cat > "$STUB_DIR/ps-setup-probe" <<'EOF' |
| 338 | +#!/usr/bin/env bash |
| 339 | +stdin=redirected |
| 340 | +stdout=redirected |
| 341 | +stderr=redirected |
| 342 | +[[ -t 0 ]] && stdin=tty |
| 343 | +[[ -t 1 ]] && stdout=tty |
| 344 | +[[ -t 2 ]] && stderr=tty |
| 345 | +printf 'stdin=%s stdout=%s stderr=%s\n' "$stdin" "$stdout" "$stderr" > "$PS_TTY_LOG" |
| 346 | +echo SETUP_OUTPUT_VISIBLE |
| 347 | +EOF |
| 348 | + chmod +x "$STUB_DIR/ps-setup-probe" |
| 349 | + |
| 350 | + cat > "$STUB_DIR/ps-first-time-tty-driver.ps1" <<'EOF' |
| 351 | +$ErrorActionPreference = 'Stop' |
| 352 | +$tokens = $null; $parseErrors = $null |
| 353 | +$ast = [System.Management.Automation.Language.Parser]::ParseFile($env:INSTALL_PS1_PATH, [ref]$tokens, [ref]$parseErrors) |
| 354 | +if ($parseErrors.Count -gt 0) { throw "install.ps1 parse errors: $($parseErrors -join '; ')" } |
| 355 | +foreach ($name in @('Warn', 'Invoke-FirstTimeSetup')) { |
| 356 | + $fn = $ast.Find({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name }, $true) |
| 357 | + if (-not $fn) { throw "$name not found in install.ps1" } |
| 358 | + . ([scriptblock]::Create($fn.Extent.Text)) |
| 359 | +} |
| 360 | +Invoke-FirstTimeSetup $env:PS_SETUP_STUB |
| 361 | +EOF |
| 362 | + |
| 363 | + run script -qec "INSTALL_PS1_PATH='$INSTALL_PS1' PS_SETUP_STUB='$STUB_DIR/ps-setup-probe' PS_TTY_LOG='$PS_TTY_LOG' pwsh -NoProfile -File '$STUB_DIR/ps-first-time-tty-driver.ps1'" /dev/null |
| 364 | + [[ "$status" -eq 0 ]] |
| 365 | + [[ "$output" == *"SETUP_OUTPUT_VISIBLE"* ]] |
| 366 | + [[ "$(cat "$PS_TTY_LOG")" == "stdin=tty stdout=tty stderr=tty" ]] |
| 367 | + run grep -F '[void](Invoke-FirstTimeSetup' "$INSTALL_PS1" |
| 368 | + [[ "$status" -ne 0 ]] |
| 369 | +} |
| 370 | + |
| 371 | +@test "install.ps1 treats redirected stderr as non-interactive" { |
| 372 | + [[ "$(uname -s)" == "Linux" ]] || skip "PTY stream reproduction requires Linux" |
| 373 | + command -v pwsh >/dev/null 2>&1 || skip "pwsh not installed" |
| 374 | + command -v script >/dev/null 2>&1 || skip "script is required" |
| 375 | + |
| 376 | + cat > "$STUB_DIR/ps-interactive-driver.ps1" <<'EOF' |
| 377 | +$tokens = $null; $parseErrors = $null |
| 378 | +$ast = [System.Management.Automation.Language.Parser]::ParseFile($env:INSTALL_PS1_PATH, [ref]$tokens, [ref]$parseErrors) |
| 379 | +if ($parseErrors.Count -gt 0) { throw "install.ps1 parse errors: $($parseErrors -join '; ')" } |
| 380 | +$fn = $ast.Find({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq 'Test-InteractiveSession' }, $true) |
| 381 | +if (-not $fn) { throw 'Test-InteractiveSession not found in install.ps1' } |
| 382 | +. ([scriptblock]::Create($fn.Extent.Text)) |
| 383 | +"INTERACTIVE:$(Test-InteractiveSession)" |
| 384 | +EOF |
| 385 | + |
| 386 | + run script -qec "INSTALL_PS1_PATH='$INSTALL_PS1' pwsh -NoProfile -File '$STUB_DIR/ps-interactive-driver.ps1' 2>'$STUB_DIR/ps-stderr.log'" /dev/null |
| 387 | + [[ "$status" -eq 0 ]] |
| 388 | + [[ "$output" == *"INTERACTIVE:False"* ]] |
| 389 | +} |
| 390 | + |
221 | 391 | # The Windows canary can never prove the ps1 belt — Credential Manager works |
222 | 392 | # headlessly with or without it — so pin the behavior here. The function under |
223 | 393 | # test is extracted from install.ps1's AST and evaluated alone: Main never |
|
0 commit comments