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

# Regression tests related to (built-in) utilities of the shell.

TEST title="options to 'command' can be expansions"
	v='-v'
	MSH_NOT_FOUND_OK=1
	command $v : >/dev/null 2>&1
	v=$?
	unset -v MSH_NOT_FOUND_OK
	case $v in
	( 0 )	mustNotHave BUG_CMDOPTEXP ;;
	# test suite runs with PATH=/dev/null, so we can rely on 127 = not found
	( 127 )	mustHave BUG_CMDOPTEXP ;;
	( * )	return 1 ;;
	esac
ENDT

TEST title="'command' parses arguments properly"
	# FTL_COMMAND2P: double evaluation of grammar and/or parameter
	# expansion grammar when using 'command' with an external command.
	# (AT&T ksh88; schilytools sh <= 2017-08-14)
	unset -v v
	command /dev/null \${v=x} 2>/dev/null
	not isset v
ENDT

TEST title="'command -v -p' searches default PATH"
	command -v -p chmod >/dev/null 2>&1 \
	&& command -v -p : >/dev/null
	e=$?
	case $e in
	( 0 )	mustNotHave BUG_CMDPV ;;
	( 1 | 127 )
		if ( eval '[[ ${.sh.version} == Version\ *\ 201?-??-?? ]]' ) 2>/dev/null; then
			xfailmsg='ksh intermittent -p bug'
			# ref.: https://github.com/att/ast/issues/426
			return 2
		fi
		mustHave BUG_CMDPV ;;
	( * )	failmsg="e = $e"
		return 1 ;;
	esac
ENDT

TEST title="'command' stops special builtins exiting"
	v=$(	readonly v=foo
		exec 2>/dev/null
		# All the "special builtins" below should fail, and not exit, so 'putln ok' is reached.
		# Ref.: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/contents.html
		# Left out are 'command exec /dev/null/nonexistent', where no shell follows the standard,
		# 'command eval "("', where too many shells either exit on syntax error or become crash-prone,
		# as well as 'command exit' and 'command return', because, well, obviously.
		command : </dev/null/nonexistent
		command . /dev/null/nonexistent
		command export v=baz
		command readonly v=bar
		command set +o bad@option
		command shift $(($# + 1))
		command times foo bar >/dev/null
		command trap foo bar baz quux
		command unset v
		if not thisshellhas QRK_BCDANGER; then
			command break
			command continue
		fi
		putln ok)
	case $v in
	( ok )	mustNotHave BUG_CMDSPEXIT ;;
	( '' )	mustHave BUG_CMDSPEXIT ;;
	( * )	return 1 ;;
	esac
ENDT

TEST title="'command -v' finds reserved words"
	v=$(command -v until)
	case $v in
	( until )
		;;
	( * )	return 1 ;;
	esac
ENDT

TEST title="'break' works from within 'eval'"
	(
		for v in 0 1 2; do
			eval "v=OK${CCn}break${CCn}v=FreeBSDvariant" 2>/dev/null
			exit 13
		done<commit_msg>tst/builtin.t: BUG_CMDSPEXIT regress: more robust testing<commit_after>#! test/for/moderni/sh
# See the file LICENSE in the main modernish directory for the licence.

# Regression tests related to (built-in) utilities of the shell.

TEST title="options to 'command' can be expansions"
	v='-v'
	MSH_NOT_FOUND_OK=1
	command $v : >/dev/null 2>&1
	v=$?
	unset -v MSH_NOT_FOUND_OK
	case $v in
	( 0 )	mustNotHave BUG_CMDOPTEXP ;;
	# test suite runs with PATH=/dev/null, so we can rely on 127 = not found
	( 127 )	mustHave BUG_CMDOPTEXP ;;
	( * )	return 1 ;;
	esac
ENDT

TEST title="'command' parses arguments properly"
	# FTL_COMMAND2P: double evaluation of grammar and/or parameter
	# expansion grammar when using 'command' with an external command.
	# (AT&T ksh88; schilytools sh <= 2017-08-14)
	unset -v v
	command /dev/null \${v=x} 2>/dev/null
	not isset v
ENDT

TEST title="'command -v -p' searches default PATH"
	command -v -p chmod >/dev/null 2>&1 \
	&& command -v -p : >/dev/null
	e=$?
	case $e in
	( 0 )	mustNotHave BUG_CMDPV ;;
	( 1 | 127 )
		if ( eval '[[ ${.sh.version} == Version\ *\ 201?-??-?? ]]' ) 2>/dev/null; then
			xfailmsg='ksh intermittent -p bug'
			# ref.: https://github.com/att/ast/issues/426
			return 2
		fi
		mustHave BUG_CMDPV ;;
	( * )	failmsg="e = $e"
		return 1 ;;
	esac
ENDT

TEST title="'command' stops special builtins exiting"
	v=$(	readonly v=foo
		exec 2>/dev/null
		# All the "special builtins" below should fail, and not exit, so 'putln ok' is reached.
		# Ref.: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/contents.html
		# Left out are 'command exec /dev/null/nonexistent', where no shell follows the standard,
		# 'command eval "("', where too many shells either exit on syntax error or become crash-prone,
		# as well as 'command exit' and 'command return', because, well, obviously.
		command : </dev/null/nonexistent	&& put BAD01
		command . /dev/null/nonexistent		&& put BAD02
		command export v=baz			&& put BAD03
		command readonly v=bar			&& put BAD04
		command set +o bad@option		&& put BAD05
		command shift $(($# + 1))		&& put BAD06
		command times foo bar >/dev/null	# many shells don't check for no arguments here; oh well
		command trap foo bar baz quux		&& put BAD08
		command unset v				&& put BAD09
		if not thisshellhas QRK_BCDANGER; then
			command break			# 'break' and 'continue' are POSIXly allowed to quietly...
			command continue		# ..."succeed" if they are used outside of a loop :-/
		fi
		putln ok)
	case $v in
	( ok )	mustNotHave BUG_CMDSPEXIT ;;
	( '' )	mustHave BUG_CMDSPEXIT ;;
	( * )	failmsg=$v; return 1 ;;
	esac
ENDT

TEST title="'command -v' finds reserved words"
	v=$(command -v until)
	case $v in
	( until )
		;;
	( * )	return 1 ;;
	esac
ENDT

TEST title="'break' works from within 'eval'"
	(
		for v in 0 1 2; do
			eval "v=OK${CCn}break${CCn}v=FreeBSDvariant" 2>/dev/null
			exit 13
		done
{'fraction_non_alphanumeric': 0.1268463810930576, 'fraction_numerical': 0.024741506646971934, 'mean_word_length': 3.3897893030794166, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 2, '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': '2331981', 'n_tokens_mistral': 2242, 'n_tokens_neox': 1992, 'n_words': 666}