testmark-bash

Replace the library's two =~ matches with globs and parameter expansions.

commit 8d0fe9cfbc27ad95f53de92af3a63a4118295358 · parent 23989549e7 · browse this tree
Ximianth, 2026-10-23 23:34:23 UTC

Simpler is better.

The handle check is a glob, exact under the wrapper's LC_ALL=C.
The annotation name is two expansions: skip leading whitespace, cut at the next.
test-parse.sh pins the name behaviour and passes unchanged.

Using bash's regexp builtins wasn't terrible,
but was unnecessarily complicated...

and though surely this is not consequential...

=~ overwrites the global BASH_REMATCH.
Yes, global.
And it can't be made local across our versions:
`local BASH_REMATCH` is a readonly error on 4.4 and 5.0,
is accepted but still clobbered on 5.1 and 5.2,
and only protects the caller's value on 5.3.

On the one hand, surely someone would never try to split BASH_REMATCH
reads at any distance from their use of the =~ operator.

On the other hand, having just learned this:
okay, so, going for simpler things: cool, yeah.
diff --git a/lib/testmark/document.bash b/lib/testmark/document.bash
index 54b9264..0a66374 100644
--- a/lib/testmark/document.bash
+++ b/lib/testmark/document.bash
@@ -20,7 +20,7 @@ _testmark_err() {
 
 ## Fails with a message unless $1 is a valid handle.
 _testmark_handle() {
-	[[ $1 =~ ^[A-Za-z0-9_]+$ ]] || {
+	[[ -n $1 && $1 != *[!A-Za-z0-9_]* ]] || {
 		_testmark_err "invalid handle \"$1\": must match [A-Za-z0-9_]+"
 		return 1
 	}
@@ -79,11 +79,14 @@ _testmark_parse() {
 		elif [[ $line == '[testmark]:#'* ]]; then
 			name=${line#'[testmark]:# ('}
 			name=${name%')'}
-			if [[ $line != '[testmark]:# ('*')' || ! $name =~ ^[[:space:]]*([^[:space:]]+) ]]; then
+			# The name is the first word: skip leading whitespace, then stop at the next.
+			name=${name#"${name%%[![:space:]]*}"}
+			name=${name%%[[:space:]]*}
+			if [[ $line != '[testmark]:# ('*')' || -z $name ]]; then
 				_testmark_err "$file:$((i + 1)): malformed annotation; want \"[testmark]:# (name)\""
 				return 1
 			fi
-			k=${BASH_REMATCH[1]}
+			k=$name
 			[[ -z ${start[$k]+set} ]] || {
 				_testmark_err "$file:$((i + 1)): duplicate hunk name \"$k\""
 				return 1