testmark-bash

tests: move expect_fail and expect_ok into lib.sh, for the patch and check tests to share.

commit d30f313568634b2f329bacfef93372a6c3adc733 · parent 4e822d05d7 · browse this tree
Ximianth, 2026-10-24 00:16:14 UTC

diff --git a/tests/lib.sh b/tests/lib.sh
index d87f243..0e8aebf 100644
--- a/tests/lib.sh
+++ b/tests/lib.sh
@@ -50,6 +50,31 @@ expect_eq() {
 	fi
 }
 
+## Runs a command that should fail, and checks it printed exactly the given error and nothing on stdout.
+##
+## - @param $1 description
+## - @param $2 the expected stderr, without its final newline
+## - @param $3... the command
+expect_fail() {
+	local rc=0
+	"${@:3}" >"$work/out" 2>"$work/err" || rc=$?
+	if [[ $rc == 0 ]]; then
+		_fail "$1: succeeded; want a failure"
+	else
+		expect_eq "$1" "$(<"$work/err")$(<"$work/out")" "$2"
+	fi
+}
+
+## Runs a command in this shell, so it can change state, and checks it succeeded with nothing on stderr.
+##
+## - @param $1 description
+## - @param $2... the command
+expect_ok() {
+	local rc=0
+	"${@:2}" 2>"$work/err" || rc=$?
+	expect_eq "$1" "$rc $(<"$work/err")" "0 "
+}
+
 ## Quotes bytes the way the conformance harnesses do: printable ASCII as is, escapes for the rest.
 ##
 ## - @param $1 bytes
diff --git a/tests/test-api.sh b/tests/test-api.sh
index d91b3c7..53e1389 100755
--- a/tests/test-api.sh
+++ b/tests/test-api.sh
@@ -5,31 +5,6 @@ cd "$(dirname "$0")"
 # shellcheck source=tests/lib.sh
 source ./lib.sh
 
-## Runs a command that should fail, and checks it printed exactly the given error and nothing on stdout.
-##
-## - @param $1 description
-## - @param $2 the expected stderr, without its final newline
-## - @param $3... the command
-expect_fail() {
-	local rc=0
-	"${@:3}" >"$work/out" 2>"$work/err" || rc=$?
-	if [[ $rc == 0 ]]; then
-		_fail "$1: succeeded; want a failure"
-	else
-		expect_eq "$1" "$(<"$work/err")$(<"$work/out")" "$2"
-	fi
-}
-
-## Runs a command in this shell, so it can change state, and checks it succeeded with nothing on stderr.
-##
-## - @param $1 description
-## - @param $2... the command
-expect_ok() {
-	local rc=0
-	"${@:2}" 2>"$work/err" || rc=$?
-	expect_eq "$1" "$rc $(<"$work/err")" "0 "
-}
-
 printf '[testmark]:# (x)\n```text\none\n```\n[testmark]:# (old)\n```\ngone\n```\n' >a.md
 printf '[testmark]:# (x)\n```\ntwo\n```\n' >b.md
 printf 'no hunks here\n' >none.md