<commit_before>#! test/for/moderni/sh
# See the file LICENSE in the main modernish directory for the licence.

# Test the trap stack and POSIX traps.

trap_testfile="$testdir/trap.t test file"
( umask 077 && : > $trap_testfile ) || die
shellquote trap_testfile_q=$trap_testfile

TEST title='push;set;check;send sig;unset;pop;check'
	# one large test since every step depends on the previous one;
	# running these separately would cause them to fail
	# ------------------
	failmsg='push traps'
	push IFS -C -u
	failmsg='trap 1' \
	&& IFS=abc && set -C \
	&& pushtrap "v=trap1; str eq \"\$IFS\" abc && isset -C && putln 'trap1ok' >>$trap_testfile_q" ALRM \
	&& failmsg='trap 2' \
	&& IFS= && set +C \
	&& pushtrap "v=trap2; str empty \"\$IFS\" && not isset -C && putln 'trap2ok' >>$trap_testfile_q" SIGALRM \
	&& pushtrap --nosubshell 'v=trap2a' sigAlrm \
	&& failmsg='trap 3' \
	&& unset -v IFS && set +u \
	&& pushtrap "v=trap3; not isset IFS && not isset -u && putln 'trap3ok' >>$trap_testfile_q" sIgAlRm
	pop --keepstatus IFS -C -u || return 1
	# ------------------
	failmsg='set POSIX trap'
	trap "putln 'POSIX-trap' >>$trap_testfile_q" SIGalrm || return 1
	# ------------------
	failmsg='check traps, test var=$(trap)'
	case $(trap) in
	( *\
"pushtrap -- \"v=trap1; str eq \\\"\\\$IFS\\\" abc && isset -C && putln 'trap1ok' >>"*"\" ALRM
pushtrap -- \"v=trap2; str empty \\\"\\\$IFS\\\" && not isset -C && putln 'trap2ok' >>"*"\" ALRM
pushtrap --nosubshell -- 'v=trap2a' ALRM
pushtrap -- \"v=trap3; not isset IFS && not isset -u && putln 'trap3ok' >>"*"\" ALRM
trap -- \"putln 'POSIX-trap' >>"*"\" ALRM"* )
		;;
	( * )	return 1 ;;
	esac
	# ------------------
	failmsg='send signal, execute traps'
	unset -v v
	kill -s ALRM "$$"
	if not isset v || not str eq $v 'trap2a'; then
		append failmsg ' (--nosubshell)'
	fi
	if not is nonempty "$trap_testfile"; then
		append failmsg ' (no output)'
	fi
	not isset failmsg
	# ------------------
	failmsg='unset POSIX trap'
	trap - ALRM || return 1
	# ------------------
	failmsg='pop trap 3' \
	&& poptrap -R alrm \
	&& str match $REPLY 'pushtrap -- *v=trap3;*trap3ok* ALRM' \
	&& failmsg='pop trap 2a' \
	&& poptrap -R aLRm \
	&& str match $REPLY 'pushtrap --nosubshell -- ?v=trap2a? ALRM' \
	&& failmsg='pop trap 2' \
	&& poptrap -R SIGALRM \
	&& str match $REPLY 'pushtrap -- *v=trap2;*trap2ok* ALRM' \
	&& failmsg='pop trap 1' \
	&& poptrap -R sigalrm \<commit_msg>tst/trap.t: fix bug in check that trap was triggered (re: ca254057)<commit_after>#! test/for/moderni/sh
# See the file LICENSE in the main modernish directory for the licence.

# Test the trap stack and POSIX traps.

trap_testfile="$testdir/trap.t test file"
( umask 077 && : > $trap_testfile ) || die
shellquote trap_testfile_q=$trap_testfile

TEST title='push;set;check;send sig;unset;pop;check'
	# one large test since every step depends on the previous one;
	# running these separately would cause them to fail
	# ------------------
	failmsg='push traps'
	push IFS -C -u
	failmsg='trap 1' \
	&& IFS=abc && set -C \
	&& pushtrap "v=trap1; str eq \"\$IFS\" abc && isset -C && putln 'trap1ok' >>$trap_testfile_q" ALRM \
	&& failmsg='trap 2' \
	&& IFS= && set +C \
	&& pushtrap "v=trap2; str empty \"\$IFS\" && not isset -C && putln 'trap2ok' >>$trap_testfile_q" SIGALRM \
	&& pushtrap --nosubshell 'v=trap2a' sigAlrm \
	&& failmsg='trap 3' \
	&& unset -v IFS && set +u \
	&& pushtrap "v=trap3; not isset IFS && not isset -u && putln 'trap3ok' >>$trap_testfile_q" sIgAlRm
	pop --keepstatus IFS -C -u || return 1
	# ------------------
	failmsg='set POSIX trap'
	trap "putln 'POSIX-trap' >>$trap_testfile_q" SIGalrm || return 1
	# ------------------
	failmsg='check traps, test var=$(trap)'
	case $(trap) in
	( *\
"pushtrap -- \"v=trap1; str eq \\\"\\\$IFS\\\" abc && isset -C && putln 'trap1ok' >>"*"\" ALRM
pushtrap -- \"v=trap2; str empty \\\"\\\$IFS\\\" && not isset -C && putln 'trap2ok' >>"*"\" ALRM
pushtrap --nosubshell -- 'v=trap2a' ALRM
pushtrap -- \"v=trap3; not isset IFS && not isset -u && putln 'trap3ok' >>"*"\" ALRM
trap -- \"putln 'POSIX-trap' >>"*"\" ALRM"* )
		;;
	( * )	return 1 ;;
	esac
	# ------------------
	failmsg='send signal, execute traps'
	unset -v v fail
	kill -s ALRM "$$"
	if not isset v || not str eq $v 'trap2a'; then
		append failmsg ' (--nosubshell)'
		fail=y
	fi
	if not is nonempty "$trap_testfile"; then
		append failmsg ' (no output)'
		fail=y
	fi
	isset fail && return 1
	# ------------------
	failmsg='unset POSIX trap'
	trap - ALRM || return 1
	# ------------------
	failmsg='pop trap 3' \
	&& poptrap -R alrm \
	&& str match $REPLY 'pushtrap -- *v=trap3;*trap3ok* ALRM' \
	&& failmsg='pop trap 2a' \
	&& poptrap -R aLRm \
	&& str match $REPLY 'pushtrap --nosubshell -- ?v=trap2a? ALRM' \
	&& failmsg='pop trap 2' \
	&& poptrap -R SIGALRM \
	&& str match $REPLY 'pushtrap -- *v=trap2;*trap2ok* ALRM' \
	&& failmsg='pop trap 1' \
	&& poptrap -R sigalrm \
{'fraction_non_alphanumeric': 0.21769262128006522, 'fraction_numerical': 0.015287403179779861, 'mean_word_length': 2.728723404255319, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7286170', 'n_tokens_mistral': 2070, 'n_tokens_neox': 2026, 'n_words': 556}