| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | # vim:et:ft=sh:sts=2:sw=2 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # shUnit2 unit test common functions | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  | # Copyright 2008-2021 Kate Ward. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # Released under the Apache 2.0 license. | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  | # http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # Author: kate.ward@forestent.com (Kate Ward) | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # https://github.com/kward/shunit2 | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | ### ShellCheck (http://www.shellcheck.net/) | 
					
						
							|  |  |  | # expr may be antiquated, but it is the only solution in some cases. | 
					
						
							|  |  |  | #   shellcheck disable=SC2003 | 
					
						
							|  |  |  | # $() are not fully portable (POSIX != portable). | 
					
						
							|  |  |  | #   shellcheck disable=SC2006 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  | # Exit immediately if a simple command exits with a non-zero status. | 
					
						
							|  |  |  | set -e | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # Treat unset variables as an error when performing parameter expansion. | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | set -u | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # Set shwordsplit for zsh. | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  | [ -n "${ZSH_VERSION:-}" ] && setopt shwordsplit | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # Constants. | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # Path to shUnit2 library. Can be overridden by setting SHUNIT_INC. | 
					
						
							|  |  |  | TH_SHUNIT=${SHUNIT_INC:-./shunit2}; export TH_SHUNIT | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # Configure debugging. Set the DEBUG environment variable to any | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | # non-empty value to enable debug output, or TRACE to enable trace | 
					
						
							|  |  |  | # output. | 
					
						
							|  |  |  | TRACE=${TRACE:+'th_trace '} | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  | [ -n "${TRACE}" ] && DEBUG=1 | 
					
						
							|  |  |  | [ -z "${TRACE}" ] && TRACE=':' | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | DEBUG=${DEBUG:+'th_debug '} | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  | [ -z "${DEBUG}" ] && DEBUG=':' | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # Variables. | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | th_RANDOM=0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # Functions. | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # Logging functions. | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  | th_trace() { echo "test:TRACE $*" >&2; } | 
					
						
							|  |  |  | th_debug() { echo "test:DEBUG $*" >&2; } | 
					
						
							|  |  |  | th_info()  { echo "test:INFO $*" >&2; } | 
					
						
							|  |  |  | th_warn()  { echo "test:WARN $*" >&2; } | 
					
						
							|  |  |  | th_error() { echo "test:ERROR $*" >&2; } | 
					
						
							|  |  |  | th_fatal() { echo "test:FATAL $*" >&2; } | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # Output subtest name. | 
					
						
							|  |  |  | th_subtest() { echo " $*" >&2; } | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | th_oneTimeSetUp() { | 
					
						
							|  |  |  |   # These files will be cleaned up automatically by shUnit2. | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |   stdoutF="${SHUNIT_TMPDIR}/stdout" | 
					
						
							|  |  |  |   stderrF="${SHUNIT_TMPDIR}/stderr" | 
					
						
							|  |  |  |   returnF="${SHUNIT_TMPDIR}/return" | 
					
						
							|  |  |  |   expectedF="${SHUNIT_TMPDIR}/expected" | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  |   export stdoutF stderrF returnF expectedF | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # Generate a random number. | 
					
						
							|  |  |  | th_generateRandom() { | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |   tfgr_random=${th_RANDOM} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  |   while [ "${tfgr_random}" = "${th_RANDOM}" ]; do | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  |     # shellcheck disable=SC2039 | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  |     if [ -n "${RANDOM:-}" ]; then | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |       # $RANDOM works | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  |       # shellcheck disable=SC2039 | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |       tfgr_random=${RANDOM}${RANDOM}${RANDOM}$$ | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  |     elif [ -r '/dev/urandom' ]; then | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |       tfgr_random=`od -vAn -N4 -tu4 </dev/urandom |sed 's/^[^0-9]*//'` | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       tfgr_date=`date '+%H%M%S'` | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  |       tfgr_random=`expr "${tfgr_date}" \* $$` | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |       unset tfgr_date | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  |     [ "${tfgr_random}" = "${th_RANDOM}" ] && sleep 1 | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |   done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   th_RANDOM=${tfgr_random} | 
					
						
							|  |  |  |   unset tfgr_random | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # This section returns the data section from the specified section of a file. A | 
					
						
							|  |  |  | # data section is defined by a [header], one or more lines of data, and then a | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | # blank line. | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | th_getDataSect() { | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |   th_sgrep "\\[$1\\]" "$2" |sed '1d' | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # This function greps a section from a file. a section is defined as a group of | 
					
						
							|  |  |  | # lines preceded and followed by blank lines.. | 
					
						
							|  |  |  | th_sgrep() { | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |   th_pattern_=$1 | 
					
						
							|  |  |  |   shift | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  |   # shellcheck disable=SC2068 | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |   sed -e '/./{H;$!d;}' -e "x;/${th_pattern_}/"'!d;' $@ |sed '1d' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   unset th_pattern_ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Custom assert that checks for true return value (0), and no output to STDOUT | 
					
						
							|  |  |  | # or STDERR. If a non-zero return value is encountered, the output of STDERR | 
					
						
							|  |  |  | # will be output. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Args: | 
					
						
							|  |  |  | #  th_test_: string: name of the subtest | 
					
						
							|  |  |  | #  th_rtrn_: integer: the return value of the subtest performed | 
					
						
							|  |  |  | #  th_stdout_: string: filename where stdout was redirected to | 
					
						
							|  |  |  | #  th_stderr_: string: filename where stderr was redirected to | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | th_assertTrueWithNoOutput() { | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |   th_test_=$1 | 
					
						
							|  |  |  |   th_rtrn_=$2 | 
					
						
							|  |  |  |   th_stdout_=$3 | 
					
						
							|  |  |  |   th_stderr_=$4 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  |   assertEquals "${th_test_}: expected return value of true" "${SHUNIT_TRUE}" "${th_rtrn_}" | 
					
						
							|  |  |  |   assertFalse "${th_test_}: expected no output to STDOUT" "[ -s '${th_stdout_}' ]" | 
					
						
							|  |  |  |   assertFalse "${th_test_}: expected no output to STDERR" "[ -s '${th_stderr_}' ]" | 
					
						
							|  |  |  |   # shellcheck disable=SC2166 | 
					
						
							|  |  |  |   if [ -s "${th_stdout_}" -o -s "${th_stderr_}" ]; then | 
					
						
							|  |  |  |     _th_showOutput "${SHUNIT_FALSE}" "${th_stdout_}" "${th_stderr_}" | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   unset th_test_ th_rtrn_ th_stdout_ th_stderr_ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Custom assert that checks for non-zero return value, output to STDOUT, but no | 
					
						
							|  |  |  | # output to STDERR. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Args: | 
					
						
							|  |  |  | #  th_test_: string: name of the subtest | 
					
						
							|  |  |  | #  th_rtrn_: integer: the return value of the subtest performed | 
					
						
							|  |  |  | #  th_stdout_: string: filename where stdout was redirected to | 
					
						
							|  |  |  | #  th_stderr_: string: filename where stderr was redirected to | 
					
						
							|  |  |  | th_assertFalseWithOutput() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   th_test_=$1 | 
					
						
							|  |  |  |   th_rtrn_=$2 | 
					
						
							|  |  |  |   th_stdout_=$3 | 
					
						
							|  |  |  |   th_stderr_=$4 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  |   assertNotEquals "${th_test_}: expected non-true return value" "${SHUNIT_TRUE}" "${th_rtrn_}" | 
					
						
							|  |  |  |   assertTrue "${th_test_}: expected output to STDOUT" "[ -s '${th_stdout_}' ]" | 
					
						
							|  |  |  |   assertFalse "${th_test_}: expected no output to STDERR" "[ -s '${th_stderr_}' ]" | 
					
						
							|  |  |  |   # shellcheck disable=SC2166 | 
					
						
							|  |  |  |   if ! [ -s "${th_stdout_}" -a ! -s "${th_stderr_}" ]; then | 
					
						
							|  |  |  |     _th_showOutput "${SHUNIT_FALSE}" "${th_stdout_}" "${th_stderr_}" | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   unset th_test_ th_rtrn_ th_stdout_ th_stderr_ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Custom assert that checks for non-zero return value, no output to STDOUT, but | 
					
						
							|  |  |  | # output to STDERR. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Args: | 
					
						
							|  |  |  | #  th_test_: string: name of the subtest | 
					
						
							|  |  |  | #  th_rtrn_: integer: the return value of the subtest performed | 
					
						
							|  |  |  | #  th_stdout_: string: filename where stdout was redirected to | 
					
						
							|  |  |  | #  th_stderr_: string: filename where stderr was redirected to | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | th_assertFalseWithError() { | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |   th_test_=$1 | 
					
						
							|  |  |  |   th_rtrn_=$2 | 
					
						
							|  |  |  |   th_stdout_=$3 | 
					
						
							|  |  |  |   th_stderr_=$4 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  |   assertFalse "${th_test_}: expected non-zero return value" "${th_rtrn_}" | 
					
						
							|  |  |  |   assertFalse "${th_test_}: expected no output to STDOUT" "[ -s '${th_stdout_}' ]" | 
					
						
							|  |  |  |   assertTrue "${th_test_}: expected output to STDERR" "[ -s '${th_stderr_}' ]" | 
					
						
							|  |  |  |   # shellcheck disable=SC2166 | 
					
						
							|  |  |  |   if ! [ ! -s "${th_stdout_}" -a -s "${th_stderr_}" ]; then | 
					
						
							|  |  |  |     _th_showOutput "${SHUNIT_FALSE}" "${th_stdout_}" "${th_stderr_}" | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   unset th_test_ th_rtrn_ th_stdout_ th_stderr_ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Some shells, zsh on Solaris in particular, return immediately from a sub-shell | 
					
						
							|  |  |  | # when a non-zero return value is encountered. To properly catch these values, | 
					
						
							|  |  |  | # they are either written to disk, or recognized as an error the file is empty. | 
					
						
							|  |  |  | th_clearReturn() { cp /dev/null "${returnF}"; } | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | th_queryReturn() { | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  |   if [ -s "${returnF}" ]; then | 
					
						
							|  |  |  |     th_return=`cat "${returnF}"` | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |   else | 
					
						
							|  |  |  |     th_return=${SHUNIT_ERROR} | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  |   export th_return | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Providing external and internal calls to the showOutput helper function. | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | th_showOutput() { _th_showOutput "$@"; } | 
					
						
							|  |  |  | _th_showOutput() { | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  |   if isSkipping; then | 
					
						
							|  |  |  |     return | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   _th_return_="${1:-${returnF}}" | 
					
						
							|  |  |  |   _th_stdout_="${2:-${stdoutF}}" | 
					
						
							|  |  |  |   _th_stderr_="${3:-${stderrF}}" | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  |   if [ "${_th_return_}" != "${SHUNIT_TRUE}" ]; then | 
					
						
							|  |  |  |     # shellcheck disable=SC2166 | 
					
						
							|  |  |  |     if [ -n "${_th_stdout_}" -a -s "${_th_stdout_}" ]; then | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |       echo '>>> STDOUT' >&2 | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  |       cat "${_th_stdout_}" >&2 | 
					
						
							|  |  |  |       echo '<<< STDOUT' >&2 | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |     fi | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  |     # shellcheck disable=SC2166 | 
					
						
							|  |  |  |     if [ -n "${_th_stderr_}" -a -s "${_th_stderr_}" ]; then | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |       echo '>>> STDERR' >&2 | 
					
						
							| 
									
										
										
										
											2022-07-07 14:22:35 +00:00
										 |  |  |       cat "${_th_stderr_}" >&2 | 
					
						
							|  |  |  |       echo '<<< STDERR' >&2 | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  |     fi | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   unset _th_return_ _th_stdout_ _th_stderr_ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2018-07-02 21:25:49 +00:00
										 |  |  | # Main. | 
					
						
							| 
									
										
										
										
											2016-08-22 08:26:38 +00:00
										 |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ${TRACE} 'trace output enabled' | 
					
						
							|  |  |  | ${DEBUG} 'debug output enabled' |