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