kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 1 | # vim:et:ft=sh:sts=2:sw=2 |
| 2 | # |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 3 | # shFlags unit test common functions |
| 4 | |
| 5 | __th_skipping=0 |
| 6 | |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 7 | # treat unset variables as an error |
| 8 | set -u |
| 9 | |
kate.ward | 1d0ecc4 | 2008-07-11 15:33:23 +0000 | [diff] [blame] | 10 | # set shwordsplit for zsh |
| 11 | [ -n "${ZSH_VERSION:-}" ] && setopt shwordsplit |
| 12 | |
kate.ward | 2fb41e3 | 2008-07-10 18:42:46 +0000 | [diff] [blame] | 13 | # my name |
| 14 | TH_MY_NAME=`basename "$0"` |
| 15 | |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 16 | # path to shFlags library. can be overridden by setting SHFLAGS_INC |
| 17 | TH_SHFLAGS=${SHFLAGS_INC:-./shflags} |
| 18 | |
| 19 | # path to shUnit2 library. can be overridden by setting SHUNIT_INC |
| 20 | TH_SHUNIT=${SHUNIT_INC:-../lib/shunit2} |
| 21 | |
| 22 | TH_BOOL_VALID='true t 0 false f 1' |
kate.ward | e10dd53 | 2013-01-04 21:52:23 +0000 | [diff] [blame] | 23 | TH_BOOL_INVALID='123 123.0 invalid' |
| 24 | TH_FLOAT_VALID='-1234.0 -1.0 -.123 0.0 0. .123 1.0 1234.0' |
kate.ward | c66a5fc | 2013-01-12 23:10:15 +0000 | [diff] [blame] | 25 | TH_FLOAT_INVALID='true false 1.2.3 -1.2.3 ""' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 26 | TH_INT_VALID='-1234 -1 0 1 1234' |
kate.ward | c66a5fc | 2013-01-12 23:10:15 +0000 | [diff] [blame] | 27 | TH_INT_INVALID='true false -1.0 -.123 0.0 .123 1.0 ""' |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 28 | |
| 29 | # |
| 30 | # test helper functions |
| 31 | # |
| 32 | |
| 33 | # message functions |
| 34 | th_trace() { echo "test:TRACE $@" >&2; } |
| 35 | th_debug() { echo "test:DEBUG $@" >&2; } |
| 36 | th_info() { echo "test:INFO $@" >&2; } |
| 37 | th_warn() { echo "test:WARN $@" >&2; } |
| 38 | th_error() { echo "test:ERROR $@" >&2; } |
| 39 | th_fatal() { echo "test:FATAL $@" >&2; } |
| 40 | |
kate.ward | 1b600c5 | 2008-11-12 21:26:05 +0000 | [diff] [blame] | 41 | th_oneTimeSetUp() |
| 42 | { |
| 43 | # load shFlags |
| 44 | [ -n "${ZSH_VERSION:-}" ] && FLAGS_PARENT=$0 |
| 45 | . ${TH_SHFLAGS} |
| 46 | |
| 47 | # these files will be cleaned up automatically by shUnit2 |
kate.ward | 1cb7960 | 2011-06-10 11:15:49 +0000 | [diff] [blame] | 48 | tmpDir=${SHUNIT_TMPDIR} |
kate.ward | 1b600c5 | 2008-11-12 21:26:05 +0000 | [diff] [blame] | 49 | stdoutF="${tmpDir}/stdout" |
| 50 | stderrF="${tmpDir}/stderr" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 51 | returnF="${tmpDir}/return" |
kate.ward | 1b600c5 | 2008-11-12 21:26:05 +0000 | [diff] [blame] | 52 | expectedF="${tmpDir}/expected" |
| 53 | } |
| 54 | |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 55 | th_showOutput() |
| 56 | { |
| 57 | _th_rtrn=$1 |
| 58 | _th_stdout=$2 |
| 59 | _th_stderr=$3 |
| 60 | |
| 61 | isSkipping |
| 62 | if [ $? -eq ${SHUNIT_FALSE} -a ${_th_rtrn} != ${FLAGS_TRUE} ]; then |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 63 | if [ -n "${_th_stdout}" -a -s "${_th_stdout}" ]; then |
| 64 | echo '>>> STDOUT' >&2 |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 65 | cat "${_th_stdout}" >&2 |
| 66 | fi |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 67 | if [ -n "${_th_stderr}" -a -s "${_th_stderr}" ]; then |
| 68 | echo '>>> STDERR' >&2 |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 69 | cat "${_th_stderr}" >&2 |
| 70 | fi |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 71 | if [ -n "${_th_stdout}" -o -n "${_th_stderr}" ]; then |
kate.ward | da1f856 | 2013-01-05 13:56:11 +0000 | [diff] [blame] | 72 | echo '<<< end output' >&2 |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 73 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 74 | fi |
| 75 | |
| 76 | unset _th_rtrn _th_stdout _th_stderr |
| 77 | } |
| 78 | |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 79 | # Some shells, zsh on Solaris in particular, return immediately from a sub-shell |
| 80 | # when a non-zero return value is encountered. To properly catch these values, |
| 81 | # they are either written to disk, or recognized as an error the file is empty. |
| 82 | th_clearReturn() { cp /dev/null "${returnF}"; } |
| 83 | th_queryReturn() |
| 84 | { |
| 85 | if [ -s "${returnF}" ]; then |
| 86 | th_return=`cat "${returnF}"` |
| 87 | else |
| 88 | th_return=${SHUNIT_ERROR} |
| 89 | fi |
| 90 | } |
| 91 | |
kate.ward | 20d0678 | 2008-10-21 19:57:19 +0000 | [diff] [blame] | 92 | _th_assertMsg() |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 93 | { |
kate.ward | 20d0678 | 2008-10-21 19:57:19 +0000 | [diff] [blame] | 94 | _th_alert_type_=$1 |
| 95 | _th_alert_msg_=$2 |
| 96 | _th_msg_=$3 |
| 97 | |
kate.ward | 2f3cad9 | 2008-10-21 23:29:23 +0000 | [diff] [blame] | 98 | case ${_th_alert_type_} in |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 99 | WARN) _th_alert_str_='a warning' ;; |
| 100 | ERROR) _th_alert_str_='an error' ;; |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 101 | FATAL) _th_alert_str_='a fatal' ;; |
kate.ward | 2f3cad9 | 2008-10-21 23:29:23 +0000 | [diff] [blame] | 102 | esac |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 103 | [ -z "${_th_alert_msg_}" ] && _th_alert_msg_='.*' |
| 104 | [ -n "${_th_msg_}" ] && _th_msg_="(${_th_msg_}) " |
| 105 | |
| 106 | grep -- "^flags:${_th_alert_type_} ${_th_alert_msg_}" "${stderrF}" \ |
kate.ward | 2f3cad9 | 2008-10-21 23:29:23 +0000 | [diff] [blame] | 107 | >/dev/null |
| 108 | assertTrue \ |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 109 | "FLAGS ${_th_msg_}failure did not generate ${_th_alert_str_} message" $? |
kate.ward | 20d0678 | 2008-10-21 19:57:19 +0000 | [diff] [blame] | 110 | |
kate.ward | 2f3cad9 | 2008-10-21 23:29:23 +0000 | [diff] [blame] | 111 | unset _th_alert_type_ _th_alert_msg_ _th_alert_str_ _th_msg_ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 112 | } |
kate.ward | 20d0678 | 2008-10-21 19:57:19 +0000 | [diff] [blame] | 113 | |
| 114 | assertWarnMsg() { _th_assertMsg 'WARN' "${1:-}" "${2:-}"; } |
| 115 | assertErrorMsg() { _th_assertMsg 'ERROR' "${1:-}" "${2:-}"; } |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 116 | assertFatalMsg() { _th_assertMsg 'FATAL' "${1:-}" "${2:-}"; } |