: osh - " Force sh(1), csh(1), and other shells to exit w/ error! " <'' ;;;
:
: "  The author of this file, J.A. Neitzel <jan (at) etsh (dot) io>,  "
: "  hereby grants it to the public domain.                           "
:
: "  From:  https://etsh.io/regression_tests  "
:

:
: "  Exit w/ zero status (true) when all tests pass.       "
: "  Exit w/ non-zero status (false) when any tests fail.  "
:
: "  usage: ../osh run [-newlog] {osh | sh6}  "
:

: >>$0'' <'' ; : >>$1'' <'' ; : >>$2'' <'' ; : >>$3'' <''

if X$u = Xroot if { fd2 -e echo Cannot run tests as superuser } \
	if { exit } if A = B
if X$0 != Xrun -a X$0 != X./run if { fd2 -e echo Wrong directory } \
	if { exit } if A = B
if $# = # if { fd2 -e echo Invalid shell - osh\(1\) required } \
	if { exit } if A = B

trap '' 1 2 3 13 15 ; : trap
setenv PATH ..:./bin:$p ; : printenv PATH
umask 22 ; : umask
rm -f TestFailCount TestPassCount syntax_error
( : ) >TestFailCount ; ( : ) >TestPassCount

set C $#

if $# = 3 -a X$1 = X-newlog -a X$2 = Xosh -a X$3 = Xsh6 goto NewLog

: Again - " Check for correct usage and/or test other binary. "

	if ! \( \( $# = 1 -o $# = 2 \) -a \( X$1 = Xosh -o X$1 = Xsh6 \) \) \
		if { fd2 -e echo usage: ../osh run \[-newlog\] osh sh6 } \
			if { exit } if A = B
	: fallthrough

: NewLog - " Generate new test logs if needed. "

	if X$1 != X-newlog goto BinCheck
	rm -f test0[1-8]_[os][sh][h6].log
	fd2 $2 test01 >test01_$2.log ; fd2 $3 test01 >test01_$3.log
	fd2 $2 test02 >test02_$2.log ; fd2 $3 test02 >test02_$3.log
	fd2 $2 test03 >test03_$2.log ; fd2 $3 test03 >test03_$3.log
	fd2 -f/dev/null $2 test04 >test04_$2.log
	fd2 -f/dev/null $3 test04 >test04_$3.log
	fd2 $2 test05 $2 >test05_$2.log ; fd2 $3 test05 $3 >test05_$3.log
	rm -f syntax_error
	fd2 $2 test08 >test08_$2.log ; fd2 $3 test08 >test08_$3.log
	( : ) >test06_$2.log ; ( : ) >test07_$2.log
	fd2 $2 test06 >test06_$2.log
	fd2 $2 test07 >test07_$2.log
	shift
	: fallthrough

: BinCheck - " Ensure that needed binaries are present and executable. "

if ! -x ../$1 if { fd2 -e echo $1: Missing or not executable } \
	if { exit } if A = B
if X$1 = Xosh goto Jump
	if \( $C -eq 2 -a $# -eq 2 \) -o \( $C -eq 1 -a $# -eq 1 \) \
		fd2 -e -- now
	: which sh6
	goto Run
: Jump
	fd2 -e -- now
	: which osh
	: fallthrough

: Run - " Run appropriate tests w/ $1 binary. "

	fd2 -e -- echo -n Testing $1 binary\ 
	fd2 $1 test01 | diff -u test01_$1.log -
	if $? != 0 if { fd2 -e echo $1: Test 1 Failed } echo >>TestFailCount
	echo >>TestPassCount
	fd2 -e -- echo -n .\ 
	fd2 $1 test02 | diff -u test02_$1.log -
	if $? != 0 if { fd2 -e echo $1: Test 2 Failed } echo >>TestFailCount
	echo >>TestPassCount
	fd2 -e -- echo -n .\ 
	fd2 $1 test03 | diff -u test03_$1.log -
	if $? != 0 if { fd2 -e echo $1: Test 3 Failed } echo >>TestFailCount
	echo >>TestPassCount
	fd2 -e -- echo -n .\ 
	fd2 -f/dev/null $1 test04 | diff -u test04_$1.log -
	if $? != 0 if { fd2 -e echo $1: Test 4 Failed } echo >>TestFailCount
	echo >>TestPassCount
	fd2 -e -- echo -n .\ 
	fd2 $1 test05 $1 | diff -u test05_$1.log -
	if $? != 0 if { fd2 -e echo $1: Test 5 Failed } echo >>TestFailCount
	echo >>TestPassCount
	fd2 -e -- echo -n .\ 
	if X$1 = Xsh6 goto Next
		: " Tests 6 and 7 are for osh only. "
		rm -f syntax_error
		fd2 $1 test06 | diff -u test06_$1.log -
		if $? != 0 if { fd2 -e echo $1: Test 6 Failed } \
			echo >>TestFailCount
		echo >>TestPassCount
		fd2 -e -- echo -n .\ 
		fd2 $1 test07 | diff -u test07_$1.log -
		if $? != 0 if { fd2 -e echo $1: Test 7 Failed } \
			echo >>TestFailCount
		echo >>TestPassCount
		fd2 -e -- echo -n .\ 
		: fallthrough
	: Next
	fd2 $1 test08 | diff -u test08_$1.log -
	if $? != 0 if { fd2 -e echo $1: Test 8 Failed } echo >>TestFailCount
	echo >>TestPassCount
	fd2 -e echo . Done .
	shift
	if $# = 0 goto Done
	goto Again

: Done - " Print final results and exit false/true as needed. "

	fd2 -e -- now
	if ! -s TestFailCount goto TestsPassed
		( wc -l <TestFailCount | tr -d ' \t\n' ; \
		  echo ' Test(s) Failed' ) | fd2 -e cat
		rm -f TestFailCount TestPassCount syntax_error ; if A = B ; exit
	: TestsPassed
		( wc -l <TestPassCount | tr -d ' \t\n' ; \
		  echo ' Test(s) Passed' ) | fd2 -e cat
		rm -f TestFailCount TestPassCount syntax_error ; : zero status
