#!/bin/bash # Reproduces every factual claim in # https://www.heatware.net/macos/brew-install-specific-version/ # # Safe by default. It installs exactly one tiny formula (lua@5.4, ~766KB, zero dependencies) # and uninstalls it again, and only if you don't already have lua or lua@5.4 — otherwise it # SKIPs rather than touching your Cellar. Nothing else is mutated. # # The homebrew/core clone (1.4GB, ~2 minutes) is OPT-IN: # HW_ALLOW_BIG_TAP=1 bash verify-brew-install-specific-version.sh # Without it, the brew extract claims report SKIP. # # Usage: bash verify-brew-install-specific-version.sh # Runtime: ~30s default, ~6 minutes with HW_ALLOW_BIG_TAP=1 set -u PASSES=0; FAILS=0; SKIPS=0 pass() { PASSES=$((PASSES+1)); printf ' \033[32mPASS\033[0m %s\n' "$1"; } fail() { FAILS=$((FAILS+1)); printf ' \033[31mFAIL\033[0m %s\n' "$1" printf ' expected: %s\n actual: %s\n' "$2" "$3"; } skip() { SKIPS=$((SKIPS+1)); printf ' \033[33mSKIP\033[0m %s (needs %s)\n' "$1" "$2"; } hdr() { printf '\n\033[1m%s\033[0m\n' "$*"; } contains() { case "$3" in *"$2"*) pass "$1";; *) fail "$1" "output containing: $2" "$(echo "$3" | head -3)";; esac; } check() { [ "$2" = "$3" ] && pass "$1" || fail "$1" "$2" "$3"; } W="$(mktemp -d -t hwbrew)" CLEAN_LUA=0 cleanup() { if [ "$CLEAN_LUA" = "1" ]; then printf '\n restoring: uninstalling lua@5.4\n' brew unpin lua@5.4 >/dev/null 2>&1 brew uninstall --formula lua@5.4 >/dev/null 2>&1 fi rm -rf "$W" } trap cleanup EXIT echo "Platform: $(sw_vers -productName) $(sw_vers -productVersion) $(uname -m)" if ! command -v brew >/dev/null 2>&1; then echo "Homebrew not installed - nothing to verify."; exit 0 fi echo "Homebrew: $(brew --version | head -1)" PREFIX="$(brew --prefix)" # ------------------------------------------------------------------ claim 1 hdr "Claim: 'brew switch' no longer exists and exits 1 like an unknown command" SW_OUT="$(brew switch node 22 2>&1)"; SW_RC=$? UNK_OUT="$(brew definitely-not-a-real-brew-command 2>&1)"; UNK_RC=$? check "brew switch exits non-zero" "1" "$SW_RC" check "an unknown brew command exits the same way" "$UNK_RC" "$SW_RC" if [ "$(echo "$SW_OUT" | head -1)" = "$(echo "$UNK_OUT" | head -1)" ]; then pass "brew switch prints the same generic help as an unknown command" else fail "brew switch prints the generic unknown-command help" "$(echo "$UNK_OUT" | head -1)" "$(echo "$SW_OUT" | head -1)" fi # ------------------------------------------------------------------ claim 2 hdr "Claim: versioned formulae are discoverable with an anchored regex search" for fam in php postgresql node python openjdk; do OUT="$(brew search "/^${fam}@/" 2>/dev/null | tr '\n' ' ')" case "$OUT" in *"${fam}@"*) pass "brew search /^${fam}@/ returns versioned formulae: ${OUT}";; *) fail "brew search /^${fam}@/ returns versioned formulae" "at least one ${fam}@N" "${OUT:-}";; esac done # ------------------------------------------------------------------ claim 3 hdr "Claim: Homebrew auto-links a keg-only versioned formula installed on request" SRC="$PREFIX/Library/Homebrew/formula_installer.rb" if [ -f "$SRC" ]; then contains "formula_installer.rb defines auto_link_versioned_keg_only?" \ "def auto_link_versioned_keg_only?" "$(cat "$SRC")" contains " ...and it bails out if any version is already installed" \ "return false if formula.any_version_installed?" "$(sed -n '/def auto_link_versioned_keg_only?/,/^ end/p' "$SRC")" else skip "formula_installer.rb source checks" "a source Homebrew install" fi if brew list --formula --versions lua 2>/dev/null | grep -q . || \ brew list --formula --versions lua@5.4 2>/dev/null | grep -q .; then skip "live install of lua@5.4" "a machine without lua already installed" else contains "brew install lua@5.4 reports it is keg-only" "keg-only" "$(brew info lua@5.4 2>&1)" if brew install lua@5.4 >"$W/install.log" 2>&1; then CLEAN_LUA=1 contains "lua@5.4 lands in the Cellar" "Cellar/lua@5.4" "$(cat "$W/install.log")" if [ -L "$PREFIX/bin/lua" ]; then pass "the keg-only versioned formula was auto-linked: $(ls -l "$PREFIX/bin/lua" | sed 's/.*-> //')" else fail "the keg-only versioned formula was auto-linked" "$PREFIX/bin/lua is a symlink" "no symlink" fi V="$("$PREFIX/opt/lua@5.4/bin/lua" -v 2>&1)" contains "the installed binary reports a 5.4 version" "Lua 5.4" "$V" # ---------------------------------------------------------- claim 4 hdr "Claim: brew pin records the pin as a symlink and brew upgrade skips pinned formulae" brew pin lua@5.4 >/dev/null 2>&1 contains "brew list --pinned reports it" "lua@5.4" "$(brew list --pinned 2>&1)" if [ -L "$PREFIX/var/homebrew/pinned/lua@5.4" ]; then pass "the pin is a symlink under var/homebrew/pinned" else fail "the pin is a symlink under var/homebrew/pinned" "symlink present" "absent" fi contains "brew pin --help warns dependents may break" \ "might not install or run correctly" "$(brew pin --help 2>&1)" UP="$PREFIX/Library/Homebrew/cmd/upgrade.rb" if [ -f "$UP" ]; then contains "upgrade.rb subtracts pinned formulae from the work list" "outdated -= pinned" "$(cat "$UP")" else skip "upgrade.rb source check" "a source Homebrew install" fi brew unpin lua@5.4 >/dev/null 2>&1 else fail "brew install lua@5.4 succeeds" "exit 0" "$(tail -3 "$W/install.log")" fi fi # ------------------------------------------------------------------ claim 5 hdr "Claim: a version with no versioned formula simply is not installable" OUT="$(brew install lua@5.3 2>&1)" contains "brew install lua@5.3 reports no available formula" "No available formula" "$OUT" # ------------------------------------------------------------------ claim 6 hdr "Claim: brew install of a .rb file by path is rejected" cat > "$W/hwtest-formula.rb" <<'RB' class HwtestFormula < Formula desc "Test" homepage "https://example.com" url "https://www.lua.org/ftp/lua-5.4.6.tar.gz" sha256 "7d5ea1b9cb6aa0b59ca3dde1c6adcb57ef83a1ba8e5432c0ecd06bf439b3ad88" end RB OUT="$(brew install "$W/hwtest-formula.rb" 2>&1)" contains "Homebrew requires formulae to be in a tap" "requires formulae to be in a tap" "$OUT" # ------------------------------------------------------------------ claim 7 hdr "Claim: brew extract needs the homebrew/core tap, which brew itself refuses to add" CORE_DIR="$PREFIX/Library/Taps/homebrew/homebrew-core" if [ -d "$CORE_DIR" ]; then skip "the 'extract without the tap' error" "a machine WITHOUT homebrew/core already tapped" else OUT="$(brew extract --version=5.3.6 lua homebrew/core 2>&1)" contains "brew extract says it requires the tap homebrew/core" "requires the tap homebrew/core" "$OUT" OUT="$(brew tap homebrew/core 2>&1)" contains "brew tap homebrew/core refuses without --force" "no longer typically necessary" "$OUT" fi # ------------------------------------------------------------------ claim 8 hdr "Claim: with the tap present, extract works - into YOUR tap, not homebrew/core" DID_TAP=0 if [ ! -d "$CORE_DIR" ]; then if [ "${HW_ALLOW_BIG_TAP:-0}" = "1" ]; then echo " cloning homebrew/core (1.4GB, ~2 min)..." if brew tap --force homebrew/core >"$W/tap.log" 2>&1; then DID_TAP=1 contains "brew tap --force homebrew/core succeeds" "Tapped" "$(cat "$W/tap.log")" SIZE="$(du -sh "$CORE_DIR" 2>/dev/null | cut -f1)" COMMITS="$(git -C "$CORE_DIR" rev-list --count HEAD 2>/dev/null)" echo " tap size: $SIZE commits: $COMMITS" [ "${COMMITS:-0}" -gt 100000 ] && pass "the tap carries the full formula history (>100k commits)" \ || fail "the tap carries the full formula history" ">100000 commits" "${COMMITS:-unknown}" else fail "brew tap --force homebrew/core succeeds" "exit 0" "$(tail -3 "$W/tap.log")" fi else skip "brew extract end-to-end" "HW_ALLOW_BIG_TAP=1 (clones 1.4GB)" fi fi if [ -d "$CORE_DIR" ]; then OUT="$(brew extract --version=5.4.6 lua homebrew/core 2>&1)" contains "extracting INTO homebrew/core is refused" "Cannot extract formula to homebrew/core" "$OUT" TAPNAME="hwverify/oldversions" TAPDIR="$PREFIX/Library/Taps/hwverify/homebrew-oldversions" if [ -d "$TAPDIR" ]; then skip "brew extract into a fresh tap" "no pre-existing $TAPNAME tap" else brew tap-new "$TAPNAME" --no-git >/dev/null 2>&1 OUT="$(brew extract --version=5.4.6 lua "$TAPNAME" 2>&1)" contains "brew extract writes lua@5.4.6.rb into your tap" "lua@5.4.6.rb" "$OUT" if [ -f "$TAPDIR/Formula/lua@5.4.6.rb" ]; then pass "the extracted formula exists on disk" contains " ...and it pins the 5.4.6 tarball" "lua-5.4.6.tar.gz" "$(cat "$TAPDIR/Formula/lua@5.4.6.rb")" else fail "the extracted formula exists on disk" "$TAPDIR/Formula/lua@5.4.6.rb" "missing" fi OUT="$(brew extract --version=5.3.6 lua "$TAPNAME" 2>&1)" contains "asking for a version that never existed fails after the history walk" \ "Could not find lua" "$OUT" brew untap "$TAPNAME" >/dev/null 2>&1 echo " restoring: untapped $TAPNAME" fi fi if [ "$DID_TAP" = "1" ]; then brew untap homebrew/core >/dev/null 2>&1 echo " restoring: untapped homebrew/core" fi # ------------------------------------------------------------------ summary printf '\n\033[1mSummary\033[0m PASS=%d FAIL=%d SKIP=%d\n' "$PASSES" "$FAILS" "$SKIPS" [ "$FAILS" -eq 0 ]