Skip to content

Commit c0ff783

Browse files
authored
fix(hack): make bats unit tests pass on a BSD/macOS checkout (#4284)
## What this PR does Refs #4272 (Defects 1 and 2). On a clean checkout of `main`, `make unit-tests` is red out of the box on macOS for reasons that have nothing to do with the change under test: the BSD userland shipped with macOS differs from the GNU tools on the Linux CI runners, and CI never sees any of it. This PR closes the two worst of those, both scoped to `hack/`. **Defect 1 — the runner cannot parse a non-ASCII `@test` title.** `hack/cozytest.sh` rebuilds each test's function name by walking its title one character at a time with awk `substr()`. BSD awk aborts that walk on the first multibyte byte (`towc: multibyte conversion failure`) rather than returning it, so a file carrying a non-ASCII `@test` title never parses and not one of its tests runs — silently, because the suite only reports that the runner errored. Four `hack/*.bats` files carry such a title today. The fix pins `LC_ALL=C` on all three awk passes (title parse, run selection, and the exit-code scan of the captured log) so each reads the input byte-wise; the parse and selection passes must share the ctype or they would derive mismatched function names for one title. A new regression test, `hack/cozytest-nonascii-title.bats`, builds a multibyte-titled fixture at runtime and asserts the runner actually runs it; it is red against the pre-fix runner (mutation-verified) and green after. **Defect 2 — GNU-only `sed -i`.** `hack/nightly-mirror.sh`, `hack/promote-rewrite-tags.sh`, and the fixture builder in `hack/promote-rewrite-tags_test.bats` edit files in place with `sed -i`. BSD sed reads the argument after `-i` as a mandatory backup suffix, so `sed -i -e` and `sed -i -E` swallow the following flag as that suffix and the edit either errors or silently no-ops. Each is rerouted through a temp file, portable to both seds. The `nightly-mirror.sh` rewrite additionally relied on the GNU `\?` optional quantifier, which BSD BRE reads as a literal `?` so a bare host was never matched; it is replaced with the POSIX `\{0,1\}`, equivalent on both. All three suites are green on macOS after the change, and behaviour on GNU sed is unchanged. ### Scope This PR is deliberately scoped to Defects 1 and 2. Defect 3 from the issue — a GNU-only `date -d` in `hack/seaweedfs-naming-audit.sh` — is tracked separately as #4273 and is left untouched here. For transparency: with Defect 1 fixed, several `hack/*.bats` suites that previously never ran now run, and a handful still fail on macOS because they depend on GNU coreutils `timeout`, which base macOS lacks (`capture-dataplane`, `cozytest-capture-gate`, `run-kubernetes-join-timing_test`, `run-kubernetes-runner-cpu_test`, `runner-identity`). Those failures are pre-existing — identical before and after this change when the runner is swapped in place — and are a distinct defect from the two fixed here, so they are out of this PR's scope. A fully green `make unit-tests` on macOS additionally depends on #4273 and on that separate `timeout` gap. ### Verification All fixes were verified on a genuine macOS host (Darwin, BSD awk 20200816 / BSD sed). `hack/cozytest-nonascii-title.bats`, `hack/nightly-mirror_test.bats` (5 tests), and `hack/promote-rewrite-tags_test.bats` (17 tests) are green; the two files the issue reports as running zero tests (`overlay-main-images_test.bats`, `promote-gate-contract.bats`) now yield 13 and 15 `Test OK`, matching the issue's numbers. Each fix is mutation-proven: reverting it turns the corresponding suite red again. ### Downstream repositories The diff touches only internal `hack/` tooling — the bats test runner and two release scripts. No downstream repository vendors or restates these: `cozystack/ccp` and `cozystack/external-apps-example` gate on `hack/package.mk`, `hack/common-envs.mk` and `hack/update-crd.sh`, none of which this PR changes, and nothing renames or moves a file or alters what a make target does. - [x] No downstream repository is affected by this change ### Release note ```release-note fix(hack): make the bats unit-test runner parse non-ASCII `@test` titles under BSD awk, and route the in-place edits in `nightly-mirror.sh` and `promote-rewrite-tags.sh` through a temp file so they work under BSD sed — closing two of the host-tool gaps that make `make unit-tests` red on a clean macOS checkout ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved test runner compatibility with non-ASCII test titles and output. * Improved text-processing and file-rewrite behavior across BSD and GNU environments. * Preserved file permissions and metadata during automated rewrites. * Improved handling of host values and release tags across supported platforms. * **Tests** * Added coverage for non-ASCII test titles. * Updated rewrite tests for portable BSD and GNU tooling. * **Documentation** * Clarified supported BSD and GNU command-line tool compatibility. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 1c88014 + 8fbec92 commit c0ff783

6 files changed

Lines changed: 122 additions & 16 deletions

‎hack/cozytest-nonascii-title.bats‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env bats
2+
# Guards that hack/cozytest.sh parses a @test title that is not ASCII.
3+
#
4+
# The runner reconstructs each test's function name by walking its title one
5+
# character at a time with awk's substr(). BSD awk aborts that walk on the first
6+
# multibyte byte ("towc: multibyte conversion failure") rather than returning it,
7+
# so a file carrying a non-ASCII @test title is never parsed and not one of its
8+
# tests runs -- silently, because the suite only reports that the runner errored.
9+
# Four hack/*.bats files carry such a title today; the byte scan is correct only
10+
# when the runner pins a single-byte (C) ctype for the parse.
11+
#
12+
# This is BSD-specific: GNU awk returns the byte and never aborts, so on the
13+
# Linux CI runner this guard passes whether or not the fix is present. It earns
14+
# its keep on a developer's macOS box, where the default UTF-8 locale is exactly
15+
# what triggers the abort -- the environment `make unit-tests` is meant to be
16+
# green in. The fixture is therefore run under an explicit UTF-8 ctype so the
17+
# byte scan is exercised the way that box exercises it; under a C ctype the scan
18+
# cannot abort and the guard would pass without proving anything.
19+
#
20+
# Harness note (see hack/promote-rewrite-tags_test.bats): the CI path is
21+
# hack/cozytest.sh, not real bats -- no `run`, `$status`, `skip` or setup(). Each
22+
# test runs as a shell function under `set -eu -x`; paths are repo-root-relative.
23+
#
24+
# Run with: hack/cozytest.sh hack/cozytest-nonascii-title.bats
25+
26+
@test "cozytest.sh runs a test whose title contains a multibyte character" {
27+
tmp=$(mktemp -d)
28+
29+
# Build the multibyte bytes at runtime so this file stays pure ASCII on disk:
30+
# the runner parsing THIS file must not itself meet a byte the bug chokes on.
31+
# U+2014 EM DASH (e2 80 94) plus U+00E9 (c3 a9), the same shapes the four
32+
# real offenders carry.
33+
emdash=$(printf '\342\200\224')
34+
eacute=$(printf '\303\251')
35+
title="promote body carries a caf${eacute} status line ${emdash} multibyte"
36+
37+
fixture="$tmp/nonascii.bats"
38+
{
39+
printf '@test "%s" {\n' "$title"
40+
printf ' :\n'
41+
printf '}\n'
42+
} > "$fixture"
43+
44+
# A UTF-8 ctype makes BSD awk's per-character scan abort on the title unless the
45+
# runner pins C for the parse. en_US.UTF-8 is present on macOS and typical CI;
46+
# where no UTF-8 locale exists libc falls back to C, and there the bug cannot
47+
# occur, so a pass is the correct answer rather than a false negative.
48+
out=$(LC_ALL=en_US.UTF-8 COZYTEST_TRACE=0 hack/cozytest.sh "$fixture" 2>&1) || true
49+
50+
case "$out" in
51+
*"Test OK: $title"*) ;;
52+
*)
53+
echo "cozytest.sh did not run the multibyte-titled test; its output was:" >&2
54+
printf '%s\n' "$out" >&2
55+
rm -rf "$tmp"
56+
return 1
57+
;;
58+
esac
59+
rm -rf "$tmp"
60+
}

