1# vim:et:ft=sh:sts=2:sw=2 2# 3# shFlags unit test common functions 4# 5# Copyright 2008-2018 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# Arrays are not available in all shells. 17# shellcheck disable=SC2089 18# Exporting variables shouldn't impact their contents. 19# shellcheck disable=SC2090 20# Disagree with [ p ] && [ q ] vs [ p -a -q ] recommendation. 21# shellcheck disable=SC2166 22 23# 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. 27set -u 28 29# Set shwordsplit for zsh. 30[ -n "${ZSH_VERSION:-}" ] && setopt shwordsplit 31 32# 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; } 39 40# Path to shFlags library. Can be overridden by setting SHFLAGS_INC. 41TH_SHFLAGS=${SHFLAGS_INC:-./shflags}; export TH_SHFLAGS 42 43# Path to shUnit2 library. Can be overridden by setting SHUNIT_INC. 44TH_SHUNIT=${SHUNIT_INC:-lib/shunit2}; export TH_SHUNIT 45 46TH_BOOL_VALID='true t 0 false f 1'; export TH_BOOL_VALID 47TH_BOOL_INVALID='123 123.0 invalid'; export TH_BOOL_INVALID 48TH_FLOAT_VALID='-1234.0 -1.0 -.123 0.0 0. .123 1.0 1234.0' 49export 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 53 54# 55# Test helper functions. 56# 57 58th_oneTimeSetUp() { 59 # Load shFlags. 60 # shellcheck disable=SC2034 61 [ -n "${ZSH_VERSION:-}" ] && FLAGS_PARENT=$0 62 . "${TH_SHFLAGS}" 63 64 # These files will be cleaned up automatically by shUnit2. 65 tmpDir=${SHUNIT_TMPDIR}; export tmpDir 66 stdoutF="${tmpDir}/stdout" && touch "${stdoutF}" 67 stderrF="${tmpDir}/stderr" && touch "${stderrF}" 68 returnF="${tmpDir}/return" && touch "${returnF}" 69 expectedF="${tmpDir}/expected" && touch "${expectedF}" 70} 71 72th_showOutput() { 73 if isSkipping; then 74 return 75 fi 76 77 _th_return="${1:-${returnF}}" 78 _th_stdout="${2:-${stdoutF}}" 79 _th_stderr="${3:-${stderrF}}" 80 81 if [ "${_th_return}" != "${FLAGS_TRUE}" ]; then 82 # shellcheck disable=SC2166 83 if [ -n "${_th_stdout}" -a -s "${_th_stdout}" ]; then 84 echo '>>> STDOUT' >&2 85 cat "${_th_stdout}" >&2 86 echo '<<< STDOUT' >&2 87 fi 88 # shellcheck disable=SC2166 89 if [ -n "${_th_stderr}" -a -s "${_th_stderr}" ]; then 90 echo '>>> STDERR' >&2 91 cat "${_th_stderr}" >&2 92 echo '<<< STDERR' >&2 93 fi 94 fi 95 96 unset _th_rtrn _th_stdout _th_stderr 97} 98 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. 102th_clearReturn() { cp /dev/null "${returnF}"; } 103th_queryReturn() { 104 if [ -s "${returnF}" ]; then 105 cat "${returnF}" 106 return $? 107 fi 108 echo "${SHUNIT_ERROR}" 109 return "${SHUNIT_ERROR}" 110} 111 112assertWarnMsg() { _th_assertMsg 'WARN' "${1:-}" "${2:-}"; } 113assertErrorMsg() { _th_assertMsg 'ERROR' "${1:-}" "${2:-}"; } 114assertFatalMsg() { _th_assertMsg 'FATAL' "${1:-}" "${2:-}"; } 115 116_th_assertMsg() { 117 _th_alert_type_=$1 118 _th_alert_msg_=$2 119 _th_msg_=$3 120 121 case ${_th_alert_type_} in 122 WARN) _th_alert_str_='a warning' ;; 123 ERROR) _th_alert_str_='an error' ;; 124 FATAL) _th_alert_str_='a fatal' ;; 125 esac 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 132 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}" $? 135 136 unset _th_alert_type_ _th_alert_msg_ _th_alert_str_ _th_msg_ 137} 138