Skip to content

Commit 6bb1821

Browse files
committed
fix(tests): support skipped tests in cozytest
Assisted-By: GPT-5 <[email protected]> Signed-off-by: immanuwell <[email protected]>
1 parent 3c780ac commit 6bb1821

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎hack/cozytest-skip.bats‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bats
2+
# Behavioral coverage for Bats-compatible skip handling in hack/cozytest.sh.
3+
4+
HACK_DIR="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")" && pwd)"
5+
RUNNER="$HACK_DIR/cozytest.sh"
6+
7+
@test "skip stops the current test and keeps the suite green" {
8+
dir=$(mktemp -d)
9+
fixture="$dir/fixture.bats"
10+
printf '%s\n' \
11+
'@test "skipped fixture" {' \
12+
' skip "not applicable"' \
13+
' echo SHOULD_NOT_RUN' \
14+
' false' \
15+
'}' \
16+
'@test "following fixture" {' \
17+
' true' \
18+
'}' >"$fixture"
19+
20+
if ! "$RUNNER" "$fixture" >"$dir/out" 2>&1; then
21+
echo "runner failed a suite containing a skipped test" >&2
22+
cat "$dir/out" >&2
23+
exit 1
24+
fi
25+
26+
grep -qF 'Test skipped: skipped fixture (not applicable)' "$dir/out"
27+
if grep -qF 'SHOULD_NOT_RUN' "$dir/out"; then
28+
echo "runner continued executing the skipped test body" >&2
29+
cat "$dir/out" >&2
30+
exit 1
31+
fi
32+
grep -qF 'Test OK: following fixture' "$dir/out"
33+
rm -rf "$dir"
34+
}

‎hack/cozytest.sh‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ TEST_FILE=${1:?Usage: ./cozytest.sh <file.bats> [pattern]}
99
PATTERN=${2:-*}
1010
LINE='----------------------------------------------------------------'
1111

12+
# Bats' skip stops the current test without failing the suite. Each test runs
13+
# in its own subshell below, so exiting here leaves the runner itself alive to
14+
# report the skip and continue with the next test.
15+
skip() {
16+
printf '%s\n' "$*" > "$skip_file"
17+
exit 0
18+
}
19+
1220
cols() { stty size 2>/dev/null | awk '{print $2}' || echo 80; }
1321
if [ -t 1 ]; then
1422
MAXW=$(( $(cols) - 12 )); [ "$MAXW" -lt 40 ] && MAXW=70
@@ -25,6 +33,7 @@ run_one() {
2533
fn=$1 title=$2
2634
tmp=$(mktemp -d) || { echo "Failed to create temp directory" >&2; exit 1; }
2735
log="$tmp/log"
36+
skip_file="$tmp/skip"
2837

2938
echo "╭ » Run test: $title"
3039
START=$(date +%s)
@@ -57,7 +66,14 @@ run_one() {
5766
[ -z "$rc" ] && rc=1
5867
now=$(( $(date +%s) - START ))
5968

60-
if [ "$rc" -eq 0 ]; then
69+
if [ "$rc" -eq 0 ] && [ -f "$skip_file" ]; then
70+
skip_reason=$(cat "$skip_file")
71+
if [ -n "$skip_reason" ]; then
72+
printf '╰[%02d:%02d] ⏭️ Test skipped: %s (%s)\n' $((now/60)) $((now%60)) "$title" "$skip_reason"
73+
else
74+
printf '╰[%02d:%02d] ⏭️ Test skipped: %s\n' $((now/60)) $((now%60)) "$title"
75+
fi
76+
elif [ "$rc" -eq 0 ]; then
6177
printf '╰[%02d:%02d] ✅ Test OK: %s\n' $((now/60)) $((now%60)) "$title"
6278
else
6379
printf '╰[%02d:%02d] ❌ Test failed: %s (exit %s)\n' \

0 commit comments

Comments
 (0)