testmark-bash

check.bash

## check.bash: checking output against a hunk, or under TESTMARK_REGEN, staging it as the hunk's new body.
## Sourced by testmark.bash; the functions here assume they run inside `_testmark_call`.
# shellcheck disable=SC2034,SC2178 # shellcheck does not follow namerefs; see document.bash

_testmark_check() {
	(($# == 2)) || {
		_testmark_err "usage: testmark_check HANDLE NAME <ACTUAL"
		return 1
	}
	_testmark_hunk "$1" "$2" || return 1
	_testmark_same_shell "$1" || return 1
	local input body
	_testmark_input "$1: hunk \"$2\"" || return 1
	# A body is "" or lines ending in LF, never CRLF, so other output could never match, and regen would rewrite it on every run.
	[[ -z $input || ($input == *$'\n' && $input != *$'\r\n'*) ]] || {
		_testmark_err "$1: hunk \"$2\": output must be empty or end in a newline, and contain no CRLF; normalize it before checking"
		return 1
	}

	# Two checks claiming one hunk with different output is a harness bug, whether or not the first one matched.
	# The same output again is compared like the first time, so a mismatch can't pass by repetition.
	local -n checked=_testmark_checked_$1
	[[ ${checked[$2]-$input} == "$input" ]] || {
		_testmark_err "$1: hunk \"$2\": checked twice, with different output"
		return 1
	}
	checked[$2]=$input

	_testmark_get_body "$1" "$2"
	[[ $input != "$body" ]] || return 0
	if [[ -n ${TESTMARK_REGEN+set} ]]; then
		_testmark_stage "$1" "$2" "$input"
		return
	fi
	local -n file=_testmark_file_$1
	_testmark_err "$1: hunk \"$2\": output doesn't match; set TESTMARK_REGEN=1 to accept it"
	# diff's status isn't checked: the comparison above has decided already, and diff is only here to show it.
	>&2 diff -u --label "$file: $2 (expected)" --label "$file: $2 (actual)" <(printf '%s' "$body") <(printf '%s' "$input")
	return 1
}

lib/testmark/check.bash · 1.8 KiB · 9a69562881