-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·253 lines (217 loc) · 6.25 KB
/
test.sh
File metadata and controls
executable file
·253 lines (217 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env bash
# Git Worktree Utils - Test Suite
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Test directory
TEST_DIR="/tmp/gwt-test-$$"
TEST_REPO="${TEST_DIR}/test-repo"
# Counters
TESTS_PASSED=0
TESTS_FAILED=0
# Source the script and help
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/git-worktree-utils.sh"
# Disable confirmations for all tests
export GWT_CONFIRM_DELETE=false
# Helper functions
print_color() {
local color=$1
shift
echo -e "${color}$*${NC}"
}
test_start() {
print_color "$BLUE" "→ Testing: $1"
}
test_pass() {
print_color "$GREEN" " ✓ PASS: $1"
((++TESTS_PASSED)) || true
}
test_fail() {
print_color "$RED" " ✗ FAIL: $1"
((++TESTS_FAILED)) || true
}
cleanup() {
cd /tmp
rm -rf "$TEST_DIR"
}
# Set up test environment
setup() {
print_color "$YELLOW" "Setting up test environment..."
# Unset git environment variables to ensure clean test environment
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
# Create test directory
mkdir -p "$TEST_DIR"
cd "$TEST_DIR"
# Create test repository
mkdir "$TEST_REPO"
cd "$TEST_REPO"
git init
git config user.email "test@example.com"
git config user.name "Test User"
# Create initial commit
echo "# Test Repo" > README.md
git add README.md
git commit -m "Initial commit"
print_color "$GREEN" "✓ Test environment ready"
echo
}
# Clean up on exit
trap cleanup EXIT
# Header
echo
print_color "$BLUE" "==================================="
print_color "$BLUE" " Git Worktree Utils Test Suite"
print_color "$BLUE" "==================================="
echo
# Set up
setup
# Test 1: List worktrees in empty repo
test_start "List worktrees (empty)"
if output=$(gwt 2>&1); then
if echo "$output" | grep -q "test-repo"; then
test_pass "Listed main worktree"
else
test_fail "Failed to list main worktree"
fi
else
test_fail "Command failed"
fi
# Test 2: Create worktree for new branch
test_start "Create worktree for new branch"
if gwt feature/test-branch > /dev/null 2>&1; then
if [[ -d "../test-repo-feature-test-branch" ]]; then
test_pass "Created worktree directory"
else
test_fail "Directory not created"
fi
# Return to main repo
cd "$TEST_REPO"
else
test_fail "Failed to create worktree"
fi
# Test 3: Create worktree for existing branch
test_start "Create worktree for existing branch"
git checkout -b existing-branch 2>/dev/null
echo "test" > test.txt
git add test.txt
git commit -m "Test commit" 2>/dev/null
git checkout main 2>/dev/null
if gwt existing-branch > /dev/null 2>&1; then
if [[ -f "../test-repo-existing-branch/test.txt" ]]; then
test_pass "Checked out existing branch correctly"
else
test_fail "Files not present in worktree"
fi
cd "$TEST_REPO"
else
test_fail "Failed to create worktree for existing branch"
fi
# Test 4: Handle directory conflicts
test_start "Handle existing directory"
mkdir -p "../test-repo-conflict-branch"
echo "existing file" > "../test-repo-conflict-branch/existing.txt"
if gwt conflict-branch > /dev/null 2>&1; then
if [[ ! -f "../test-repo-conflict-branch/existing.txt" ]]; then
test_pass "Handled directory conflict"
else
test_fail "Did not clean up existing directory"
fi
cd "$TEST_REPO"
else
test_fail "Failed to handle directory conflict"
fi
# Test 5: List all worktrees
test_start "List multiple worktrees"
if output=$(gwt 2>&1); then
worktree_count=$(echo "$output" | grep -c "test-repo")
if [[ $worktree_count -ge 3 ]]; then
test_pass "Listed all worktrees ($worktree_count found)"
else
test_fail "Missing worktrees (only $worktree_count found)"
fi
else
test_fail "Failed to list worktrees"
fi
# Test 6: Cleanup orphaned directories
test_start "Clean orphaned directories"
# Create orphaned directory (must match cleanup patterns like *-feature*)
mkdir -p "../test-repo-feature-orphan"
echo "orphan" > "../test-repo-feature-orphan/file.txt"
# Run cleanup
if gwtclean > /dev/null 2>&1; then
if [[ ! -d "../test-repo-feature-orphan" ]]; then
test_pass "Cleaned orphaned directory"
else
test_fail "Orphaned directory still exists"
fi
else
test_fail "Cleanup command failed"
fi
# Test 7: Worktree with complex branch names
test_start "Complex branch names"
if gwt feature/user-auth/oauth-2.0 > /dev/null 2>&1; then
if [[ -d "../test-repo-feature-user-auth-oauth-2.0" ]]; then
test_pass "Handled complex branch name"
else
test_fail "Failed with complex branch name"
fi
cd "$TEST_REPO"
else
test_fail "Failed to create worktree with complex name"
fi
# Test 8: Switch to existing worktree
test_start "Switch to existing worktree"
if gwt feature/test-branch > /dev/null 2>&1; then
if [[ "$(basename "$(pwd)")" == "test-repo-feature-test-branch" ]]; then
test_pass "Switched to existing worktree"
else
test_fail "Did not switch to correct directory"
fi
cd "$TEST_REPO"
else
test_fail "Failed to switch to existing worktree"
fi
# Test 9: Error handling outside git repo
test_start "Error handling outside git repo"
# Create truly isolated non-git directory
NON_GIT_DIR="${TEST_DIR}/non-git-test"
mkdir -p "$NON_GIT_DIR"
cd "$NON_GIT_DIR"
if ! gwt test-branch 2>&1 | grep -q "Not in a git repository"; then
test_fail "Did not detect non-git directory"
else
test_pass "Correctly detected non-git directory"
fi
cd "$TEST_REPO"
# Test 10: Detailed list function
test_start "Detailed worktree list"
if output=$(gwtlist 2>&1); then
if echo "$output" | grep -q "Branch:"; then
test_pass "Detailed list shows branch info"
else
test_fail "Missing branch information"
fi
else
test_fail "List command failed"
fi
# Summary
echo
print_color "$BLUE" "==================================="
print_color "$BLUE" " Test Summary"
print_color "$BLUE" "==================================="
echo
print_color "$GREEN" "Passed: $TESTS_PASSED"
if [[ $TESTS_FAILED -gt 0 ]]; then
print_color "$RED" "Failed: $TESTS_FAILED"
exit 1
else
print_color "$GREEN" "Failed: 0"
echo
print_color "$GREEN" "✓ All tests passed!"
fi
echo