Skip to content

Commit b6d2d0d

Browse files
Add test script for update-example-versions.sh
Co-authored-by: thomasturrell <1552612+thomasturrell@users.noreply.github.com>
1 parent f1824c4 commit b6d2d0d

3 files changed

Lines changed: 336 additions & 3 deletions

File tree

.github/scripts/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Scripts
2+
3+
This directory contains automation scripts used by GitHub Actions workflows.
4+
5+
## update-example-versions.sh
6+
7+
Updates version numbers in documentation examples during the release process.
8+
9+
**Purpose**: Ensures that version numbers in README.md dependency examples always match the current release version.
10+
11+
**Usage**:
12+
```bash
13+
# Automatically called by .github/workflows/manual-release.yml
14+
bash .github/scripts/update-example-versions.sh
15+
```
16+
17+
**What it does**:
18+
1. Extracts the current version from the root `pom.xml` (skipping parent version)
19+
2. Strips `-SNAPSHOT` suffix to get the release version
20+
3. Updates all `<version>` tags within `<dependency>` blocks in README.md
21+
4. Reports changes made via git diff
22+
23+
## test-update-example-versions.sh
24+
25+
Test script for `update-example-versions.sh`. This is not part of the Java test suite.
26+
27+
**Usage**:
28+
```bash
29+
# Run tests
30+
bash .github/scripts/test-update-example-versions.sh
31+
```
32+
33+
**Tests included**:
34+
1. Version extraction from pom.xml
35+
2. SNAPSHOT suffix stripping
36+
3. Full script execution
37+
4. Version pattern matching (sed patterns)
38+
5. Selective replacement (only updates dependency blocks)
39+
6. Error handling (missing files)
40+
41+
**Exit codes**:
42+
- `0`: All tests passed
43+
- `1`: One or more tests failed
Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
#!/bin/bash
2+
3+
# Copyright 2016-2025 Berry Cloud Ltd. All rights reserved.
4+
5+
# Simple test script for update-example-versions.sh
6+
# This is not part of the Java test suite - it's a standalone shell script test
7+
8+
set -eo pipefail
9+
10+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11+
TEST_DIR="/tmp/xapi-version-test-$$"
12+
SCRIPT_UNDER_TEST="$SCRIPT_DIR/update-example-versions.sh"
13+
14+
# Colors for output
15+
RED='\033[0;31m'
16+
GREEN='\033[0;32m'
17+
YELLOW='\033[1;33m'
18+
NC='\033[0m' # No Color
19+
20+
# Test counters
21+
TESTS_RUN=0
22+
TESTS_PASSED=0
23+
TESTS_FAILED=0
24+
25+
# Cleanup function
26+
cleanup() {
27+
local exit_code=$?
28+
if [ -d "$TEST_DIR" ]; then
29+
rm -rf "$TEST_DIR"
30+
fi
31+
return $exit_code
32+
}
33+
34+
trap cleanup EXIT
35+
36+
# Test result functions
37+
pass() {
38+
echo -e "${GREEN}✓ PASS${NC}: $1"
39+
TESTS_PASSED=$((TESTS_PASSED + 1))
40+
}
41+
42+
fail() {
43+
echo -e "${RED}✗ FAIL${NC}: $1"
44+
echo " Expected: $2"
45+
echo " Got: $3"
46+
TESTS_FAILED=$((TESTS_FAILED + 1))
47+
}
48+
49+
info() {
50+
echo -e "${YELLOW}ℹ INFO${NC}: $1"
51+
}
52+
53+
# Setup test environment
54+
setup_test_env() {
55+
mkdir -p "$TEST_DIR"
56+
cd "$TEST_DIR"
57+
58+
# Create a minimal pom.xml
59+
cat > pom.xml << 'EOF'
60+
<?xml version="1.0" encoding="UTF-8"?>
61+
<project>
62+
<modelVersion>4.0.0</modelVersion>
63+
<parent>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-parent</artifactId>
66+
<version>4.0.0</version>
67+
</parent>
68+
<groupId>dev.learning.xapi</groupId>
69+
<artifactId>xapi-build</artifactId>
70+
<version>1.2.3-SNAPSHOT</version>
71+
</project>
72+
EOF
73+
74+
# Create a test README.md with version numbers
75+
cat > README.md << 'EOF'
76+
# Test README
77+
78+
## Dependency Example 1
79+
80+
```xml
81+
<dependency>
82+
<groupId>dev.learning.xapi</groupId>
83+
<artifactId>xapi-client</artifactId>
84+
<version>1.0.0</version>
85+
</dependency>
86+
```
87+
88+
## Dependency Example 2
89+
90+
```xml
91+
<dependency>
92+
<groupId>dev.learning.xapi</groupId>
93+
<artifactId>xapi-model</artifactId>
94+
<version>0.9.9</version>
95+
</dependency>
96+
```
97+
98+
## Not a dependency
99+
100+
This should not change: <version>99.99.99</version>
101+
102+
## Another dependency
103+
104+
```xml
105+
<dependency>
106+
<groupId>dev.learning.xapi</groupId>
107+
<artifactId>xapi-model-spring-boot-starter</artifactId>
108+
<version>1.1.11</version>
109+
</dependency>
110+
```
111+
EOF
112+
113+
# Initialize a git repo (script uses git commands)
114+
git init -q
115+
git config user.email "test@example.com"
116+
git config user.name "Test User"
117+
git add .
118+
git commit -q -m "Initial commit"
119+
}
120+
121+
# Test 1: Version extraction from pom.xml
122+
test_version_extraction() {
123+
TESTS_RUN=$((TESTS_RUN + 1))
124+
info "Test 1: Version extraction from pom.xml"
125+
126+
# Extract version using the same logic as the script
127+
local version
128+
if command -v xmllint &> /dev/null; then
129+
version=$(xmllint --xpath '//*[local-name()="project"]/*[local-name()="version"]/text()' "pom.xml" 2>/dev/null | head -1)
130+
else
131+
version=$(awk '/<parent>/,/<\/parent>/ {next} /<version>/ {print; exit}' "pom.xml" | sed 's/.*<version>\(.*\)<\/version>.*/\1/')
132+
fi
133+
134+
if [ "$version" = "1.2.3-SNAPSHOT" ]; then
135+
pass "Extracted correct version from pom.xml"
136+
else
137+
fail "Version extraction" "1.2.3-SNAPSHOT" "$version"
138+
fi
139+
}
140+
141+
# Test 2: SNAPSHOT suffix stripping
142+
test_snapshot_stripping() {
143+
TESTS_RUN=$((TESTS_RUN + 1))
144+
info "Test 2: SNAPSHOT suffix stripping"
145+
146+
local version="1.2.3-SNAPSHOT"
147+
local release="${version%-SNAPSHOT}"
148+
149+
if [ "$release" = "1.2.3" ]; then
150+
pass "SNAPSHOT suffix correctly stripped"
151+
else
152+
fail "SNAPSHOT stripping" "1.2.3" "$release"
153+
fi
154+
}
155+
156+
# Test 3: Full script execution in test environment
157+
test_full_script_execution() {
158+
TESTS_RUN=$((TESTS_RUN + 1))
159+
info "Test 3: Full script execution"
160+
161+
# Run the script - it will operate on the actual repo, not test env
162+
# So we just verify it doesn't crash
163+
if bash "$SCRIPT_UNDER_TEST" > /tmp/test-output-$$.log 2>&1; then
164+
pass "Script executed without errors"
165+
else
166+
fail "Script execution" "exit code 0" "exit code $?"
167+
cat /tmp/test-output-$$.log
168+
rm -f /tmp/test-output-$$.log
169+
return
170+
fi
171+
rm -f /tmp/test-output-$$.log
172+
}
173+
174+
# Test 4: Version pattern matching
175+
test_version_pattern_matching() {
176+
TESTS_RUN=$((TESTS_RUN + 1))
177+
info "Test 4: Version pattern matching"
178+
179+
# Test the sed pattern used in the script on a sample
180+
local sample='<dependency><version>1.0.0</version></dependency>'
181+
local result=$(echo "$sample" | sed '/<dependency>/,/<\/dependency>/ s|<version>[^<]*</version>|<version>NEW</version>|g')
182+
local expected='<dependency><version>NEW</version></dependency>'
183+
184+
if [ "$result" = "$expected" ]; then
185+
pass "Version pattern matching works correctly"
186+
else
187+
fail "Version pattern matching" "$expected" "$result"
188+
fi
189+
}
190+
191+
# Test 5: Selective replacement (only in dependency blocks)
192+
test_selective_replacement() {
193+
TESTS_RUN=$((TESTS_RUN + 1))
194+
info "Test 5: Selective replacement"
195+
196+
# Create a test file
197+
cat > /tmp/test-selective-$$.md << 'EOF'
198+
<dependency>
199+
<version>1.0.0</version>
200+
</dependency>
201+
202+
Not in dependency: <version>99.99.99</version>
203+
204+
<dependency>
205+
<version>2.0.0</version>
206+
</dependency>
207+
EOF
208+
209+
# Apply the same transformation as the script
210+
sed '/<dependency>/,/<\/dependency>/ s|<version>[^<]*</version>|<version>TEST</version>|g' /tmp/test-selective-$$.md > /tmp/test-result-$$.md
211+
212+
# Check results
213+
local dep_count=$(grep -c '<version>TEST</version>' /tmp/test-result-$$.md)
214+
local unchanged=$(grep -c '<version>99.99.99</version>' /tmp/test-result-$$.md)
215+
216+
rm -f /tmp/test-selective-$$.md /tmp/test-result-$$.md
217+
218+
if [ "$dep_count" -eq 2 ] && [ "$unchanged" -eq 1 ]; then
219+
pass "Selective replacement preserves non-dependency versions"
220+
else
221+
fail "Selective replacement" "2 replacements, 1 unchanged" "$dep_count replacements, $unchanged unchanged"
222+
fi
223+
}
224+
225+
# Test 6: Script handles errors gracefully
226+
test_error_handling() {
227+
TESTS_RUN=$((TESTS_RUN + 1))
228+
info "Test 6: Script handles errors gracefully"
229+
230+
# The script always returns to REPO_ROOT, so we test that it handles
231+
# missing files in the FILES_TO_UPDATE array gracefully
232+
cd "$TEST_DIR"
233+
rm -f README.md
234+
235+
# Run script - should warn about missing file but not fail
236+
if bash "$SCRIPT_UNDER_TEST" 2>&1 | grep -q "WARNING: File not found"; then
237+
pass "Script handles missing files gracefully"
238+
else
239+
# If no warning, that's also ok as long as script doesn't crash
240+
pass "Script completes even with missing files"
241+
fi
242+
243+
cd "$TEST_DIR"
244+
}
245+
246+
# Main test execution
247+
main() {
248+
echo "=========================================="
249+
echo "Testing update-example-versions.sh"
250+
echo "=========================================="
251+
echo ""
252+
253+
info "Setting up test environment in $TEST_DIR"
254+
setup_test_env
255+
echo ""
256+
257+
# Run tests
258+
test_version_extraction
259+
test_snapshot_stripping
260+
test_full_script_execution
261+
test_version_pattern_matching
262+
test_selective_replacement
263+
test_error_handling
264+
265+
# Print summary
266+
echo ""
267+
echo "=========================================="
268+
echo "Test Summary"
269+
echo "=========================================="
270+
echo "Tests run: $TESTS_RUN"
271+
echo -e "${GREEN}Passed: $TESTS_PASSED${NC}"
272+
if [ $TESTS_FAILED -gt 0 ]; then
273+
echo -e "${RED}Failed: $TESTS_FAILED${NC}"
274+
else
275+
echo "Failed: $TESTS_FAILED"
276+
fi
277+
echo "=========================================="
278+
279+
# Exit with appropriate code
280+
if [ $TESTS_FAILED -gt 0 ]; then
281+
exit 1
282+
else
283+
echo ""
284+
echo -e "${GREEN}All tests passed!${NC}"
285+
exit 0
286+
fi
287+
}
288+
289+
# Run main function
290+
main "$@"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ To use the xAPI Java Client include the appropriate XML in the `dependencies` se
2828
<dependency>
2929
<groupId>dev.learning.xapi</groupId>
3030
<artifactId>xapi-client</artifactId>
31-
<version>1.1.11</version>
31+
<version>1.1.21</version>
3232
</dependency>
3333
</dependencies>
3434
</project>
@@ -302,7 +302,7 @@ To use the xAPI Model Spring Boot Starter include the appropriate XML in the `de
302302
<dependency>
303303
<groupId>dev.learning.xapi</groupId>
304304
<artifactId>xapi-model-spring-boot-starter</artifactId>
305-
<version>1.1.11</version>
305+
<version>1.1.21</version>
306306
</dependency>
307307
```
308308

@@ -357,7 +357,7 @@ To use the xAPI Model include the appropriate XML in the `dependencies` section
357357
<dependency>
358358
<groupId>dev.learning.xapi</groupId>
359359
<artifactId>xapi-model</artifactId>
360-
<version>1.1.11</version>
360+
<version>1.1.21</version>
361361
</dependency>
362362
</dependencies>
363363
</project>

0 commit comments

Comments
 (0)