-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-bump-version.sh
More file actions
131 lines (120 loc) · 5.11 KB
/
Copy pathtest-bump-version.sh
File metadata and controls
131 lines (120 loc) · 5.11 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
#!/usr/bin/env bash
# Quick manual test for bump-version.sh logic
# Run: bash test-bump-version.sh
set -euo pipefail
SCRIPT="$(cd "$(dirname "$0")" && pwd)/scripts/bump-version.sh"
log() { echo " $*"; }
pass() { log "PASS"; }
fail() { log "FAIL: $*"; FAILED=1; }
FAILED=0
# ── Helper: set up a mock environment ─────────────────────────────────────────
prep() {
local D
D="$(mktemp -d)"
mkdir -p "$D/bin" "$D/.git"
cat > "$D/bin/gh" <<'MOCKEOF'
#!/usr/bin/env bash
mkdir -p "$(dirname "$GH_CALLS")"
echo "$@" >> "$GH_CALLS"
# Mock gh pr list — return a PR URL so pr_exists thinks a PR is found
case "$*" in
*pr\ list*)
mkdir -p "/tmp/gh-mock"
if [ -f "/tmp/gh-mock/force_pr" ]; then
echo '["https://github.com/user/repo/pull/42 - v1.17.4"]'
else
echo ""
fi
;;
esac
MOCKEOF
chmod +x "$D/bin/gh"
# Also mock git to allow reset during gh workflow
cat > "$D/bin/git" <<'EOF'
#!/usr/bin/env bash
# Allow any git command
exit 0
EOF
chmod +x "$D/bin/git"
echo "$D"
}
# ── Test 1: Create a new PR (no existing PR) ──────────────────────────────────
log "Test 1: Create a new PR (no existing PR)..."
S1="$(prep)"
echo "1.17.0" > "$S1/version.txt"
pushd "$S1" >/dev/null
git init -q && git add version.txt && git commit -q --allow-empty -m "init"
GH_CALLS="$S1/calls.txt" GH_TOKEN="mock" \
PATH="$S1/bin:$PATH" bash "$SCRIPT" "1.17.4" >/dev/null 2>&1
popd >/dev/null
c1=$(grep -c 'pr create' "$S1/calls.txt" 2>/dev/null || echo 0)
if [ "$c1" -ge 1 ]; then pass; else fail "no gh pr create captured"; fi
rm -rf "$S1"
# ── Test 2: Skip when version already current ─────────────────────────────────
log "Test 2: gh pr list runs for all bumps (skip logic is in GH Action)..."
S2="$(prep)"
echo "1.17.4" > "$S2/version.txt"
pushd "$S2" >/dev/null
git init -q && git add version.txt && git commit -q --allow-empty -m "init"
GH_CALLS="$S2/calls.txt" GH_TOKEN="mock" \
PATH="$S2/bin:$PATH" bash "$SCRIPT" "1.17.4" >/dev/null 2>&1
popd >/dev/null
has_list=$(grep -c 'pr list' "$S2/calls.txt" 2>/dev/null || echo 0)
if [ "$has_list" -ge 1 ]; then pass; else fail "expected gh pr list"; fi
rm -rf "$S2"
# ── Test 3: Version format — strips leading v ─────────────────────────────────
log "Test 3: Version format — no double-v (v1.17.4 → v1.17.4)..."
S3="$(prep)"
echo "1.17.0" > "$S3/version.txt"
pushd "$S3" >/dev/null
git init -q && git add version.txt && git commit -q --allow-empty -m "init"
GH_CALLS="$S3/calls.txt" GH_TOKEN="mock" \
PATH="$S3/bin:$PATH" bash "$SCRIPT" "v1.17.4" >/dev/null 2>&1
popd >/dev/null
if grep -q 'pr/opencode-release/vv' "$S3/calls.txt" 2>/dev/null; then
fail "double-v found in calls"
else
pass
fi
rm -rf "$S3"
# ── Test 4: Version format — pre-release tags ─────────────────────────────────
log "Test 4: Version format — pre-release (1.18.0-beta.1)..."
S4="$(prep)"
echo "1.17.0" > "$S4/version.txt"
pushd "$S4" >/dev/null
git init -q && git add version.txt && git commit -q --allow-empty -m "init"
GH_CALLS="$S4/calls.txt" GH_TOKEN="mock" \
PATH="$S4/bin:$PATH" bash "$SCRIPT" "1.18.0-beta.1" >/dev/null 2>&1
popd >/dev/null
c4=$(grep -c 'pr create' "$S4/calls.txt" 2>/dev/null || echo 0)
if [ "$c4" -ge 1 ] && grep -q '1.18.0-beta.1' "$S4/calls.txt"; then pass; else fail "bad pre-release handling"; fi
rm -rf "$S4"
# ── Test 5: Error on empty version ───────────────────────────────────────────
log "Test 5: Error on empty version string..."
S5="$(prep)"
err=$(GH_TOKEN="mock" PATH="$S5/bin:$PATH" bash "$SCRIPT" "" 2>&1 || true)
if echo "$err" | grep -qi 'usage'; then pass; else fail "no usage error, got: $err"; fi
rm -rf "$S5"
# ── Test 6: Update existing PR ───────────────────────────────────────────────
log "Test 6: Update existing PR (force gh pr list to return PR)..."
S6="$(prep)"
echo "1.17.0" > "$S6/version.txt"
pushd "$S6" >/dev/null
git init -q && git add version.txt && git commit -q --allow-empty -m "init"
mkdir -p /tmp/gh-mock
echo "1" > /tmp/gh-mock/force_pr
GH_CALLS="$S6/calls.txt" GH_TOKEN="mock" \
PATH="$S6/bin:$PATH" bash "$SCRIPT" "1.17.4" >/dev/null 2>&1
popd >/dev/null
c6=$(grep -c 'pr edit' "$S6/calls.txt" 2>/dev/null || echo 0)
rm -f /tmp/gh-mock/force_pr
if [ "$c6" -ge 1 ]; then pass; else fail "expected 'gh pr edit', got: $(cat "$S6/calls.txt" 2>/dev/null || echo "empty")"; fi
rm -rf "$S6"
# ── Summary ──────────────────────────────────────────────────────────────────
echo ""
if [ "${FAILED:-0}" = "0" ]; then
echo "All tests passed."
else
echo "Some tests FAILED." >&2
exit 1
fi