11#! /usr/bin/env bash
22# Tests for auto-tag-release.sh
33set -euo pipefail
4- trap ' echo "Error occurred at line $LINENO"; exit 1' ERR
54
6- SCRIPT_DIR=" $( cd " $( dirname " $0 " ) " ; pwd) "
7- PASS=0
8- FAIL=0
9-
10- run_test () {
11- local name=" $1 "
12- local expected_exit=" $2 "
13- local expected_output=" $3 "
14- shift 3
15-
16- local output exit_code
17- output=$( " $@ " 2>&1 ) && exit_code=0 || exit_code=$?
18-
19- if [ " $exit_code " -ne " $expected_exit " ]; then
20- echo " FAIL: $name — expected exit $expected_exit , got $exit_code "
21- echo " output: $output "
22- FAIL=$(( FAIL + 1 ))
23- return
24- fi
25-
26- if [ -n " $expected_output " ] && ! echo " $output " | grep -qF " $expected_output " ; then
27- echo " FAIL: $name — expected output containing: $expected_output "
28- echo " actual: $output "
29- FAIL=$(( FAIL + 1 ))
30- return
31- fi
32-
33- echo " PASS: $name "
34- PASS=$(( PASS + 1 ))
35- }
5+ cd " $( dirname " ${BASH_SOURCE[0]} " ) "
6+ SCRIPT_DIR=" $( pwd) "
7+ source ../test_helpers.sh
368
379# Set up a temporary bare repo to act as "origin" and a working clone.
3810TMPDIR=$( mktemp -d)
3911trap ' rm -rf "$TMPDIR"' EXIT
4012
41- git config user.email || git config --global user.email " test@test.com"
42- git config user.name || git config --global user.name " test"
43- git init --bare --initial-branch=main " $TMPDIR /origin.git"
44- git clone " $TMPDIR /origin.git" " $TMPDIR /work"
13+ git config user.email > /dev/null || git config --global user.email " test@test.com"
14+ git config user.name > /dev/null || git config --global user.name " test"
15+ git init --bare --initial-branch=main " $TMPDIR /origin.git" > /dev/null
16+ git clone " $TMPDIR /origin.git" " $TMPDIR /work" > /dev/null
4517cd " $TMPDIR /work"
46- git commit --allow-empty -m " initial"
47- git push origin main
18+ git commit --allow-empty -m " initial" > /dev/null
19+ git push origin main > /dev/null
4820
4921# =============================================
5022# parse_changelog unit tests
@@ -167,7 +139,7 @@ cat <<'EOF' > CHANGELOG.md
167139## [1.0.0] - 2026-01-01
168140EOF
169141
170- run_test " content under [Unreleased], untagged version" 1 " is not tagged" \
142+ expect_failure_output " content under [Unreleased], untagged version" " is not tagged" \
171143 env CHANGELOG_PATH=CHANGELOG.md " $SCRIPT_DIR /auto-tag-release.sh"
172144
173145# --- Empty [Unreleased], new version, no existing tag — should tag ---
@@ -177,11 +149,11 @@ cat <<'EOF' > CHANGELOG.md
177149## [1.0.0] - 2026-01-01
178150EOF
179151
180- run_test " creates new tag" 0 " Tagged v1.0.0 successfully" \
152+ expect_success_output " creates new tag" " Tagged v1.0.0 successfully" \
181153 env CHANGELOG_PATH=CHANGELOG.md " $SCRIPT_DIR /auto-tag-release.sh"
182154
183155# Verify the tag was actually created
184- if git rev-parse v1.0.0; then
156+ if git rev-parse v1.0.0 > /dev/null ; then
185157 echo " PASS: tag v1.0.0 exists"
186158 PASS=$(( PASS + 1 ))
187159else
190162fi
191163
192164# --- Tag already exists — should skip ---
193- run_test " tag already exists" 0 " already exists" \
165+ expect_success_output " tag already exists" " already exists" \
194166 env CHANGELOG_PATH=CHANGELOG.md " $SCRIPT_DIR /auto-tag-release.sh"
195167
196168# --- Content under [Unreleased], previous version already tagged — should pass ---
@@ -204,23 +176,23 @@ cat <<'EOF' > CHANGELOG.md
204176## [1.0.0] - 2026-01-01
205177EOF
206178
207- run_test " content under [Unreleased], tagged version" 0 " already tagged" \
179+ expect_success_output " content under [Unreleased], tagged version" " already tagged" \
208180 env CHANGELOG_PATH=CHANGELOG.md " $SCRIPT_DIR /auto-tag-release.sh"
209181
210182# --- No version sections at all — should skip ---
211183cat << 'EOF ' > CHANGELOG.md
212184## [Unreleased]
213185EOF
214186
215- run_test " no released version" 0 " No released version" \
187+ expect_success_output " no released version" " No released version found " \
216188 env CHANGELOG_PATH=CHANGELOG.md " $SCRIPT_DIR /auto-tag-release.sh"
217189
218190# --- No [Unreleased] section — should skip ---
219191cat << 'EOF ' > CHANGELOG.md
220192## [2.0.0] - 2026-01-01
221193EOF
222194
223- run_test " no [Unreleased] section" 0 " No released version " \
195+ expect_success " no [Unreleased] section" \
224196 env CHANGELOG_PATH=CHANGELOG.md " $SCRIPT_DIR /auto-tag-release.sh"
225197
226198# --- [Unreleased] with only blank lines (no content) — should tag ---
@@ -232,10 +204,7 @@ cat <<'EOF' > CHANGELOG.md
232204## [2.0.0] - 2026-01-01
233205EOF
234206
235- run_test " blank lines under [Unreleased]" 0 " Tagged v2.0.0 successfully " \
207+ expect_success " blank lines under [Unreleased]" \
236208 env CHANGELOG_PATH=CHANGELOG.md " $SCRIPT_DIR /auto-tag-release.sh"
237209
238- # --- Summary ---
239- echo " "
240- echo " Results: $PASS passed, $FAIL failed"
241- [ " $FAIL " -eq 0 ] || exit 1
210+ print_results
0 commit comments