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

# Sanity checks to verify correct modernish initialisation.

doTest1() {
	title='availability of POSIX utils in $DEFPATH'
	# Modernish and its main modules depend on these POSIX utilities
	# to be installed in $(getconf PATH).
	# TODO: periodically update
	push IFS PATH cmd p
	IFS=:
	PATH=$DEFPATH
	for cmd in \
		[ \
		awk \
		bc \
		cat \
		dd \
		echo \
		expr \
		fold \
		grep \
		iconv \
		id \
		kill \
		ls \
		mkdir \
		nl \
		paste \
		printf \
		ps \
		rm \
		sed \
		sh \
		sort \
		stty \
		test \
		tput \
		tr \
		wc
	do
		for p in $DEFPATH; do
			can exec $p/$cmd && continue 2
		done
		xfailmsg=${xfailmsg:+${xfailmsg}, }\'$cmd\'
	done
	pop IFS PATH cmd p
	if isset xfailmsg; then
		if eq opt_q 2; then
			# We xfail rather than fail because it's not a bug in modernish or the shell. However,
			# if we're testing in extra-quiet mode, we might be running from install.sh. The xfails
			# are not displayed, but we still really want to warn the user about missing utilities.
			contains $xfailmsg ',' && v=utilities || v=utility
			putln "  ${tBold}WARNING:${tReset} Standard $v missing in $DEFPATH: ${tRed}${xfailmsg}${tReset}"
		fi
		xfailmsg="missing: $xfailmsg"
		return 2
	fi
}

doTest2() {
	title='ASCII chars and control char constants'
	# Test the correctness of $CC01..$CC1F,$CC7F (which are all concatenated in $CONTROLCHARS). These
	# are initialised quickly by including their control character values directly in bin/modernish.
	# Most editors handle this gracefully, but check here that no corruption has occurred.
	# Include all the other ASCII characters in the test as well while we're at it;
	# $ASCIICHARS starts with $CONTROLCHARS.
	case $ASCIICHARS in
	( $'\1\2\3\4\5\6\a\b\t\n\v\f\r\16\17\20\21\22\23\24\25\26\27\30\31\32\e\34\35\36\37\177 "#$&'\
\'$'()*;<>?[\\\\\\]`{|}~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz%+,./:=@_^!-' )
		# escaping a ' like use $'foo\'bar' causes syntax error on shells without CESCQUOT; use $'foo'\'$'bar'.
		mustHave CESCQUOT ;;
	( "$(PATH=$DEFPATH command printf \
	   '\1\2\3\4\5\6\a\b\t\n\v\f\r\16\17\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37\177 "#$&'\
\''()*;<>?[\\\\\\]`{|}~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz%%+,./:=@_^!-')" )		# "
		# note that CESCQUOT supports \e, but POSIX printf(1) doesn't
		# and that the % must be doubled on printf.
		identic $'a' '$a' && mustNotHave CESCQUOT ;;
	( * )	return 1 ;;
	esac
}

doTest3() {
	title='check for fatal bug/quirk combinations'<commit_msg>tests/@sanitychecks.t: recognise Busybox ash CESCQUOT variant without \e<commit_after>#! test/for/moderni/sh
# See the file LICENSE in the main modernish directory for the licence.

# Sanity checks to verify correct modernish initialisation.

doTest1() {
	title='availability of POSIX utils in $DEFPATH'
	# Modernish and its main modules depend on these POSIX utilities
	# to be installed in $(getconf PATH).
	# TODO: periodically update
	push IFS PATH cmd p
	IFS=:
	PATH=$DEFPATH
	for cmd in \
		[ \
		awk \
		bc \
		cat \
		dd \
		echo \
		expr \
		fold \
		grep \
		iconv \
		id \
		kill \
		ls \
		mkdir \
		nl \
		paste \
		printf \
		ps \
		rm \
		sed \
		sh \
		sort \
		stty \
		test \
		tput \
		tr \
		wc
	do
		for p in $DEFPATH; do
			can exec $p/$cmd && continue 2
		done
		xfailmsg=${xfailmsg:+${xfailmsg}, }\'$cmd\'
	done
	pop IFS PATH cmd p
	if isset xfailmsg; then
		if eq opt_q 2; then
			# We xfail rather than fail because it's not a bug in modernish or the shell. However,
			# if we're testing in extra-quiet mode, we might be running from install.sh. The xfails
			# are not displayed, but we still really want to warn the user about missing utilities.
			contains $xfailmsg ',' && v=utilities || v=utility
			putln "  ${tBold}WARNING:${tReset} Standard $v missing in $DEFPATH: ${tRed}${xfailmsg}${tReset}"
		fi
		xfailmsg="missing: $xfailmsg"
		return 2
	fi
}

doTest2() {
	title='ASCII chars and control char constants'
	# Test the correctness of $CC01..$CC1F,$CC7F (which are all concatenated in $CONTROLCHARS). These
	# are initialised quickly by including their control character values directly in bin/modernish.
	# Most editors handle this gracefully, but check here that no corruption has occurred.
	# Include all the other ASCII characters in the test as well while we're at it;
	# $ASCIICHARS starts with $CONTROLCHARS.
	case $ASCIICHARS in
	( $'\1\2\3\4\5\6\a\b\t\n\v\f\r\16\17\20\21\22\23\24\25\26\27\30\31\32\e\34\35\36\37\177 "#$&'\
\'$'()*;<>?[\\\\\\]`{|}~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz%+,./:=@_^!-' )
		# escaping a ' like use $'foo\'bar' causes syntax error on shells without CESCQUOT; use $'foo'\'$'bar'.
		mustHave CESCQUOT ;;
	( $'\1\2\3\4\5\6\a\b\t\n\v\f\r\16\17\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37\177 "#$&'\
\'$'()*;<>?[\\\\\\]`{|}~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz%+,./:=@_^!-' )
		# busybox ash with "bash compatibility" compiled in has CESCQUOT without \e.
		mustHave CESCQUOT && okmsg="$okmsg (no \\e)" ;;
	( "$(PATH=$DEFPATH command printf \
	   '\1\2\3\4\5\6\a\b\t\n\v\f\r\16\17\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37\177 "#$&'\
\''()*;<>?[\\\\\\]`{|}~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz%%+,./:=@_^!-')" )		# "
		# note that CESCQUOT supports \e, but POSIX printf(1) doesn't
		# and that the % must be doubled on printf.
		identic $'a' '$a' && mustNotHave CESCQUOT ;;
	( * )	return 1 ;;
	esac
}

doTest3() {
	title='check for fatal bug/quirk combinations'
{'fraction_non_alphanumeric': 0.15240120503278398, 'fraction_numerical': 0.052277157540315436, 'mean_word_length': 3.140865737344094, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '1353809', 'n_tokens_mistral': 2591, 'n_tokens_neox': 2223, 'n_words': 614}