diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..c69a666 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,17 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + +permissions: + contents: read + +jobs: + test: + runs-on: macos-latest + steps: + - uses: actions/checkout@v7 + - name: Run tests + run: zsh tests/run.zsh diff --git a/brewshelf.sh b/brewshelf.sh index 8ecb667..b647d49 100755 --- a/brewshelf.sh +++ b/brewshelf.sh @@ -1,5 +1,17 @@ #!/usr/bin/env zsh +# Must stay POSIX-compatible up to this check: `bash brewshelf.sh` or `sh brewshelf.sh` +# would otherwise fail later with a cryptic zsh syntax error +if [ -z "$ZSH_VERSION" ]; then + echo "brewshelf: this script requires zsh — run it as 'zsh brewshelf.sh' or './brewshelf.sh'" >&2 + exit 1 +fi + +if ! command -v brew >/dev/null 2>&1; then + print -u2 "brewshelf: Homebrew not found — install it from https://brew.sh" + exit 1 +fi + # Colors BOLD=$'\e[1m' RESET=$'\e[0m' @@ -207,8 +219,15 @@ category_color() { print_shelf() { local -a formulas casks - formulas=("${(@f)$(brew list --formula 2>/dev/null)}") - casks=("${(@f)$(brew list --cask 2>/dev/null)}") + # ${(f)...} unquoted drops empty lines, so an empty list becomes an empty array + # (the quoted "${(@f)...}" form yields one empty element instead) + formulas=(${(f)"$(brew list --formula 2>/dev/null)"}) + casks=(${(f)"$(brew list --cask 2>/dev/null)"}) + + if (( ${#formulas[@]} == 0 && ${#casks[@]} == 0 )); then + print "No Homebrew packages installed." + return 0 + fi typeset -A cat_items local -a unknown @@ -234,17 +253,7 @@ print_shelf() { for cat in "${order[@]}"; do [[ -z "${cat_items[$cat]:-}" ]] && continue - case "$cat" in - "Video & Media") color="$MAGENTA" ;; - "Development") color="$CYAN" ;; - "Database") color="$YELLOW" ;; - "Security & Network") color="$RED" ;; - "CLI Tools") color="$GREEN" ;; - "Image & Graphics") color="$BLUE" ;; - "Compression & Data") color="$WHITE" ;; - "System Libraries") color="$DIM" ;; - *) color="$WHITE" ;; - esac + color="$(category_color "$cat")" print "${BOLD}${color}▶ ${cat}${RESET}" print "${color}$(printf '─%.0s' {1..50})${RESET}" for pkg in ${=cat_items[$cat]}; do @@ -287,7 +296,9 @@ print_shelf() { fi local total=$(( ${#formulas[@]} + ${#casks[@]} )) - print "${DIM} Total: ${total} packages installed${RESET}" + local noun="packages" + (( total == 1 )) && noun="package" + print "${DIM} Total: ${total} ${noun} installed${RESET}" echo "" } diff --git a/tests/bin/brew b/tests/bin/brew new file mode 100755 index 0000000..7a3640d --- /dev/null +++ b/tests/bin/brew @@ -0,0 +1,8 @@ +#!/bin/sh +# Fake `brew` for tests. Package lists come from the files named in +# FAKE_BREW_FORMULAS / FAKE_BREW_CASKS (one name per line). +case "$1 $2" in + "list --formula") cat "$FAKE_BREW_FORMULAS" 2>/dev/null ;; + "list --cask") cat "$FAKE_BREW_CASKS" 2>/dev/null ;; + *) echo "fake brew: unsupported command: $*" >&2; exit 1 ;; +esac diff --git a/tests/run.zsh b/tests/run.zsh new file mode 100644 index 0000000..6da0cb4 --- /dev/null +++ b/tests/run.zsh @@ -0,0 +1,84 @@ +#!/usr/bin/env zsh +# Runs brewshelf.sh against a fake `brew` (tests/bin/brew) and checks the output. +# Usage: zsh tests/run.zsh + +TESTS_DIR=${0:A:h} +SCRIPT=${TESTS_DIR:h}/brewshelf.sh +WORK=$(mktemp -d) +trap 'rm -rf -- "$WORK"' EXIT + +# Only the fake brew and system tools — the real Homebrew must not leak in +TEST_PATH="$TESTS_DIR/bin:/usr/bin:/bin" + +typeset -i passed=0 failed=0 +OUT="" STATUS=0 + +# run_brewshelf [shell] — lists are newline-separated names +run_brewshelf() { + print -rn -- "$1" > "$WORK/formulas" + print -rn -- "$2" > "$WORK/casks" + local shell=${3:-zsh} + OUT=$(env -i HOME="$HOME" PATH="$TEST_PATH" \ + FAKE_BREW_FORMULAS="$WORK/formulas" FAKE_BREW_CASKS="$WORK/casks" \ + "$shell" "$SCRIPT" 2>&1) + STATUS=$? + # Strip ANSI colors so assertions match plain text + OUT=$(print -r -- "$OUT" | sed $'s/\e\\[[0-9;]*m//g') +} + +check() { + local description=$1; shift + if "$@"; then + (( passed++ )) + print " ✔ $description" + else + (( failed++ )) + print " ✖ $description" + print -r -- "$OUT" | sed 's/^/ │ /' + fi +} + +contains() { [[ "$OUT" == *"$1"* ]] } +not_contains() { [[ "$OUT" != *"$1"* ]] } +status_is() { (( STATUS == $1 )) } + +print "syntax" +check "brewshelf.sh parses with zsh -n" zsh -n "$SCRIPT" + +print "known packages" +run_brewshelf $'node\nffmpeg\nsqlite' $'ngrok' +check "exits 0" status_is 0 +check "groups formulas by category" contains "▶ Development" +check "shows the description" contains "JavaScript runtime (Node.js)" +check "lists casks" contains "ngrok" +check "counts formulas + casks" contains "Total: 4 packages installed" + +print "unknown packages" +run_brewshelf $'node\nsome-unknown-formula' '' +check "puts unknown formulas under Other" contains "▶ Other / Dependencies" +check "lists the unknown formula" contains "some-unknown-formula" + +print "no casks" +run_brewshelf $'node' '' +check "skips the casks section" not_contains "GUI Applications" +check "does not count a phantom cask" contains "Total: 1 package installed" + +print "nothing installed" +run_brewshelf '' '' +check "exits 0" status_is 0 +check "says nothing is installed" contains "No Homebrew packages installed." +check "prints no shelf" not_contains "Total:" + +print "Homebrew missing" +OUT=$(env -i HOME="$HOME" PATH="/usr/bin:/bin" zsh "$SCRIPT" 2>&1); STATUS=$? +check "exits 1" status_is 1 +check "explains that Homebrew is missing" contains "Homebrew not found" + +print "wrong shell" +run_brewshelf $'node' '' bash +check "bash: exits 1" status_is 1 +check "bash: asks for zsh" contains "requires zsh" + +print "" +print "$passed passed, $failed failed" +(( failed == 0 ))