testmark-bash

test-caller-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

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

# Patch, check, and write, called directly the same way; and write under noclobber, since it writes the file in place.
printf '[testmark]:# (x)\n```\nhello\n```\n[testmark]:# (y)\n```\nhello\n```\n[testmark]:# (z)\n```\nhello\n```\n' >in.md
testmark_parse doc in.md
testmark_check doc x <<<'hello'
rc=0
testmark_check doc z <<<'HELLO' 2>/dev/null || rc=$?
expect_eq "nocasematch: a check that differs only in case fails" "$rc" 1
testmark_patch doc x <<<'patched'
TESTMARK_REGEN=1 testmark_check doc y <<<'regen'
set -C
testmark_write doc
set +C
expect_eq "errexit, noclobber: patch, check, and write" "$(testmark_body doc x)$(testmark_body doc y)" "patchedregen"
expect_eq "the ERR trap never fired" "$err_trap_fired" 0

# The library turns on set -u for its own code only.
set +u
before=$-
testmark_body doc x >/dev/null
expect_eq "set -u: a caller without it still lacks it after a call" "$-" "$before"
set -u

exit "$RESULT"

tests/test-caller-env.sh · 2.2 KiB · 29c582aab1