tests: parse under hostile caller settings.
commit 7331992346dcfc1934df3d824155ad394f6ec4e2 · parent 5bcf631fcb
· browse this tree
Ximianth, 2026-10-23 09:44:26 UTC
errexit with an ERR trap, nocasematch, failglob, extglob, an IFS of syntax characters, and LC_ALL=C.UTF-8. Removing the wrapper's nocasematch handling, or its LC_ALL=C, makes this fail.
diff --git a/tests/test-hostile-env.sh b/tests/test-hostile-env.sh new file mode 100755 index 0000000..6e8b7df --- /dev/null +++ b/tests/test-hostile-env.sh @@ -0,0 +1,38 @@ +#!/bin/bash +## A few parse cases under caller settings that would bend the library's behaviour if they reached it: +## errexit and an ERR trap, case-blind matching, failglob, an IFS full of syntax characters, and a UTF-8 locale. +set -euo pipefail +cd "$(dirname "$0")" +# shellcheck source=tests/lib.sh +source ./lib.sh + +err_trap_fired=0 +set -E +trap 'err_trap_fired=$((err_trap_fired + 1))' ERR +shopt -s nocasematch failglob extglob +IFS='(#) ' +export LC_ALL=C.UTF-8 + +## Like test-parse.sh's parse_case. +parse_case() { + printf -- "$2" >in.md + dump_parse in.md >got + expect_eq "$1" "$(<got)" "$(printf '%s\n' "${@:3}")" +} + +parse_case "nocasematch: annotations are case-sensitive" '[TESTMARK]:# (x)\n```\nhello\n```\n' +parse_case "UTF-8 locale: an em space isn't whitespace" \ + '[testmark]:# (a\xe2\x80\x83b)\n```\xe2\x80\x83json\xe2\x80\x83\nhello\n```\n' \ + 'hunk "a\xe2\x80\x83b"' 'info "\xe2\x80\x83json\xe2\x80\x83"' 'body "hello\n"' +parse_case "IFS: names and info strings" '[testmark]:# ( (x#) )\n``` (a b) \nhello\n```\n' 'hunk "(x#)"' 'info "(a b)"' 'body "hello\n"' + +# Called directly, not through dump_parse's `if`, which would shield them from errexit and the ERR trap. +printf '[testmark]:# (x)\n```\nhello\n```' >in.md +testmark_parse doc in.md +expect_eq "errexit: a doc without a final newline" "$(testmark_body doc x)" "hello" +rc=0 +testmark_body doc nope 2>/dev/null || rc=$? +expect_eq "errexit: an error returns, to a caller that checks" "$rc" 1 +expect_eq "the ERR trap never fired" "$err_trap_fired" 0 + +exit "$RESULT"