testmark-bash

test-hostile-env.sh

#!/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"

tests/test-hostile-env.sh · 1.5 KiB · aec43e5485