blob: 5ebde6c3cd02bc48d939c8aa6589a5c1bf499bbe (
plain)
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
|
#!/bin/bash
# Integration test script for gitsyncer
# This script runs all tests in sequence and reports results
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Get script directory
TEST_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(dirname "$TEST_DIR")"
# Function to print test headers
print_test() {
echo -e "\n${YELLOW}=== $1 ===${NC}"
}
# Function to print success
print_success() {
echo -e "${GREEN}✓ $1${NC}"
}
# Function to print failure
print_failure() {
echo -e "${RED}✗ $1${NC}"
}
# Start testing
echo "GitSyncer Integration Tests"
echo "============================"
# Clean up any previous test artifacts
print_test "Cleaning up previous test artifacts"
cd "$TEST_DIR"
rm -rf repos work work-* test-config.json
print_success "Cleanup complete"
# Build the project
print_test "Building gitsyncer"
cd "$PROJECT_ROOT"
if go build -o gitsyncer ./cmd/gitsyncer; then
print_success "Build successful"
else
print_failure "Build failed"
exit 1
fi
# Test 1: Version command
print_test "Test 1: Version command"
if ./gitsyncer version | grep -q "gitsyncer version"; then
print_success "Version command works"
else
print_failure "Version command failed"
exit 1
fi
# Test 2: Setup test repositories
print_test "Test 2: Setting up test repositories"
cd "$TEST_DIR"
if ./setup_test_repos.sh > /dev/null 2>&1; then
print_success "Test repositories created"
echo " - org1: Has main, develop, and feature/test-feature branches"
echo " - org2: Has only main branch"
else
print_failure "Failed to setup test repositories"
exit 1
fi
# Test 3: List organizations
print_test "Test 3: List organizations"
cd "$PROJECT_ROOT"
if ./gitsyncer --config test/test-config.json list orgs | grep -q "org1"; then
print_success "Organizations listed successfully"
else
print_failure "Failed to list organizations"
exit 1
fi
# Test 4: Initial sync
print_test "Test 4: Initial repository sync"
rm -rf test/work
if ./gitsyncer --config test/test-config.json sync repo test-repo --work-dir test/work > /dev/null 2>&1; then
print_success "Initial sync completed"
# Verify all branches are synced
cd "$TEST_DIR"
ORG2_BRANCHES=$(git ls-remote file://$(pwd)/repos/org2/test-repo.git | grep refs/heads | wc -l)
if [ "$ORG2_BRANCHES" -eq "3" ]; then
print_success "All branches synced to org2 (found $ORG2_BRANCHES branches)"
else
print_failure "Branch sync failed - expected 3 branches, found $ORG2_BRANCHES"
exit 1
fi
else
print_failure "Initial sync failed"
exit 1
fi
# Test 5: Sync with no changes (idempotent)
print_test "Test 5: Idempotent sync (no changes)"
cd "$PROJECT_ROOT"
rm -rf test/work2
if ./gitsyncer --config test/test-config.json sync repo test-repo --work-dir test/work2 > /dev/null 2>&1; then
print_success "Idempotent sync successful"
else
print_failure "Idempotent sync failed"
exit 1
fi
# Test 6: Create changes and sync
print_test "Test 6: Sync with new changes"
cd "$TEST_DIR"
# Create a change in org1
WORK_DIR="$TEST_DIR/repos/change-work"
rm -rf "$WORK_DIR"
mkdir -p "$WORK_DIR"
cd "$WORK_DIR"
git clone -q file://"$TEST_DIR"/repos/org1/test-repo.git
cd test-repo
echo "New feature added" > feature2.txt
git add feature2.txt
git commit -q -m "Add feature2.txt"
git push -q origin main
# Run sync
cd "$PROJECT_ROOT"
rm -rf test/work3
if ./gitsyncer --config test/test-config.json sync repo test-repo --work-dir test/work3 > /dev/null 2>&1; then
print_success "Sync with changes successful"
# Verify change is in org2
cd "$TEST_DIR"
if git ls-remote file://$(pwd)/repos/org2/test-repo.git HEAD | grep -q "$(git ls-remote file://$(pwd)/repos/org1/test-repo.git HEAD | cut -f1)"; then
print_success "Changes propagated to org2"
else
print_failure "Changes not propagated to org2"
exit 1
fi
else
print_failure "Sync with changes failed"
exit 1
fi
# Clean up work directory
rm -rf "$WORK_DIR"
# Test 7: Conflict detection
print_test "Test 7: Merge conflict detection"
cd "$TEST_DIR"
if ./test_conflict.sh > /dev/null 2>&1; then
print_success "Conflicting changes created"
else
print_failure "Failed to create conflicting changes"
exit 1
fi
# Try to sync with conflicts (should fail)
cd "$PROJECT_ROOT"
if ./gitsyncer --config test/test-config.json --sync test-repo --work-dir test/work-conflict > /dev/null 2>&1; then
print_failure "Sync should have failed with conflicts but didn't"
exit 1
else
print_success "Conflict detection working - sync correctly failed"
fi
# Test 8: Missing config file
print_test "Test 8: Missing config file handling"
if ./gitsyncer --config test/nonexistent.json sync repo test-repo 2>&1 | grep -q "Error loading configuration"; then
print_success "Missing config file handled correctly"
else
print_failure "Missing config file not handled properly"
exit 1
fi
# Test 9: Empty organization list
print_test "Test 9: Invalid configuration handling"
cd "$TEST_DIR"
echo '{"organizations": []}' > empty-config.json
cd "$PROJECT_ROOT"
if ./gitsyncer --config test/empty-config.json sync repo test-repo 2>&1 | grep -q "invalid configuration: no organizations configured"; then
print_success "Empty organization list handled correctly"
else
print_failure "Empty organization list not handled properly"
exit 1
fi
# Test 10: Multiple repository configuration
print_test "Test 10: Multiple repository sync"
cd "$TEST_DIR"
./test_multi_repo.sh > /dev/null 2>&1
cd "$PROJECT_ROOT"
# Test list-repos
if ./gitsyncer --config test/multi-repo-config.json list repos | grep -q "repo1"; then
print_success "Repository listing works"
else
print_failure "Failed to list repositories"
exit 1
fi
# Test sync-all
if ./gitsyncer --config test/multi-repo-config.json sync all --work-dir test/work-multi > /dev/null 2>&1; then
print_success "Multiple repository sync completed"
else
print_failure "Multiple repository sync failed"
exit 1
fi
# Cleanup test artifacts
print_test "Cleaning up test artifacts"
cd "$TEST_DIR"
rm -rf repos work work-* empty-config.json multi-repo-config.json
print_success "Cleanup complete"
# Summary
echo -e "\n${GREEN}=== All tests passed! ===${NC}"
echo "GitSyncer is working correctly and can:"
echo " ✓ Sync repositories between multiple organizations"
echo " ✓ Handle all branches automatically"
echo " ✓ Merge changes from all remotes"
echo " ✓ Detect and report merge conflicts"
echo " ✓ Handle configuration errors gracefully"
echo " ✓ Sync multiple repositories with --sync-all"
echo " ✓ Handle missing remote repositories gracefully"
|