blob: 971367f6a1691b62f2bfa991736db87a3f168363 [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#
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.wardf51c6162008-06-17 16:38:35 +000018
Kate Ward743fe642017-10-18 00:37:59 +020019# Treat unset variables as an error.
kate.ward31cb24f2008-11-12 20:13:28 +000020set -u
21
Kate Ward743fe642017-10-18 00:37:59 +020022# Set shwordsplit for zsh.
kate.ward1d0ecc42008-07-11 15:33:23 +000023[ -n "${ZSH_VERSION:-}" ] && setopt shwordsplit
24
Kate Ward743fe642017-10-18 00:37:59 +020025# Message functions.
26th_trace() { echo "test:TRACE $*" >&2; }
27th_debug() { echo "test:DEBUG $*" >&2; }
28th_info() { echo "test:INFO $*" >&2; }
29th_warn() { echo "test:WARN $*" >&2; }
30th_error() { echo "test:ERROR $*" >&2; }
31th_fatal() { echo "test:FATAL $*" >&2; exit 1; }
kate.ward2fb41e32008-07-10 18:42:46 +000032
Kate Ward743fe642017-10-18 00:37:59 +020033# My name.
34TH_MY_NAME=`basename "$0"` || th_fatal 'Error executing basename.'
35export TH_MY_NAME
36
37# Path to shFlags library. Can be overridden by setting SHFLAGS_INC.
38export TH_SHFLAGS=${SHFLAGS_INC:-./shflags}
kate.wardf51c6162008-06-17 16:38:35 +000039
Kate Wardccfb1cf2017-10-06 22:45:27 +020040# Path to shUnit2 library. Can be overridden by setting SHUNIT_INC.
41export TH_SHUNIT=${SHUNIT_INC:-lib/shunit2}
kate.wardf51c6162008-06-17 16:38:35 +000042
Kate Ward743fe642017-10-18 00:37:59 +020043export TH_BOOL_VALID='true t 0 false f 1'
44export TH_BOOL_INVALID='123 123.0 invalid'
45export TH_FLOAT_VALID='-1234.0 -1.0 -.123 0.0 0. .123 1.0 1234.0'
46export TH_FLOAT_INVALID='true false 1.2.3 -1.2.3 ""'
47export TH_INT_VALID='-1234 -1 0 1 1234'
48export TH_INT_INVALID='true false -1.0 -.123 0.0 .123 1.0 ""'
kate.wardf51c6162008-06-17 16:38:35 +000049
50#
Kate Ward743fe642017-10-18 00:37:59 +020051# Test helper functions.
kate.wardf51c6162008-06-17 16:38:35 +000052#
53
Kate Ward743fe642017-10-18 00:37:59 +020054th_oneTimeSetUp() {
55 # Load shFlags.
56 # shellcheck disable=SC2034
kate.ward1b600c52008-11-12 21:26:05 +000057 [ -n "${ZSH_VERSION:-}" ] && FLAGS_PARENT=$0
Kate Ward743fe642017-10-18 00:37:59 +020058 . "${TH_SHFLAGS}"
kate.ward1b600c52008-11-12 21:26:05 +000059
Kate Ward743fe642017-10-18 00:37:59 +020060 # 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.ward1b600c52008-11-12 21:26:05 +000066}
67
Kate Ward743fe642017-10-18 00:37:59 +020068th_showOutput() {
kate.wardf51c6162008-06-17 16:38:35 +000069 _th_rtrn=$1
70 _th_stdout=$2
71 _th_stderr=$3
72
73 isSkipping
Kate Ward743fe642017-10-18 00:37:59 +020074 if [ $? -eq "${SHUNIT_FALSE}" -a "${_th_rtrn}" != "${FLAGS_TRUE}" ]; then
kate.ward271aece2008-11-13 01:12:35 +000075 if [ -n "${_th_stdout}" -a -s "${_th_stdout}" ]; then
76 echo '>>> STDOUT' >&2
kate.ward31cb24f2008-11-12 20:13:28 +000077 cat "${_th_stdout}" >&2
78 fi
kate.ward271aece2008-11-13 01:12:35 +000079 if [ -n "${_th_stderr}" -a -s "${_th_stderr}" ]; then
80 echo '>>> STDERR' >&2
kate.ward31cb24f2008-11-12 20:13:28 +000081 cat "${_th_stderr}" >&2
82 fi
kate.ward271aece2008-11-13 01:12:35 +000083 if [ -n "${_th_stdout}" -o -n "${_th_stderr}" ]; then
kate.wardda1f8562013-01-05 13:56:11 +000084 echo '<<< end output' >&2
kate.ward271aece2008-11-13 01:12:35 +000085 fi
kate.wardf51c6162008-06-17 16:38:35 +000086 fi
87
88 unset _th_rtrn _th_stdout _th_stderr
89}
90
kate.warda2adce12013-01-15 00:00:39 +000091# 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.
94th_clearReturn() { cp /dev/null "${returnF}"; }
Kate Warde46d6322017-10-18 00:02:22 +020095th_queryReturn() {
kate.warda2adce12013-01-15 00:00:39 +000096 if [ -s "${returnF}" ]; then
Kate Warde46d6322017-10-18 00:02:22 +020097 cat "${returnF}"
98 return $?
kate.warda2adce12013-01-15 00:00:39 +000099 fi
Kate Ward743fe642017-10-18 00:37:59 +0200100 echo "${SHUNIT_ERROR}"
101 return "${SHUNIT_ERROR}"
kate.warda2adce12013-01-15 00:00:39 +0000102}
103
Kate Ward743fe642017-10-18 00:37:59 +0200104_th_assertMsg() {
kate.ward20d06782008-10-21 19:57:19 +0000105 _th_alert_type_=$1
106 _th_alert_msg_=$2
107 _th_msg_=$3
108
kate.ward2f3cad92008-10-21 23:29:23 +0000109 case ${_th_alert_type_} in
kate.ward31cb24f2008-11-12 20:13:28 +0000110 WARN) _th_alert_str_='a warning' ;;
111 ERROR) _th_alert_str_='an error' ;;
kate.ward271aece2008-11-13 01:12:35 +0000112 FATAL) _th_alert_str_='a fatal' ;;
kate.ward2f3cad92008-10-21 23:29:23 +0000113 esac
kate.ward31cb24f2008-11-12 20:13:28 +0000114 [ -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.ward2f3cad92008-10-21 23:29:23 +0000118 >/dev/null
119 assertTrue \
kate.ward31cb24f2008-11-12 20:13:28 +0000120 "FLAGS ${_th_msg_}failure did not generate ${_th_alert_str_} message" $?
kate.ward20d06782008-10-21 19:57:19 +0000121
kate.ward2f3cad92008-10-21 23:29:23 +0000122 unset _th_alert_type_ _th_alert_msg_ _th_alert_str_ _th_msg_
kate.wardf51c6162008-06-17 16:38:35 +0000123}
kate.ward20d06782008-10-21 19:57:19 +0000124
125assertWarnMsg() { _th_assertMsg 'WARN' "${1:-}" "${2:-}"; }
126assertErrorMsg() { _th_assertMsg 'ERROR' "${1:-}" "${2:-}"; }
kate.ward271aece2008-11-13 01:12:35 +0000127assertFatalMsg() { _th_assertMsg 'FATAL' "${1:-}" "${2:-}"; }