Skip to content

Commit

Permalink
Unify the unit test framework
Browse files Browse the repository at this point in the history
This modifies how ksh unit tests are run to have a consistent
environment (i.e., preamble) and post test behavior (i.e., postscript).
There was at least one unit test that did not correctly report the
number of errors via the exit status.

Each test is now run in a unique temporary directory. If the test
doesn't complete without error the temp dir is not removed. This makes
it easier when running the tests on a personal system (rather than, say,
a CI environment like Travis) to explore the state of the unit test.

The `HOME` env var is now set to the temp dir to help keep the test from
modifying the user's environment (e.g., their ~/.sh_history file).

Resolves #429
Resolves #477
  • Loading branch information
krader1961 committed Apr 19, 2018
1 parent e6df102 commit f5b9c64
Show file tree
Hide file tree
Showing 59 changed files with 325 additions and 1,006 deletions.
9 changes: 4 additions & 5 deletions src/cmd/ksh93/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ shcomp_exe = executable('shcomp', shcomp_files, c_args: shcomp_c_args ,
install_man('ksh.1')

test_dir = join_paths(meson.current_source_dir(), 'tests')
test_driver = join_paths(test_dir, 'util', 'run_test.sh')

# This variable is used by some tests while executing subtests
shell_var = 'SHELL=' + ksh93_exe.full_path()
Expand All @@ -78,8 +79,6 @@ ld_library_path = 'LD_LIBRARY_PATH=' + ':'.join(
# explicitly set string to 'C' or 'POSIX' here if possible.
locales = ['', 'C.UTF-8']

shcomp_test_path = join_paths(test_dir, 'shcomptest')

# Each entry in `all_tests` is an array of one or two elements. The first
# element is the test name. The second is an optional timeout if the default
# timeout of 30 seconds is too short. Try to keep the list sorted.
Expand Down Expand Up @@ -108,21 +107,21 @@ shcomp_tests_to_skip = ['io', 'namespace', 'treemove']
foreach testspec : all_tests
testname = testspec[0]
timeout = (testspec.length() == 2) ? testspec[1] : default_timeout
test_path = join_paths(test_dir, testname + '.sh')
foreach locale : locales
lang_var = 'LANG=' + locale
if locale != ''
locale = '/' + locale
endif
test(testname + locale, ksh93_exe, timeout: timeout, args: [test_path, testname],
test(testname + locale, ksh93_exe, timeout: timeout,
args: [test_driver, testname],
env: [shell_var, lang_var, ld_library_path])
endforeach

# shcomp tests
lang_var = 'LANG='
if not shcomp_tests_to_skip.contains(testname)
test(testname + '/shcomp', ksh93_exe, timeout: timeout,
args: [ shcomp_test_path, test_path, testname],
args: [ test_driver, 'shcomp', testname],
env: [shell_var, lang_var, shcomp_var, ld_library_path])
endif
endforeach
16 changes: 0 additions & 16 deletions src/cmd/ksh93/tests/alias.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@
# #
########################################################################

function err_exit
{
print -u2 -n "\t"
print -u2 -r ${Command}[$1]: "${@:2}"
let Errors+=1
}
alias err_exit='err_exit $LINENO'

Command=${0##*/}
integer Errors=0

tmp=$(mktemp -dt ksh.${Command}.XXXXXXXXXX) || { err_exit mktemp -dt failed; exit 1; }
trap "cd /; rm -rf $tmp" EXIT

alias foo='print hello'
if [[ $(foo) != hello ]]
then
Expand Down Expand Up @@ -132,5 +118,3 @@ for i in compound float integer nameref
do
[[ $i=$(whence $i) == "$(alias $i)" ]] || err_exit "whence $i not matching $(alias $i)"
done

exit $((Errors<125?Errors:125))
12 changes: 0 additions & 12 deletions src/cmd/ksh93/tests/append.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@
# #
########################################################################

function err_exit
{
print -u2 -n "\t"
print -u2 -r ${Command}[$1]: "${@:2}"
let Errors+=1
}
alias err_exit='err_exit $LINENO'

Command=${0##*/}
integer Errors=0
{
x=abc
x+=def ;} 2> /dev/null
Expand Down Expand Up @@ -143,5 +133,3 @@ foo=()
foo+=(x=1 y=2)
foo+=(x=3 y=4)
[[ ${!foo[@]} == '0 1' ]] || err_exit 'append to unset array of types not working'

exit $((Errors<125?Errors:125))
16 changes: 0 additions & 16 deletions src/cmd/ksh93/tests/arith.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@
# #
########################################################################

function err_exit
{
print -u2 -n "\t"
print -u2 -r ${Command}[$1]: "${@:2}"
let Errors+=1
}
alias err_exit='err_exit $LINENO'

Command=${0##*/}
integer Errors=0

tmp=$(mktemp -dt ksh.${Command}.XXXXXXXXXX) || { err_exit mktemp -dt failed; exit 1; }
trap "cd /; rm -rf $tmp" EXIT

trap '' FPE # NOTE: osf.alpha requires this (no ieee math)

integer x=1 y=2 z=3
Expand Down Expand Up @@ -1020,5 +1006,3 @@ integer -u u=123
(( u.MAX < (1<<31) )) && err_exit '$((i.MAX)) not workng when i is unsigned int'

[[ $(( (2**32) << 67 )) == 0 ]] || err_exit 'left shift count 67 is non-zero'

exit $((Errors<125?Errors:125))
15 changes: 0 additions & 15 deletions src/cmd/ksh93/tests/arrays.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@
# David Korn <dgkorn@gmail.com> #
# #
########################################################################
function err_exit
{
print -u2 -n "\t"
print -u2 -r ${Command}[$1]: "${@:2}"
let Errors+=1
}
alias err_exit='err_exit $LINENO'

Command=${0##*/}
integer Errors=0

tmp=$(mktemp -dt ksh.${Command}.XXXXXXXXXX) || { err_exit mktemp -dt failed; exit 1; }
trap "cd /; rm -rf $tmp" EXIT

function fun
{
Expand Down Expand Up @@ -881,5 +868,3 @@ unset foo bar
typeset -a foo=([1]=w [2]=x) bar=(a b c)
foo+=("${bar[@]}")
[[ $(typeset -p foo) == 'typeset -a foo=([1]=w [2]=x [3]=a [4]=b [5]=c)' ]] || err_exit 'Appending does not work if array contains empty indexes'
exit $((Errors<125?Errors:125))
11 changes: 0 additions & 11 deletions src/cmd/ksh93/tests/arrays2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,7 @@
# David Korn <dgkorn@gmail.com> #
# #
########################################################################
function err_exit
{
print -u2 -n "\t"
print -u2 -r ${Command}[$1]: "${@:2}"
let Errors+=1
}
alias err_exit='err_exit $LINENO'

Command=${0##*/}
integer Errors=0
for ((i=0; i < 4; i++ ))
do
for ((j=0; j < 5; j++ ))
Expand Down Expand Up @@ -280,5 +271,3 @@ typeset -A foo[bar]
foo[bar][x]=2
(( foo[bar][x]++ ))
[[ ${foo[bar][x]} == 3 ]] || err_ext 'subscrit gets added incorrectly to an associat array when ++ operator is called'

exit $((Errors<125?Errors:125))
16 changes: 0 additions & 16 deletions src/cmd/ksh93/tests/attributes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@
# #
########################################################################

function err_exit
{
print -u2 -n "\t"
print -u2 -r ${Command}[$1]: "${@:2}"
let Errors+=1
}
alias err_exit='err_exit $LINENO'

Command=${0##*/}
integer Errors=0

tmp=$(mktemp -dt ksh.${Command}.XXXXXXXXXX) || { err_exit mktemp -dt failed; exit 1; }
trap "cd /; rm -rf $tmp" EXIT

r=readonly u=Uppercase l=Lowercase i=22 i8=10 L=abc L5=def uL5=abcdef xi=20
x=export t=tagged H=hostname LZ5=026 RZ5=026 Z5=123 lR5=ABcdef R5=def n=l
for option in u l i i8 L L5 LZ5 RZ5 Z5 r x H t R5 uL5 lR5 xi n
Expand Down Expand Up @@ -585,5 +571,3 @@ do
read -r -N6 var
[[ $var == dHdvdG93 ]] && ((i !=2)) && err_exit 'loop optimization bug with typeset -b variables'
done <<< 'twotowthreetfourro'

exit $((Errors<125?Errors:125))
47 changes: 17 additions & 30 deletions src/cmd/ksh93/tests/basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@
# David Korn <dgkorn@gmail.com> #
# #
########################################################################
function err_exit
{
print -u2 -n "\t"
print -u2 -r ${Command}[$1]: "${@:2}"
let Errors+=1
}
alias err_exit='err_exit $LINENO'

Command=${0##*/}
integer Errors=0

tmp=$(mktemp -dt ksh.${Command}.XXXXXXXXXX) || { err_exit mktemp -dt failed; exit 1; }
trap "cd /; rm -rf $tmp" EXIT

# test basic file operations like redirection, pipes, file expansion
set -- \
Expand Down Expand Up @@ -83,23 +70,25 @@ then
print -u2 "optimizer bug with file expansion"
fi

rm -f out foobar
mkdir top_dir
cd top_dir
mkdir dir
if [[ $(print */) != dir/ ]]
then
err_exit 'file expansion with trailing / not working'
fi
actual="$(print */)"
expect=dir/
[[ $actual == $expect ]] ||
err_exit 'file expansion with trailing / not working' "$expect" "$actual"

if [[ $(print *) != dir ]]
then
err_exit 'file expansion with single file not working'
fi
actual="$(print */)"
expect=dir/
[[ $actual == $expect ]] ||
err_exit 'file expansion with single file not working' "$expect" "$actual"

print hi > .foo
if [[ $(print *) != dir ]]
then
err_exit 'file expansion leading . not working'
fi
actual="$(print *)"
expect=dir
[[ $actual == $expect ]] ||
err_exit 'file expansion leading . not working' "$expect" "$actual"
cd ..

date > dat1 || err_exit "date > dat1 failed"
test -r dat1 || err_exit "dat1 is not readable"
Expand Down Expand Up @@ -278,15 +267,15 @@ then
err_exit "subshell in command substitution fails"
fi

exec 9>& 1
exec 7>& 1
exec 1>&-
x=$(print hello)
if [[ $x != hello ]]
then
err_exit "command subsitution with stdout closed failed"
fi

exec >& 9
exec >& 7
cd $pwd
x=$(cat <<\! | $SHELL
/bin/echo | /bin/cat
Expand Down Expand Up @@ -671,5 +660,3 @@ $SHELL -c 'kill -0 123456789123456789123456789' 2> /dev/null && err_exit 'kill n
$SHELL -xc '$(LD_LIBRARY_PATH=$LD_LIBRARY_PATH exec $SHELL -c :)' > /dev/null 2>&1 || err_exit "ksh -xc '(name=value exec ksh)' fails with err=$?"
$SHELL 2> /dev/null -c $'for i;\ndo :;done' || err_exit 'for i ; <newline> not vaid'
exit $((Errors<125?Errors:125))
15 changes: 0 additions & 15 deletions src/cmd/ksh93/tests/bracket.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@
# David Korn <dgkorn@gmail.com> #
# #
########################################################################
function err_exit
{
print -u2 -n "\t"
print -u2 -r ${Command}[$1]: "${@:2}"
let Errors+=1
}
alias err_exit='err_exit $LINENO'

Command=${0##*/}
integer Errors=0

tmp=$(mktemp -dt ksh.${Command}.XXXXXXXXXX) || { err_exit mktemp -dt failed; exit 1; }
trap "cd /; rm -rf $tmp" EXIT

null=''
if [[ ! -z $null ]]
Expand Down Expand Up @@ -474,5 +461,3 @@ x=10
# POSIX specifies that on error, test builtin should always return value > 1
test 123 -eq 123x 2>/dev/null
[[ $? -ge 2 ]] || err_exit 'test builtin should return value greater than 1 on error'

exit $((Errors<125?Errors:125))
30 changes: 0 additions & 30 deletions src/cmd/ksh93/tests/builtin_poll.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,6 @@
# Test module to check the ksh93 "poll" builtin
#

# test setup
function err_exit
{
print -u2 -n '\t'
print -u2 -r ${Command}[$1]: "${@:2}"
(( Errors++ ))
}
alias err_exit='err_exit $LINENO'

set -o nounset
Command=${0##*/}
integer Errors=0

typeset ocwd
typeset tmpdir

# create temporary test directory
ocwd="${PWD}"
tmpdir=$(mktemp -dt ksh.${Command}.XXXXXXXXXX ) || err_exit 'Cannot create temporary directory.'

cd "${tmpdir}" || { err_exit "cd ${tmpdir} failed." ; exit $((Errors<125?Errors:125)) ; }


# basic tests
function test1
{
Expand Down Expand Up @@ -472,10 +449,3 @@ builtin rmdir || { err_exit 'rmdir builtin not found.'; exit 1; }
test1
test_sparse_array1
test_fifo_circus1

# cleanup
cd "${ocwd}"
rmdir "${tmpdir}" || err_exit "Cannot remove temporary directory ${tmpdir}."

# tests done
exit $((Errors<125?Errors:125))
Loading

0 comments on commit f5b9c64

Please sign in to comment.