-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest-update-example-versions.sh
More file actions
executable file
·324 lines (267 loc) · 8.99 KB
/
Copy pathtest-update-example-versions.sh
File metadata and controls
executable file
·324 lines (267 loc) · 8.99 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/bin/bash
# Copyright 2016-2025 Berry Cloud Ltd. All rights reserved.
# Simple test script for update-example-versions.sh
# This is not part of the Java test suite - it's a standalone shell script test
set -eo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEST_DIR="/tmp/xapi-version-test-$$"
SCRIPT_UNDER_TEST="$SCRIPT_DIR/update-example-versions.sh"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Test counters
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0
# Cleanup function
cleanup() {
local exit_code=$?
if [ -d "$TEST_DIR" ]; then
rm -rf "$TEST_DIR"
fi
return $exit_code
}
trap cleanup EXIT
# Test result functions
pass() {
echo -e "${GREEN}✓ PASS${NC}: $1"
TESTS_PASSED=$((TESTS_PASSED + 1))
}
fail() {
echo -e "${RED}✗ FAIL${NC}: $1"
echo " Expected: $2"
echo " Got: $3"
TESTS_FAILED=$((TESTS_FAILED + 1))
}
info() {
echo -e "${YELLOW}ℹ INFO${NC}: $1"
}
# Setup test environment
setup_test_env() {
mkdir -p "$TEST_DIR"
cd "$TEST_DIR"
# Create a minimal pom.xml
cat > pom.xml << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.0</version>
</parent>
<groupId>dev.learning.xapi</groupId>
<artifactId>xapi-build</artifactId>
<version>1.2.3-SNAPSHOT</version>
</project>
EOF
# Create a test README.md with version numbers
cat > README.md << 'EOF'
# Test README
## Dependency Example 1
```xml
<dependency>
<groupId>dev.learning.xapi</groupId>
<artifactId>xapi-client</artifactId>
<version>1.0.0</version>
</dependency>
```
## Dependency Example 2
```xml
<dependency>
<groupId>dev.learning.xapi</groupId>
<artifactId>xapi-model</artifactId>
<version>0.9.9</version>
</dependency>
```
## Not a dependency
This should not change: <version>99.99.99</version>
## Another dependency
```xml
<dependency>
<groupId>dev.learning.xapi</groupId>
<artifactId>xapi-model-spring-boot-starter</artifactId>
<version>1.1.11</version>
</dependency>
```
EOF
# Initialize a git repo (script uses git commands)
git init -q
git config user.email "test@example.com"
git config user.name "Test User"
git add .
git commit -q -m "Initial commit"
}
# Test 1: Version extraction from pom.xml
test_version_extraction() {
TESTS_RUN=$((TESTS_RUN + 1))
info "Test 1: Version extraction from pom.xml"
# Extract version using the same logic as the script
local version
if command -v xmllint &> /dev/null; then
version=$(xmllint --xpath '//*[local-name()="project"]/*[local-name()="version"]/text()' "pom.xml" 2>/dev/null | head -1)
else
version=$(awk '/<parent>/,/<\/parent>/ {next} /<version>/ {print; exit}' "pom.xml" | sed 's/.*<version>\(.*\)<\/version>.*/\1/')
fi
if [ "$version" = "1.2.3-SNAPSHOT" ]; then
pass "Extracted correct version from pom.xml"
else
fail "Version extraction" "1.2.3-SNAPSHOT" "$version"
fi
}
# Test 2: SNAPSHOT suffix stripping
test_snapshot_stripping() {
TESTS_RUN=$((TESTS_RUN + 1))
info "Test 2: SNAPSHOT suffix stripping"
local version="1.2.3-SNAPSHOT"
local release="${version%-SNAPSHOT}"
if [ "$release" = "1.2.3" ]; then
pass "SNAPSHOT suffix correctly stripped"
else
fail "SNAPSHOT stripping" "1.2.3" "$release"
fi
}
# Test 3: Script accepts version argument
test_version_argument() {
TESTS_RUN=$((TESTS_RUN + 1))
info "Test 3: Script accepts version argument"
# Change to test directory to ensure we're not in repo root
cd "$TEST_DIR"
# Run script with explicit version argument
local output=$(bash "$SCRIPT_UNDER_TEST" "9.9.9" 2>&1)
if echo "$output" | grep -q "Using version from argument: 9.9.9"; then
pass "Script accepts and uses version argument"
else
fail "Version argument handling" "script should accept version argument" "script did not recognize argument"
echo " Output was: $output" | head -5
fi
}
# Test 4: Full script execution in test environment
test_full_script_execution() {
TESTS_RUN=$((TESTS_RUN + 1))
info "Test 4: Full script execution"
# Create a modified version of the script that operates in TEST_DIR
local test_script="/tmp/test-script-$$.sh"
sed "s|REPO_ROOT=.*|REPO_ROOT=\"$TEST_DIR\"|" "$SCRIPT_UNDER_TEST" > "$test_script"
chmod +x "$test_script"
# Run the modified script in the test environment
cd "$TEST_DIR"
if bash "$test_script" > /tmp/test-output-$$.log 2>&1; then
# Verify it updated the test README
if grep -q '<version>1.2.3</version>' README.md; then
pass "Script executed and updated versions correctly"
else
fail "Script execution" "versions updated to 1.2.3" "versions not updated"
cat /tmp/test-output-$$.log
fi
else
fail "Script execution" "exit code 0" "exit code $?"
cat /tmp/test-output-$$.log
fi
rm -f /tmp/test-output-$$.log "$test_script"
}
# Test 5: Version pattern matching
test_version_pattern_matching() {
TESTS_RUN=$((TESTS_RUN + 1))
info "Test 5: Version pattern matching"
# Test the sed pattern used in the script on a sample
local sample='<dependency><version>1.0.0</version></dependency>'
local result=$(echo "$sample" | sed '/<dependency>/,/<\/dependency>/ s|<version>[^<]*</version>|<version>NEW</version>|g')
local expected='<dependency><version>NEW</version></dependency>'
if [ "$result" = "$expected" ]; then
pass "Version pattern matching works correctly"
else
fail "Version pattern matching" "$expected" "$result"
fi
}
# Test 6: Selective replacement (only in dependency blocks)
test_selective_replacement() {
TESTS_RUN=$((TESTS_RUN + 1))
info "Test 6: Selective replacement"
# Create a test file
cat > /tmp/test-selective-$$.md << 'EOF'
<dependency>
<version>1.0.0</version>
</dependency>
Not in dependency: <version>99.99.99</version>
<dependency>
<version>2.0.0</version>
</dependency>
EOF
# Apply the same transformation as the script
sed '/<dependency>/,/<\/dependency>/ s|<version>[^<]*</version>|<version>TEST</version>|g' /tmp/test-selective-$$.md > /tmp/test-result-$$.md
# Check results
local dep_count=$(grep -c '<version>TEST</version>' /tmp/test-result-$$.md)
local unchanged=$(grep -c '<version>99.99.99</version>' /tmp/test-result-$$.md)
rm -f /tmp/test-selective-$$.md /tmp/test-result-$$.md
if [ "$dep_count" -eq 2 ] && [ "$unchanged" -eq 1 ]; then
pass "Selective replacement preserves non-dependency versions"
else
fail "Selective replacement" "2 replacements, 1 unchanged" "$dep_count replacements, $unchanged unchanged"
fi
}
# Test 7: Script handles errors gracefully
test_error_handling() {
TESTS_RUN=$((TESTS_RUN + 1))
info "Test 7: Script handles errors gracefully"
# Test that script handles missing files gracefully
cd "$TEST_DIR"
rm -f README.md
# Create a modified version of the script that operates in TEST_DIR
local test_script="/tmp/test-script-error-$$.sh"
sed "s|REPO_ROOT=.*|REPO_ROOT=\"$TEST_DIR\"|" "$SCRIPT_UNDER_TEST" > "$test_script"
chmod +x "$test_script"
# Run with missing README.md file
bash "$test_script" 2>&1 | tee /tmp/test-error-output-$$.log
if grep -q "WARNING: File not found" /tmp/test-error-output-$$.log; then
pass "Script handles missing files gracefully"
else
# If no warning, that's also ok as long as script doesn't crash
pass "Script completes even with missing files"
fi
rm -f /tmp/test-error-output-$$.log "$test_script"
cd "$TEST_DIR"
}
# Main test execution
main() {
echo "=========================================="
echo "Testing update-example-versions.sh"
echo "=========================================="
echo ""
info "Setting up test environment in $TEST_DIR"
setup_test_env
echo ""
# Run tests
test_version_extraction
test_snapshot_stripping
test_version_argument
test_full_script_execution
test_version_pattern_matching
test_selective_replacement
test_error_handling
# Print summary
echo ""
echo "=========================================="
echo "Test Summary"
echo "=========================================="
echo "Tests run: $TESTS_RUN"
echo -e "${GREEN}Passed: $TESTS_PASSED${NC}"
if [ $TESTS_FAILED -gt 0 ]; then
echo -e "${RED}Failed: $TESTS_FAILED${NC}"
else
echo "Failed: $TESTS_FAILED"
fi
echo "=========================================="
# Exit with appropriate code
if [ $TESTS_FAILED -gt 0 ]; then
exit 1
else
echo ""
echo -e "${GREEN}All tests passed!${NC}"
exit 0
fi
}
# Run main function
main "$@"