test: add regression tests for gerrit and github links

This commit is contained in:
Paul Irish 2026-04-27 10:45:49 -07:00
parent ba6759fa94
commit 5c78bb1641
No known key found for this signature in database
2 changed files with 104 additions and 21 deletions

View file

@ -32,7 +32,7 @@ fi
# The HEAD of the primary branch (eg main or master or w/e), for diffing.
# TODO: some branch mgmt approaches don't work well with this. And may prefer `git log --pretty=format:%H --merges -n 1`. See https://github.com/paulirish/git-recent/issues/28
diff_base=$(git symbolic-ref refs/remotes/origin/HEAD)
diff_base=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null || git rev-parse origin/main 2>/dev/null || git rev-parse origin/master 2>/dev/null || echo "HEAD")
# Extract branch name (without any trailing text, like the Chromium link)
define_branchname="branchname=\\\$(echo {1} | cut -d' ' -f1)"
@ -102,7 +102,7 @@ _browse_branches() {
local cs="" ct="" gs="" gp="" gc="" sl="$show_links"
if [[ "$sl" == true ]]; then
gc=$(git config --get-regexp "branch\..*\.gerrit" 2>/dev/null)
gs=$(git config --get "gerritserver" 2>/dev/null | sed -E 's|https?://||')
gs=$( (git config --get "gerrit.host" || git config --get "gerritserver") 2>/dev/null | sed -E 's|https?://||')
gp=$(git config --get "gerrit.project" 2>/dev/null)
if command -v gh >/dev/null 2>&1 && git remote -v | grep -q "github.com"; then
ct="github"; cs=$(gh pr list --state all --author "@me" --limit 100 --json headRefName,number,url --template '{{range .}}{{ .headRefName }} : {{ .url }} {{ .number }}{{"\n"}}{{end}}')
@ -117,16 +117,30 @@ _browse_branches() {
num=$(echo "$gc" | grep "branch\.${branch_name}\.gerritissue" | awk '{print $2}')
if [[ -n "$num" ]]; then
srv=$(echo "$gc" | grep "branch\.${branch_name}\.gerritserver" | awk '{print $2}' | sed -E 's|https?://||')
url="https://${srv:-$gs}/c/${gp:+$gp/+/}$num"; label="$num"
elif info=$(echo "$cs" | grep -F "${branch_name} :" | head -n 1); then
url=$(echo "$info" | grep -o -E 'https://[^ ]+')
label=$(echo "$url" | grep -o -E '[0-9]+$')
[[ "$ct" == "github" ]] && label="#$label"
if [[ -n "$gp" ]]; then
url="https://${srv:-$gs}/c/$gp/+/$num"
else
url="https://${srv:-$gs}/c/$num"
fi
label="$num"
elif [[ -n "$cs" ]]; then
info=$(echo "$cs" | grep -F "${branch_name} :" | head -n 1)
if [[ -n "$info" ]]; then
url=$(echo "$info" | grep -o -E 'https://[^ ]+')
label=$(echo "$url" | grep -o -E '[0-9]+$')
[[ "$ct" == "github" ]] && label="#$label"
fi
fi
fi
if [[ -n "$label" ]]; then
printf "$YELLOW%s$DIM%s \033]8;;%s\a%s\033]8;;\a$NC\n" "$branch_name" "$wt_symbol" "$url" "$label"
# Clickable hyperlink: https://iterm2.com/feature-reporting/Hyperlinks_in_Terminal_Emulators.html
# We skip the hyperlink escape sequence if TERM=dumb (for easier testing/plain logs)
if [[ "$TERM" == "dumb" ]]; then
printf "$YELLOW%s$DIM%s %s ($url)$NC\n" "$branch_name" "$wt_symbol" "$label"
else
printf "$YELLOW%s$DIM%s \033]8;;%s\a%s\033]8;;\a$NC\n" "$branch_name" "$wt_symbol" "$url" "$label"
fi
else
printf "$YELLOW%s$DIM%s$NC\n" "$branch_name" "$wt_symbol"
fi
@ -143,9 +157,11 @@ _browse_branches() {
--bind "ctrl-o:preview:$diffbranch_cmd"
}
output="$(_browse_branches)"
# If fzf returned a single branch in non-interactive mode (GIT_RECENT_QUERY set),
# it normally auto-checkouts. We detect if we should skip that for testing.
line_count=$(printf "%s" "$output" | wc -l)
if [[ -n "$output" ]] && (( line_count == 0 )); then
if [[ -n "$output" ]] && (( line_count == 0 )) && [[ "$GIT_RECENT_TEST_NO_CHECKOUT" != "true" ]]; then
chosen_branch=$(echo "$output" | cut -d' ' -f1)
wt_path=$(_get_worktree_for_branch "$chosen_branch")

91
test.sh
View file

@ -3,6 +3,8 @@ set -e
# Setup a temporary directory for the test repository
TEST_DIR=$(mktemp -d)
BIN_DIR="$TEST_DIR/bin"
mkdir -p "$BIN_DIR"
trap 'rm -rf "$TEST_DIR"' EXIT
# Absolute paths to the scripts in the current directory
@ -17,6 +19,11 @@ git init --initial-branch=main
git config user.name "BrewTestBot"
git config user.email "brew@test.bot"
# Setup dummy remote to satisfy git symbolic-ref refs/remotes/origin/HEAD
git remote add origin https://example.com/repo.git
mkdir -p .git/refs/remotes/origin
echo "ref: refs/remotes/origin/main" > .git/refs/remotes/origin/HEAD
git commit --allow-empty -m "test_commit"
git checkout -b "feature-x-branch"
git commit --allow-empty -m "commit on feature branch"
@ -31,24 +38,84 @@ if echo "$OG_OUTPUT" | grep -q "main" && echo "$OG_OUTPUT" | grep -q "BrewTestBo
echo "✅ git-recent-og output matches expected pattern."
else
echo "❌ git-recent-og output mismatch."
echo "Output was: $OG_OUTPUT"
exit 1
fi
# 3. Test git-recent
# Setting GIT_RECENT_QUERY="x" uses fzf --filter="x" which is non-interactive.
# git-recent logic: if fzf returns exactly one line (line_count == 0 due to printf/wc behavior), it checks it out.
echo "Testing git-recent..."
export GIT_RECENT_QUERY="x"
# 3. Test git-recent basic checkout
echo "Testing git-recent basic checkout..."
export GIT_RECENT_QUERY="feature-x"
$GIT_RECENT > /dev/null 2>&1
[ "$(git rev-parse --abbrev-ref HEAD)" == "feature-x-branch" ] && echo "✅ git-recent checkout passed." || exit 1
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$CURRENT_BRANCH" == "feature-x-branch" ]; then
echo "✅ git-recent correctly checked out 'feature-x-branch'."
# 4. Regression: Gerrit Link Generation (via git config)
echo "Testing Gerrit link generation (via git config)..."
git config "branch.feature-x-branch.gerritissue" "1234"
git config "gerrit.host" "chromium-review.googlesource.com"
git config "gerrit.project" "devtools/devtools-frontend"
git checkout main > /dev/null 2>&1
export GIT_RECENT_QUERY="feature-x"
export GIT_RECENT_TEST_NO_CHECKOUT="true"
export TERM="dumb"
# We use fzf --filter which outputs the matching line
# We capture both stdout and stderr to be safe
OUTPUT=$($GIT_RECENT --cl 2>&1)
# We check if the number and the URL fragment are present.
# Terminal escape codes for hyperlinks can make exact string matching tricky in bash.
if echo "$OUTPUT" | grep -q "1234" && echo "$OUTPUT" | grep -q "chromium-review.googlesource.com" && echo "$OUTPUT" | grep -q "devtools-frontend"; then
echo "✅ Gerrit link correctly generated from config."
else
echo "❌ git-recent failed to check out expected branch."
echo "Current branch is: $CURRENT_BRANCH"
echo "❌ Gerrit link mismatch."
echo "Output was: $OUTPUT"
exit 1
fi
echo "All tests passed! 🎉"
# 5. Regression: GitHub PR Link Generation (via gh mock)
echo "Testing GitHub PR link generation (via mock gh)..."
# Create a mock gh command
cat > "$BIN_DIR/gh" <<EOF
#!/bin/bash
if [[ "\$*" == *"pr list"* ]]; then
echo "feature-x-branch : https://github.com/paulirish/git-recent/pull/99 99"
fi
EOF
chmod +x "$BIN_DIR/gh"
export PATH="$BIN_DIR:$PATH"
# Set a github remote to trigger github logic
git remote add origin-github https://github.com/paulirish/git-recent.git
# Remove gerrit configs to avoid gerrit matching first
git config --unset "branch.feature-x-branch.gerritissue"
git config --remove-section gerrit 2>/dev/null || true
export GIT_RECENT_QUERY="feature-x"
OUTPUT=$($GIT_RECENT --pr 2>&1)
if echo "$OUTPUT" | grep -q "#99" && echo "$OUTPUT" | grep -q "github.com/paulirish/git-recent/pull/99"; then
echo "✅ GitHub PR link correctly generated from gh."
else
echo "❌ GitHub PR link mismatch."
echo "Output was: $OUTPUT"
exit 1
fi
# 6. Regression: Priority Detection (GitHub over Gerrit false positive)
echo "Testing Repo Priority (GitHub over Gerrit false positive)..."
# Mock git-cl to return 0 but No issue assigned (the false positive case)
cat > "$BIN_DIR/git-cl" <<EOF
#!/bin/bash
echo "No issue assigned."
exit 0
EOF
chmod +x "$BIN_DIR/git-cl"
# With both a github remote and git-cl present, it should still pick GitHub
OUTPUT=$($GIT_RECENT --cl 2>&1)
if echo "$OUTPUT" | grep -q "#99"; then
echo "✅ GitHub prioritized over git-cl false positive."
else
echo "❌ Repo detection failed to prioritize GitHub."
echo "Output was: $OUTPUT"
exit 1
fi
echo "All regression tests passed! 🎉"