blob: f5d219130561e3f61ef5b06f67788f8bf13d9931 [file] [log] [blame]
kate.wardf51c6162008-06-17 16:38:35 +00001# vim:et:ft=sh:sts=2:sw=2
2#
kate.wardf51c6162008-06-17 16:38:35 +00003# shFlags unit test common functions
Kate Ward743fe642017-10-18 00:37:59 +02004#
Kate Wardc6b8f1c2018-01-16 23:00:35 +01005# Copyright 2008-2018 Kate Ward. All Rights Reserved.
Kate Ward743fe642017-10-18 00:37:59 +02006# 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 Ward3908d0f2018-01-18 01:05:45 +010016# Arrays are not available in all shells.
Kate Wardf7a99942018-01-18 01:10:37 +010017# shellcheck disable=SC2089
Kate Ward6ead6d72018-01-18 01:20:52 +010018# Exporting variables shouldn't impact their contents.
19# shellcheck disable=SC2090
Kate Ward743fe642017-10-18 00:37:59 +020020# Disagree with [ p ] && [ q ] vs [ p -a -q ] recommendation.
21# shellcheck disable=SC2166
kate.wardf51c6162008-06-17 16:38:35 +000022
Kate Ward1962cd82020-04-11 14:06:34 +020023# Exit immediately if a simple command exits with a non-zero status.
24set -e
25
26# Treat unset variables as an error when performing parameter expansion.
kate.ward31cb24f2008-11-12 20:13:28 +000027set -u
28
Kate Ward743fe642017-10-18 00:37:59 +020029# Set shwordsplit for zsh.
kate.ward1d0ecc42008-07-11 15:33:23 +000030[ -n "${ZSH_VERSION:-}" ] && setopt shwordsplit
31
Kate Ward743fe642017-10-18 00:37:59 +020032# Message functions.
33th_trace() { echo "test:TRACE $*" >&2; }
34th_debug() { echo "test:DEBUG $*" >&2; }
35th_info() { echo "test:INFO $*" >&2; }
36th_warn() { echo "test:WARN $*" >&2; }
37th_error() { echo "test:ERROR $*" >&2; }
38th_fatal() { echo "test:FATAL $*" >&2; exit 1; }
kate.ward2fb41e32008-07-10 18:42:46 +000039
Kate Ward743fe642017-10-18 00:37:59 +020040# Path to shFlags library. Can be overridden by setting SHFLAGS_INC.
Kate Ward5704f922018-01-18 00:20:48 +010041TH_SHFLAGS=${SHFLAGS_INC:-./shflags}; export TH_SHFLAGS
kate.wardf51c6162008-06-17 16:38:35 +000042
Kate Wardccfb1cf2017-10-06 22:45:27 +020043# Path to shUnit2 library. Can be overridden by setting SHUNIT_INC.
Kate Ward5704f922018-01-18 00:20:48 +010044TH_SHUNIT=${SHUNIT_INC:-lib/shunit2}; export TH_SHUNIT
kate.wardf51c6162008-06-17 16:38:35 +000045
Kate Ward3908d0f2018-01-18 01:05:45 +010046TH_BOOL_VALID='true t 0 false f 1'; export TH_BOOL_VALID
47TH_BOOL_INVALID='123 123.0 invalid'; export TH_BOOL_INVALID
Kate Ward5704f922018-01-18 00:20:48 +010048TH_FLOAT_VALID='-1234.0 -1.0 -.123 0.0 0. .123 1.0 1234.0'
Kate Ward3908d0f2018-01-18 01:05:45 +010049export TH_FLOAT_VALID
50TH_FLOAT_INVALID='true false 1.2.3 -1.2.3 ""'; export TH_FLOAT_INVALID
51TH_INT_VALID='-1234 -1 0 1 1234'; export TH_INT_VALID
52TH_INT_INVALID='true false -1.0 -.123 0.0 .123 1.0 ""'; export TH_INT_INVALID
kate.wardf51c6162008-06-17 16:38:35 +000053
54#
Kate Ward743fe642017-10-18 00:37:59 +020055# Test helper functions.
kate.wardf51c6162008-06-17 16:38:35 +000056#
57
Kate Ward743fe642017-10-18 00:37:59 +020058th_oneTimeSetUp() {
59 # Load shFlags.
60 # shellcheck disable=SC2034
kate.ward1b600c52008-11-12 21:26:05 +000061 [ -n "${ZSH_VERSION:-}" ] && FLAGS_PARENT=$0
Kate Ward743fe642017-10-18 00:37:59 +020062 . "${TH_SHFLAGS}"
kate.ward1b600c52008-11-12 21:26:05 +000063
Kate Ward743fe642017-10-18 00:37:59 +020064 # These files will be cleaned up automatically by shUnit2.
Kate Ward5704f922018-01-18 00:20:48 +010065 tmpDir=${SHUNIT_TMPDIR}; export tmpDir
Kate Ward743fe642017-10-18 00:37:59 +020066 stdoutF="${tmpDir}/stdout" && touch "${stdoutF}"
67 stderrF="${tmpDir}/stderr" && touch "${stderrF}"
68 returnF="${tmpDir}/return" && touch "${returnF}"
69 expectedF="${tmpDir}/expected" && touch "${expectedF}"
kate.ward1b600c52008-11-12 21:26:05 +000070}
71
Kate Ward743fe642017-10-18 00:37:59 +020072th_showOutput() {
Kate Ward34692b32020-04-11 20:21:26 +020073 if isSkipping; then
74 return
75 fi
76
77 _th_return="${1:-${returnF}}"
Kate Wardc6b8f1c2018-01-16 23:00:35 +010078 _th_stdout="${2:-${stdoutF}}"
79 _th_stderr="${3:-${stderrF}}"
kate.wardf51c6162008-06-17 16:38:35 +000080
Kate Ward34692b32020-04-11 20:21:26 +020081 if [ "${_th_return}" != "${FLAGS_TRUE}" ]; then
82 # shellcheck disable=SC2166
kate.ward271aece2008-11-13 01:12:35 +000083 if [ -n "${_th_stdout}" -a -s "${_th_stdout}" ]; then
84 echo '>>> STDOUT' >&2
kate.ward31cb24f2008-11-12 20:13:28 +000085 cat "${_th_stdout}" >&2
Kate Ward34692b32020-04-11 20:21:26 +020086 echo '<<< STDOUT' >&2
kate.ward31cb24f2008-11-12 20:13:28 +000087 fi
Kate Ward34692b32020-04-11 20:21:26 +020088 # shellcheck disable=SC2166
kate.ward271aece2008-11-13 01:12:35 +000089 if [ -n "${_th_stderr}" -a -s "${_th_stderr}" ]; then
90 echo '>>> STDERR' >&2
kate.ward31cb24f2008-11-12 20:13:28 +000091 cat "${_th_stderr}" >&2
Kate Ward34692b32020-04-11 20:21:26 +020092 echo '<<< STDERR' >&2
kate.ward271aece2008-11-13 01:12:35 +000093 fi
kate.wardf51c6162008-06-17 16:38:35 +000094 fi
95
96 unset _th_rtrn _th_stdout _th_stderr
97}
98
kate.warda2adce12013-01-15 00:00:39 +000099# 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.
102th_clearReturn() { cp /dev/null "${returnF}"; }
Kate Warde46d6322017-10-18 00:02:22 +0200103th_queryReturn() {
kate.warda2adce12013-01-15 00:00:39 +0000104 if [ -s "${returnF}" ]; then
Kate Warde46d6322017-10-18 00:02:22 +0200105 cat "${returnF}"
106 return $?
kate.warda2adce12013-01-15 00:00:39 +0000107 fi
Kate Ward743fe642017-10-18 00:37:59 +0200108 echo "${SHUNIT_ERROR}"
109 return "${SHUNIT_ERROR}"
kate.warda2adce12013-01-15 00:00:39 +0000110}
111
Kate Ward1c69f4a2020-04-12 00:25:04 +0200112assertWarnMsg() { _th_assertMsg 'WARN' "${1:-}" "${2:-}"; }
113assertErrorMsg() { _th_assertMsg 'ERROR' "${1:-}" "${2:-}"; }
114assertFatalMsg() { _th_assertMsg 'FATAL' "${1:-}" "${2:-}"; }
115
Kate Ward743fe642017-10-18 00:37:59 +0200116_th_assertMsg() {
kate.ward20d06782008-10-21 19:57:19 +0000117 _th_alert_type_=$1
118 _th_alert_msg_=$2
119 _th_msg_=$3
120
kate.ward2f3cad92008-10-21 23:29:23 +0000121 case ${_th_alert_type_} in
Kate Ward1c69f4a2020-04-12 00:25:04 +0200122 WARN) _th_alert_str_='a warning' ;;
kate.ward31cb24f2008-11-12 20:13:28 +0000123 ERROR) _th_alert_str_='an error' ;;
kate.ward271aece2008-11-13 01:12:35 +0000124 FATAL) _th_alert_str_='a fatal' ;;
kate.ward2f3cad92008-10-21 23:29:23 +0000125 esac
Kate Ward1c69f4a2020-04-12 00:25:04 +0200126 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.ward31cb24f2008-11-12 20:13:28 +0000132
Kate Ward1c69f4a2020-04-12 00:25:04 +0200133 (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.ward20d06782008-10-21 19:57:19 +0000135
kate.ward2f3cad92008-10-21 23:29:23 +0000136 unset _th_alert_type_ _th_alert_msg_ _th_alert_str_ _th_msg_
kate.wardf51c6162008-06-17 16:38:35 +0000137}