|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# fp_test.sh - Test the force-push functionality of gitstack |
| 4 | +# |
| 5 | +# This script tests the fp command by using a real remote repository |
| 6 | + |
| 7 | +set -e # Exit immediately if a command exits with a nonzero status |
| 8 | + |
| 9 | +# Get the absolute path of the script directory |
| 10 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 11 | + |
| 12 | +function fail() { |
| 13 | + echo "❌ Test failed: $1" |
| 14 | + exit 1 |
| 15 | +} |
| 16 | + |
| 17 | +function verify_branch_exists() { |
| 18 | + local branch="$1" |
| 19 | + local message="$2" |
| 20 | + |
| 21 | + if git rev-parse --verify "$branch" >/dev/null 2>&1; then |
| 22 | + echo "✅ $message" |
| 23 | + else |
| 24 | + echo "❌ $message" |
| 25 | + echo " Branch '$branch' not found" |
| 26 | + exit 1 |
| 27 | + fi |
| 28 | +} |
| 29 | + |
| 30 | +# Create a temporary test directory |
| 31 | +TEST_DIR=$(mktemp -d) |
| 32 | +echo "Creating test repository in $TEST_DIR" |
| 33 | + |
| 34 | +# Create the remote (bare) repository |
| 35 | +REMOTE_DIR="$TEST_DIR/remote" |
| 36 | +mkdir -p "$REMOTE_DIR" |
| 37 | +cd "$REMOTE_DIR" || exit 1 |
| 38 | +git init --bare |
| 39 | +echo "Created bare repository in $REMOTE_DIR" |
| 40 | + |
| 41 | +# Create the local repository |
| 42 | +LOCAL_DIR="$TEST_DIR/local" |
| 43 | +mkdir -p "$LOCAL_DIR" |
| 44 | +cd "$LOCAL_DIR" || exit 1 |
| 45 | +git init |
| 46 | +git config --local user.email "test@example.com" |
| 47 | +git config --local user.name "Test User" |
| 48 | + |
| 49 | +# Add remote |
| 50 | +git remote add origin "$REMOTE_DIR" |
| 51 | + |
| 52 | +# Create initial commit |
| 53 | +touch README.md |
| 54 | +git add README.md |
| 55 | +git commit -m "Initial commit" |
| 56 | + |
| 57 | +echo "Starting fp command tests..." |
| 58 | + |
| 59 | +# Create test stack |
| 60 | +"$SCRIPT_DIR/gitstack.sh" create test-fp |
| 61 | +echo "test1" > test1.txt |
| 62 | +git add test1.txt |
| 63 | +git commit -m "test1" |
| 64 | + |
| 65 | +"$SCRIPT_DIR/gitstack.sh" increment |
| 66 | +echo "test2" > test2.txt |
| 67 | +git add test2.txt |
| 68 | +git commit -m "test2" |
| 69 | + |
| 70 | +"$SCRIPT_DIR/gitstack.sh" increment |
| 71 | +echo "test3" > test3.txt |
| 72 | +git add test3.txt |
| 73 | +git commit -m "test3" |
| 74 | + |
| 75 | +echo "Testing fp with explicit stack name..." |
| 76 | +"$SCRIPT_DIR/gitstack.sh" fp test-fp |
| 77 | + |
| 78 | +# Clone the remote to verify the pushes |
| 79 | +VERIFY_DIR="$TEST_DIR/verify" |
| 80 | +git clone "$REMOTE_DIR" "$VERIFY_DIR" |
| 81 | +cd "$VERIFY_DIR" || exit 1 |
| 82 | + |
| 83 | +# Verify all branches were pushed |
| 84 | +verify_branch_exists "origin/test-fp-0" "fp should push first branch" |
| 85 | +verify_branch_exists "origin/test-fp-1" "fp should push second branch" |
| 86 | +verify_branch_exists "origin/test-fp-2" "fp should push third branch" |
| 87 | + |
| 88 | +# Return to local repo |
| 89 | +cd "$LOCAL_DIR" || exit 1 |
| 90 | + |
| 91 | +echo "Testing fp without stack name (using current branch)..." |
| 92 | +git checkout test-fp-1 |
| 93 | + |
| 94 | +# Make a change to test force-push |
| 95 | +echo "modified" > test2.txt |
| 96 | +git commit -a --amend -m "test2 modified" |
| 97 | + |
| 98 | +"$SCRIPT_DIR/gitstack.sh" fp |
| 99 | + |
| 100 | +# Verify in the verify repo |
| 101 | +cd "$VERIFY_DIR" || exit 1 |
| 102 | +git fetch origin |
| 103 | + |
| 104 | +# Get the commit message of test-fp-1 |
| 105 | +commit_msg=$(git log -1 --format=%s origin/test-fp-1) |
| 106 | +if [ "$commit_msg" = "test2 modified" ]; then |
| 107 | + echo "✅ fp successfully force-pushed amended commit" |
| 108 | +else |
| 109 | + fail "fp did not force-push amended commit" |
| 110 | +fi |
| 111 | + |
| 112 | +# Return to local repo |
| 113 | +cd "$LOCAL_DIR" || exit 1 |
| 114 | + |
| 115 | +echo "Testing error case - non-existent stack..." |
| 116 | +if "$SCRIPT_DIR/gitstack.sh" fp nonexistent-stack 2>/dev/null; then |
| 117 | + fail "fp should fail on non-existent stack" |
| 118 | +else |
| 119 | + echo "✅ fp correctly failed on non-existent stack" |
| 120 | +fi |
| 121 | + |
| 122 | +echo "Testing error case - not on stack branch and no stack provided..." |
| 123 | +git checkout main |
| 124 | +if "$SCRIPT_DIR/gitstack.sh" fp 2>/dev/null; then |
| 125 | + fail "fp should fail when not on stack branch and no stack provided" |
| 126 | +else |
| 127 | + echo "✅ fp correctly failed when not on stack branch and no stack provided" |
| 128 | +fi |
| 129 | + |
| 130 | +# Clean up |
| 131 | +git checkout main |
| 132 | +"$SCRIPT_DIR/gitstack.sh" delete -f test-fp |
| 133 | +rm -rf test1.txt test2.txt test3.txt |
| 134 | + |
| 135 | +echo |
| 136 | +echo "🎉 All fp tests passed!" |
| 137 | + |
| 138 | +# Clean up test repository |
| 139 | +cd - > /dev/null || exit 1 |
| 140 | +rm -rf "$TEST_DIR" |
0 commit comments