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 |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 4 | # |
| 5 | # Copyright 2008-2017 Kate Ward. All Rights Reserved. |
| 6 | # Released under the Apache 2.0 license. |
| 7 | # |
| 8 | # Author: kate.ward@forestent.com (Kate Ward) |
| 9 | # https://github.com/kward/shflags |
| 10 | # |
| 11 | ### ShellCheck (http://www.shellcheck.net/) |
| 12 | # Disable source following. |
| 13 | # shellcheck disable=SC1090,SC1091 |
| 14 | # $() are not fully portable (POSIX != portable). |
| 15 | # shellcheck disable=SC2006 |
| 16 | # Disagree with [ p ] && [ q ] vs [ p -a -q ] recommendation. |
| 17 | # shellcheck disable=SC2166 |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 18 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 19 | # Treat unset variables as an error. |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 20 | set -u |
| 21 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 22 | # Set shwordsplit for zsh. |
kate.ward | 1d0ecc4 | 2008-07-11 15:33:23 +0000 | [diff] [blame] | 23 | [ -n "${ZSH_VERSION:-}" ] && setopt shwordsplit |
| 24 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 25 | # Message functions. |
| 26 | th_trace() { echo "test:TRACE $*" >&2; } |
| 27 | th_debug() { echo "test:DEBUG $*" >&2; } |
| 28 | th_info() { echo "test:INFO $*" >&2; } |
| 29 | th_warn() { echo "test:WARN $*" >&2; } |
| 30 | th_error() { echo "test:ERROR $*" >&2; } |
| 31 | th_fatal() { echo "test:FATAL $*" >&2; exit 1; } |
kate.ward | 2fb41e3 | 2008-07-10 18:42:46 +0000 | [diff] [blame] | 32 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 33 | # My name. |
| 34 | TH_MY_NAME=`basename "$0"` || th_fatal 'Error executing basename.' |
| 35 | export TH_MY_NAME |
| 36 | |
| 37 | # Path to shFlags library. Can be overridden by setting SHFLAGS_INC. |
| 38 | export TH_SHFLAGS=${SHFLAGS_INC:-./shflags} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 39 | |
Kate Ward | ccfb1cf | 2017-10-06 22:45:27 +0200 | [diff] [blame] | 40 | # Path to shUnit2 library. Can be overridden by setting SHUNIT_INC. |
| 41 | export TH_SHUNIT=${SHUNIT_INC:-lib/shunit2} |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 42 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 43 | export TH_BOOL_VALID='true t 0 false f 1' |
| 44 | export TH_BOOL_INVALID='123 123.0 invalid' |
| 45 | export TH_FLOAT_VALID='-1234.0 -1.0 -.123 0.0 0. .123 1.0 1234.0' |
| 46 | export TH_FLOAT_INVALID='true false 1.2.3 -1.2.3 ""' |
| 47 | export TH_INT_VALID='-1234 -1 0 1 1234' |
| 48 | export 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] | 49 | |
| 50 | # |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 51 | # Test helper functions. |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 52 | # |
| 53 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 54 | th_oneTimeSetUp() { |
| 55 | # Load shFlags. |
| 56 | # shellcheck disable=SC2034 |
kate.ward | 1b600c5 | 2008-11-12 21:26:05 +0000 | [diff] [blame] | 57 | [ -n "${ZSH_VERSION:-}" ] && FLAGS_PARENT=$0 |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 58 | . "${TH_SHFLAGS}" |
kate.ward | 1b600c5 | 2008-11-12 21:26:05 +0000 | [diff] [blame] | 59 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 60 | # These files will be cleaned up automatically by shUnit2. |
| 61 | export tmpDir=${SHUNIT_TMPDIR} |
| 62 | stdoutF="${tmpDir}/stdout" && touch "${stdoutF}" |
| 63 | stderrF="${tmpDir}/stderr" && touch "${stderrF}" |
| 64 | returnF="${tmpDir}/return" && touch "${returnF}" |
| 65 | expectedF="${tmpDir}/expected" && touch "${expectedF}" |
kate.ward | 1b600c5 | 2008-11-12 21:26:05 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 68 | th_showOutput() { |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 69 | _th_rtrn=$1 |
| 70 | _th_stdout=$2 |
| 71 | _th_stderr=$3 |
| 72 | |
| 73 | isSkipping |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 74 | if [ $? -eq "${SHUNIT_FALSE}" -a "${_th_rtrn}" != "${FLAGS_TRUE}" ]; then |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 75 | if [ -n "${_th_stdout}" -a -s "${_th_stdout}" ]; then |
| 76 | echo '>>> STDOUT' >&2 |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 77 | cat "${_th_stdout}" >&2 |
| 78 | fi |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 79 | if [ -n "${_th_stderr}" -a -s "${_th_stderr}" ]; then |
| 80 | echo '>>> STDERR' >&2 |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 81 | cat "${_th_stderr}" >&2 |
| 82 | fi |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 83 | if [ -n "${_th_stdout}" -o -n "${_th_stderr}" ]; then |
kate.ward | da1f856 | 2013-01-05 13:56:11 +0000 | [diff] [blame] | 84 | echo '<<< end output' >&2 |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 85 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 86 | fi |
| 87 | |
| 88 | unset _th_rtrn _th_stdout _th_stderr |
| 89 | } |
| 90 | |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 91 | # Some shells, zsh on Solaris in particular, return immediately from a sub-shell |
| 92 | # when a non-zero return value is encountered. To properly catch these values, |
| 93 | # they are either written to disk, or recognized as an error the file is empty. |
| 94 | th_clearReturn() { cp /dev/null "${returnF}"; } |
Kate Ward | e46d632 | 2017-10-18 00:02:22 +0200 | [diff] [blame] | 95 | th_queryReturn() { |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 96 | if [ -s "${returnF}" ]; then |
Kate Ward | e46d632 | 2017-10-18 00:02:22 +0200 | [diff] [blame] | 97 | cat "${returnF}" |
| 98 | return $? |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 99 | fi |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 100 | echo "${SHUNIT_ERROR}" |
| 101 | return "${SHUNIT_ERROR}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 104 | _th_assertMsg() { |
kate.ward | 20d0678 | 2008-10-21 19:57:19 +0000 | [diff] [blame] | 105 | _th_alert_type_=$1 |
| 106 | _th_alert_msg_=$2 |
| 107 | _th_msg_=$3 |
| 108 | |
kate.ward | 2f3cad9 | 2008-10-21 23:29:23 +0000 | [diff] [blame] | 109 | case ${_th_alert_type_} in |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 110 | WARN) _th_alert_str_='a warning' ;; |
| 111 | ERROR) _th_alert_str_='an error' ;; |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 112 | FATAL) _th_alert_str_='a fatal' ;; |
kate.ward | 2f3cad9 | 2008-10-21 23:29:23 +0000 | [diff] [blame] | 113 | esac |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 114 | [ -z "${_th_alert_msg_}" ] && _th_alert_msg_='.*' |
| 115 | [ -n "${_th_msg_}" ] && _th_msg_="(${_th_msg_}) " |
| 116 | |
| 117 | grep -- "^flags:${_th_alert_type_} ${_th_alert_msg_}" "${stderrF}" \ |
kate.ward | 2f3cad9 | 2008-10-21 23:29:23 +0000 | [diff] [blame] | 118 | >/dev/null |
| 119 | assertTrue \ |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 120 | "FLAGS ${_th_msg_}failure did not generate ${_th_alert_str_} message" $? |
kate.ward | 20d0678 | 2008-10-21 19:57:19 +0000 | [diff] [blame] | 121 | |
kate.ward | 2f3cad9 | 2008-10-21 23:29:23 +0000 | [diff] [blame] | 122 | unset _th_alert_type_ _th_alert_msg_ _th_alert_str_ _th_msg_ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 123 | } |
kate.ward | 20d0678 | 2008-10-21 19:57:19 +0000 | [diff] [blame] | 124 | |
| 125 | assertWarnMsg() { _th_assertMsg 'WARN' "${1:-}" "${2:-}"; } |
| 126 | assertErrorMsg() { _th_assertMsg 'ERROR' "${1:-}" "${2:-}"; } |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 127 | assertFatalMsg() { _th_assertMsg 'FATAL' "${1:-}" "${2:-}"; } |