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 | # |
Kate Ward | c6b8f1c | 2018-01-16 23:00:35 +0100 | [diff] [blame] | 5 | # Copyright 2008-2018 Kate Ward. All Rights Reserved. |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 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 |
Kate Ward | 3908d0f | 2018-01-18 01:05:45 +0100 | [diff] [blame] | 16 | # Arrays are not available in all shells. |
Kate Ward | f7a9994 | 2018-01-18 01:10:37 +0100 | [diff] [blame] | 17 | # shellcheck disable=SC2089 |
Kate Ward | 6ead6d7 | 2018-01-18 01:20:52 +0100 | [diff] [blame] | 18 | # Exporting variables shouldn't impact their contents. |
| 19 | # shellcheck disable=SC2090 |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 20 | # Disagree with [ p ] && [ q ] vs [ p -a -q ] recommendation. |
| 21 | # shellcheck disable=SC2166 |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 22 | |
Kate Ward | 1962cd8 | 2020-04-11 14:06:34 +0200 | [diff] [blame] | 23 | # Exit immediately if a simple command exits with a non-zero status. |
| 24 | set -e |
| 25 | |
| 26 | # Treat unset variables as an error when performing parameter expansion. |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 27 | set -u |
| 28 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 29 | # Set shwordsplit for zsh. |
kate.ward | 1d0ecc4 | 2008-07-11 15:33:23 +0000 | [diff] [blame] | 30 | [ -n "${ZSH_VERSION:-}" ] && setopt shwordsplit |
| 31 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 32 | # Message functions. |
| 33 | th_trace() { echo "test:TRACE $*" >&2; } |
| 34 | th_debug() { echo "test:DEBUG $*" >&2; } |
| 35 | th_info() { echo "test:INFO $*" >&2; } |
| 36 | th_warn() { echo "test:WARN $*" >&2; } |
| 37 | th_error() { echo "test:ERROR $*" >&2; } |
| 38 | th_fatal() { echo "test:FATAL $*" >&2; exit 1; } |
kate.ward | 2fb41e3 | 2008-07-10 18:42:46 +0000 | [diff] [blame] | 39 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 40 | # Path to shFlags library. Can be overridden by setting SHFLAGS_INC. |
Kate Ward | 5704f92 | 2018-01-18 00:20:48 +0100 | [diff] [blame] | 41 | TH_SHFLAGS=${SHFLAGS_INC:-./shflags}; export TH_SHFLAGS |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 42 | |
Kate Ward | ccfb1cf | 2017-10-06 22:45:27 +0200 | [diff] [blame] | 43 | # Path to shUnit2 library. Can be overridden by setting SHUNIT_INC. |
Kate Ward | 5704f92 | 2018-01-18 00:20:48 +0100 | [diff] [blame] | 44 | TH_SHUNIT=${SHUNIT_INC:-lib/shunit2}; export TH_SHUNIT |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 45 | |
Kate Ward | 3908d0f | 2018-01-18 01:05:45 +0100 | [diff] [blame] | 46 | TH_BOOL_VALID='true t 0 false f 1'; export TH_BOOL_VALID |
| 47 | TH_BOOL_INVALID='123 123.0 invalid'; export TH_BOOL_INVALID |
Kate Ward | 5704f92 | 2018-01-18 00:20:48 +0100 | [diff] [blame] | 48 | TH_FLOAT_VALID='-1234.0 -1.0 -.123 0.0 0. .123 1.0 1234.0' |
Kate Ward | 3908d0f | 2018-01-18 01:05:45 +0100 | [diff] [blame] | 49 | export TH_FLOAT_VALID |
| 50 | TH_FLOAT_INVALID='true false 1.2.3 -1.2.3 ""'; export TH_FLOAT_INVALID |
| 51 | TH_INT_VALID='-1234 -1 0 1 1234'; export TH_INT_VALID |
| 52 | TH_INT_INVALID='true false -1.0 -.123 0.0 .123 1.0 ""'; export TH_INT_INVALID |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 53 | |
| 54 | # |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 55 | # Test helper functions. |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 56 | # |
| 57 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 58 | th_oneTimeSetUp() { |
| 59 | # Load shFlags. |
| 60 | # shellcheck disable=SC2034 |
kate.ward | 1b600c5 | 2008-11-12 21:26:05 +0000 | [diff] [blame] | 61 | [ -n "${ZSH_VERSION:-}" ] && FLAGS_PARENT=$0 |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 62 | . "${TH_SHFLAGS}" |
kate.ward | 1b600c5 | 2008-11-12 21:26:05 +0000 | [diff] [blame] | 63 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 64 | # These files will be cleaned up automatically by shUnit2. |
Kate Ward | 5704f92 | 2018-01-18 00:20:48 +0100 | [diff] [blame] | 65 | tmpDir=${SHUNIT_TMPDIR}; export tmpDir |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 66 | stdoutF="${tmpDir}/stdout" && touch "${stdoutF}" |
| 67 | stderrF="${tmpDir}/stderr" && touch "${stderrF}" |
| 68 | returnF="${tmpDir}/return" && touch "${returnF}" |
| 69 | expectedF="${tmpDir}/expected" && touch "${expectedF}" |
kate.ward | 1b600c5 | 2008-11-12 21:26:05 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 72 | th_showOutput() { |
Kate Ward | 34692b3 | 2020-04-11 20:21:26 +0200 | [diff] [blame] | 73 | if isSkipping; then |
| 74 | return |
| 75 | fi |
| 76 | |
| 77 | _th_return="${1:-${returnF}}" |
Kate Ward | c6b8f1c | 2018-01-16 23:00:35 +0100 | [diff] [blame] | 78 | _th_stdout="${2:-${stdoutF}}" |
| 79 | _th_stderr="${3:-${stderrF}}" |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 80 | |
Kate Ward | 34692b3 | 2020-04-11 20:21:26 +0200 | [diff] [blame] | 81 | if [ "${_th_return}" != "${FLAGS_TRUE}" ]; then |
| 82 | # shellcheck disable=SC2166 |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 83 | if [ -n "${_th_stdout}" -a -s "${_th_stdout}" ]; then |
| 84 | echo '>>> STDOUT' >&2 |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 85 | cat "${_th_stdout}" >&2 |
Kate Ward | 34692b3 | 2020-04-11 20:21:26 +0200 | [diff] [blame] | 86 | echo '<<< STDOUT' >&2 |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 87 | fi |
Kate Ward | 34692b3 | 2020-04-11 20:21:26 +0200 | [diff] [blame] | 88 | # shellcheck disable=SC2166 |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 89 | if [ -n "${_th_stderr}" -a -s "${_th_stderr}" ]; then |
| 90 | echo '>>> STDERR' >&2 |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 91 | cat "${_th_stderr}" >&2 |
Kate Ward | 34692b3 | 2020-04-11 20:21:26 +0200 | [diff] [blame] | 92 | echo '<<< STDERR' >&2 |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 93 | fi |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 94 | fi |
| 95 | |
| 96 | unset _th_rtrn _th_stdout _th_stderr |
| 97 | } |
| 98 | |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 99 | # Some shells, zsh on Solaris in particular, return immediately from a sub-shell |
| 100 | # when a non-zero return value is encountered. To properly catch these values, |
| 101 | # they are either written to disk, or recognized as an error the file is empty. |
| 102 | th_clearReturn() { cp /dev/null "${returnF}"; } |
Kate Ward | e46d632 | 2017-10-18 00:02:22 +0200 | [diff] [blame] | 103 | th_queryReturn() { |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 104 | if [ -s "${returnF}" ]; then |
Kate Ward | e46d632 | 2017-10-18 00:02:22 +0200 | [diff] [blame] | 105 | cat "${returnF}" |
| 106 | return $? |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 107 | fi |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 108 | echo "${SHUNIT_ERROR}" |
| 109 | return "${SHUNIT_ERROR}" |
kate.ward | a2adce1 | 2013-01-15 00:00:39 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Kate Ward | 1c69f4a | 2020-04-12 00:25:04 +0200 | [diff] [blame] | 112 | assertWarnMsg() { _th_assertMsg 'WARN' "${1:-}" "${2:-}"; } |
| 113 | assertErrorMsg() { _th_assertMsg 'ERROR' "${1:-}" "${2:-}"; } |
| 114 | assertFatalMsg() { _th_assertMsg 'FATAL' "${1:-}" "${2:-}"; } |
| 115 | |
Kate Ward | 743fe64 | 2017-10-18 00:37:59 +0200 | [diff] [blame] | 116 | _th_assertMsg() { |
kate.ward | 20d0678 | 2008-10-21 19:57:19 +0000 | [diff] [blame] | 117 | _th_alert_type_=$1 |
| 118 | _th_alert_msg_=$2 |
| 119 | _th_msg_=$3 |
| 120 | |
kate.ward | 2f3cad9 | 2008-10-21 23:29:23 +0000 | [diff] [blame] | 121 | case ${_th_alert_type_} in |
Kate Ward | 1c69f4a | 2020-04-12 00:25:04 +0200 | [diff] [blame] | 122 | WARN) _th_alert_str_='a warning' ;; |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 123 | ERROR) _th_alert_str_='an error' ;; |
kate.ward | 271aece | 2008-11-13 01:12:35 +0000 | [diff] [blame] | 124 | FATAL) _th_alert_str_='a fatal' ;; |
kate.ward | 2f3cad9 | 2008-10-21 23:29:23 +0000 | [diff] [blame] | 125 | esac |
Kate Ward | 1c69f4a | 2020-04-12 00:25:04 +0200 | [diff] [blame] | 126 | if [ -z "${_th_alert_msg_}" ]; then |
| 127 | _th_alert_msg_='.*' |
| 128 | fi |
| 129 | if [ -n "${_th_msg_}" ]; then |
| 130 | _th_msg_="(${_th_msg_}) " |
| 131 | fi |
kate.ward | 31cb24f | 2008-11-12 20:13:28 +0000 | [diff] [blame] | 132 | |
Kate Ward | 1c69f4a | 2020-04-12 00:25:04 +0200 | [diff] [blame] | 133 | (grep -- "^flags:${_th_alert_type_} ${_th_alert_msg_}" "${stderrF}" >/dev/null) |
| 134 | assertEquals "FLAGS ${_th_msg_}failure did not generate ${_th_alert_str_} message" "${FLAGS_TRUE}" $? |
kate.ward | 20d0678 | 2008-10-21 19:57:19 +0000 | [diff] [blame] | 135 | |
kate.ward | 2f3cad9 | 2008-10-21 23:29:23 +0000 | [diff] [blame] | 136 | unset _th_alert_type_ _th_alert_msg_ _th_alert_str_ _th_msg_ |
kate.ward | f51c616 | 2008-06-17 16:38:35 +0000 | [diff] [blame] | 137 | } |