‎hack/cozytest.sh‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ run_one() {
9393
printf '┊[%02d:%02d] %s\n' $((now/60)) $((now%60)) "$out"
9494
done
9595

96-
rc=$(awk '/^__RC__/ {print substr($0,7)}' "$log" | tail -n1)
96+
# LC_ALL=C so BSD awk scans the captured log byte-wise: a test whose trace or
97+
# output carries a multibyte character would otherwise abort this scan ("towc:
98+
# multibyte conversion failure") and leave the exit code unread.
99+
rc=$(LC_ALL=C awk '/^__RC__/ {print substr($0,7)}' "$log" | tail -n1)
97100
[ -z "$rc" ] && rc=1
98101
now=$(( $(date +%s) - START ))
99102

@@ -281,7 +284,14 @@ _cozy_on_exit() {
281284
rm -f "$TMP_SH"
282285
}
283286
trap '_cozy_on_exit' EXIT
284-
awk '
287+
# LC_ALL=C pins a single-byte ctype for the title scan below. It walks each title
288+
# one character at a time with substr(), and BSD awk aborts that walk on the first
289+
# multibyte byte ("towc: multibyte conversion failure") instead of returning it,
290+
# so a file with a non-ASCII @test title never parses and none of its tests run.
291+
# Under C every byte is its own character, which is all the scan needs: the title
292+
# is preserved verbatim for display and only the derived function name is mangled.
293+
# The run-selection pass below is pinned the same way so the two agree on names.
294+
LC_ALL=C awk '
285295
/^@test[[:space:]]+"/ {
286296
line = substr($0, index($0, "\"") + 1)
287297
title = substr(line, 1, index(line, "\"") - 1)
@@ -310,7 +320,7 @@ awk '
310320
###############################################################################
311321
# run selected tests #
312322
###############################################################################
313-
awk -v pat="$PATTERN" '
323+
LC_ALL=C awk -v pat="$PATTERN" '
314324
/^### / {
315325
title = substr($0, 5)
316326
name = "test_"

‎hack/nightly-mirror.sh‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ done
133133
# its digest (platformSourceRef) is reset by the caller after the GHCR re-push.
134134
SRC_ESC=$(printf '%s' "$SRC_REGISTRY" | sed -e 's/[].[^$*/\\]/\\&/g')
135135
if [ "$DRY_RUN" -eq 1 ]; then
136-
echo "DRY-RUN sed -i 's|${SRC_REGISTRY}/|${DST_REGISTRY}/|g' over $(image_ref_files "$TREE" | wc -l | tr -d ' ') ref-bearing files under ${TREE}"
136+
# Name the rewrite by intent, not by the `sed -i` idiom the real path stopped
137+
# using, and report both expressions it applies: the `<host>/` prefix and the
138+
# bare-host scalar. Naming one expression understates what the run touches.
139+
echo "DRY-RUN rewrite image host ${SRC_REGISTRY}/ -> ${DST_REGISTRY}/ (and a bare ${SRC_REGISTRY} host scalar -> ${DST_REGISTRY}) over $(image_ref_files "$TREE" | wc -l | tr -d ' ') ref-bearing files under ${TREE}"
137140
else
138141
# Exactly the files collect_refs scanned, via the same enumeration: the set
139142
# whose hosts are rewritten must equal the set scanned for images to mirror.
@@ -156,11 +159,26 @@ else
156159
# idyksih5sir9/cozystack/<name>`): neither key holds SRC_REGISTRY as a whole,
157160
# so rewriting it needs the structure-aware pass docs/agents/image-refs.md
158161
# describes. That gap stays dormant while no CI path rebuilds the package.
162+
# In-place edit written portably: `sed -i` takes the next argument as the
163+
# backup suffix on BSD but as a flag on GNU, so `sed -i -e` diverges between
164+
# them. Route the output through a temp file and copy it back over the original
165+
# (cat, not mv, keeps the file's inode and permissions). `cat` truncates before
166+
# it writes, so an interrupt here can leave a half-written file where `mv` would
167+
# not; that is the lesser evil against `mv` stamping mktemp's 0600 onto every
168+
# rewritten file in the tree.
159169
image_ref_files "$TREE" | while IFS= read -r f; do
160-
sed -i \
170+
# `\{0,1\}` rather than the `\?` optional-quantifier: `\?` is a GNU BRE
171+
# extension that BSD sed reads as a literal `?`, turning each `["']\?` into
172+
# "a quote followed by a literal ?". On macOS that makes the whole expression
173+
# dead -- it matches neither a bare host nor a quoted one, not just the bare
174+
# case. `\{0,1\}` is POSIX BRE and behaves the same on both.
175+
tmp_f=$(mktemp)
176+
sed \
161177
-e "s|${SRC_ESC}/|${DST_REGISTRY}/|g" \
162-
-e "s|\(:[[:space:]]*[\"']\?\)${SRC_ESC}\([\"']\?\)[[:space:]]*$|\1${DST_REGISTRY}\2|" \
163-
"$f"
178+
-e "s|\(:[[:space:]]*[\"']\{0,1\}\)${SRC_ESC}\([\"']\{0,1\}\)[[:space:]]*$|\1${DST_REGISTRY}\2|" \
179+
"$f" > "$tmp_f"
180+
cat "$tmp_f" > "$f"
181+
rm -f "$tmp_f"
164182
done
165183
fi
166184

‎hack/nightly-mirror_test.bats‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,12 @@ _make_tree() {
187187
# The cozystack-packages artifact is excluded — it is rebuilt downstream.
188188
! grep -qE 'skopeo copy.*cozystack-packages' "$tmp/out"
189189

190-
# The host rewrite is planned source->dest.
191-
grep -q "s|iad.ocir.io/idyksih5sir9/cozystack/|ghcr.io/cozystack/cozystack/|g" "$tmp/out"
190+
# The host rewrite is planned source->dest, reported by intent for both
191+
# expressions the real path applies: the '<host>/' prefix and the bare-host
192+
# scalar. The message must not name `sed -i` -- the path no longer uses it.
193+
grep -Fq "rewrite image host iad.ocir.io/idyksih5sir9/cozystack/ -> ghcr.io/cozystack/cozystack/" "$tmp/out"
194+
grep -Fq "bare iad.ocir.io/idyksih5sir9/cozystack host scalar -> ghcr.io/cozystack/cozystack" "$tmp/out"
195+
! grep -q 'sed -i' "$tmp/out"
192196
}
193197

194198
@test "empty selection (wrong source registry) exits non-zero with a diagnostic" {

‎hack/promote-rewrite-tags.sh‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
# observable by cutting a release. It is a script so that
2020
# hack/promote-rewrite-tags_test.bats can round-trip it against the real tree.
2121
#
22-
# Requires a POSIX shell plus GNU sed and GNU grep: `sed -i` and grep's
23-
# -r/--exclude/--exclude-dir are GNU extensions, not POSIX. Both are present on
24-
# the CI runners and in the build image; this is not portable to BSD userland
25-
# as written.
22+
# grep's -r/-I/--exclude/--exclude-dir are non-POSIX, but both GNU and BSD grep
23+
# implement them, so this runs unchanged on the CI runners, in the build image,
24+
# and on a macOS developer box. The edit is written through a temp file rather
25+
# than `sed -i`, whose argument handling differs between the two seds.
2626
set -eu
2727

2828
RC_VERSION="${1:?usage: promote-rewrite-tags.sh <rc-version> <stable-version> [root]}"
@@ -71,7 +71,17 @@ while IFS= read -r f; do
7171
break
7272
fi
7373
if grep -q "$RC_ESC" "$f"; then
74-
sed -i "s/${RC_ESC}/${STABLE_VERSION}/g" "$f"
74+
# Written through a temp file rather than `sed -i`: the in-place flag takes
75+
# the substitution as its backup suffix on BSD and edits nothing, so the
76+
# rewrite would silently no-op on a macOS `make unit-tests`. cat back over the
77+
# original to keep its inode and permissions. `cat` truncates before it
78+
# writes, so an interrupt can leave a half-written file where `mv` would not;
79+
# that is the lesser evil against `mv` stamping mktemp's 0600 onto a file in
80+
# the release tree.
81+
tmp_f=$(mktemp)
82+
sed "s/${RC_ESC}/${STABLE_VERSION}/g" "$f" > "$tmp_f"
83+
cat "$tmp_f" > "$f"
84+
rm -f "$tmp_f"
7585
echo " ~ $f"
7686
rewritten=$((rewritten + 1))
7787
else

‎hack/promote-rewrite-tags_test.bats‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,13 @@
6363
# value is a version, as cilium and kubeovn write it), so the fixture
6464
# exercises every shape the enumeration claims to cover rather than only the
6565
# single-string one.
66+
# `sed -i -E`: on BSD `-i` consumes the next argument as the backup suffix, so
67+
# `-E` is eaten and never enables extended syntax — the `\1` backreferences
68+
# then refer to groups plain BRE never captured. Route each edit through a temp
69+
# file instead; `sed -E` (extended regex) is understood by both GNU and BSD.
6670
for f in $(grep -rIl 'cozystack/cozystack' "$tmp/packages"); do
67-
sed -i -E "s|(cozystack/cozystack/[A-Za-z0-9._-]+):v[0-9]+\.[0-9]+\.[0-9]+@|\1:v${RC}@|g" "$f"
68-
sed -i -E "s|^([[:space:]]*tag:[[:space:]]*)v[0-9]+\.[0-9]+\.[0-9]+([[:space:]]*)$|\1v${RC}\2|" "$f"
71+
sed -E "s|(cozystack/cozystack/[A-Za-z0-9._-]+):v[0-9]+\.[0-9]+\.[0-9]+@|\1:v${RC}@|g" "$f" > "$f.tmp" && mv "$f.tmp" "$f"
72+
sed -E "s|^([[:space:]]*tag:[[:space:]]*)v[0-9]+\.[0-9]+\.[0-9]+([[:space:]]*)$|\1v${RC}\2|" "$f" > "$f.tmp" && mv "$f.tmp" "$f"
6973
done
7074

7175
# Sanity: the fixture must actually contain the rc string, otherwise the

0 commit comments

Comments
 (0)