Skip to content
Merged
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
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
39 changes: 25 additions & 14 deletions brewshelf.sh
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 ""
}

Expand Down
8 changes: 8 additions & 0 deletions tests/bin/brew
Original file line number Diff line number Diff line change
@@ -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
84 changes: 84 additions & 0 deletions tests/run.zsh
Original file line number Diff line number Diff line change
@@ -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 <formulas> <casks> [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 ))
Loading