-
Notifications
You must be signed in to change notification settings - Fork 23
/
log4sh_test_helpers
154 lines (126 loc) · 3.43 KB
/
log4sh_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# vim:et:ft=sh:sts=2:sw=2
#
# Copyright 2008 Kate Ward. All Rights Reserved.
# Released under the LGPL (GNU Lesser General Public License)
# Author: [email protected] (Kate Ward)
#
# log4sh unit test common functions.
# treat unset variables as an error
set -u
# Set shwordsplit for zsh.
[ -n "${ZSH_VERSION:-}" ] && setopt shwordsplit
# name of script
TH_ARGV0=`basename "$0"`
# path to log4sh library. can be overridden by setting LOG4SH_INC.
TH_LOG4SH=${LOG4SH_INC:-./log4sh}
# path to shUnit2 library. can be overridden by setting SHUNIT_INC.
TH_SHUNIT=${SHUNIT_INC:-lib/shunit2}
# path to testdata
TH_TESTDATA_DIR='testdata'
# temporary directory (overridden in th_oneTimeSetUp())
TH_TMPDIR=''
#
# constants
#
# configure debugging. set the DEBUG environment variable to any
# non-empty value to enable debug output, or TRACE to enable trace
# output.
TRACE=${TRACE:+'th_trace '}
[ -n "${TRACE}" ] && DEBUG=1
[ -z "${TRACE}" ] && TRACE=':'
DEBUG=${DEBUG:+'th_debug '}
[ -z "${DEBUG}" ] && DEBUG=':'
#
# variables
#
th_RANDOM=0
#
# test helper functions
#
# 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; }
# generate a random number
th_generateRandom()
{
th_random_=${th_RANDOM}
while [ "${th_random_}" = "${th_RANDOM}" ]; do
if [ -n "${RANDOM:-}" ]; then
# $RANDOM works
th_random_=${RANDOM}${RANDOM}${RANDOM}$$
elif [ -r '/dev/urandom' ]; then
th_random_=`od -vAn -N4 -tu4 </dev/urandom |sed 's/^[^0-9]*//'`
else
th_date_=`date '+%H%M%S'`
th_random_=`expr ${th_date_} \* $$`
unset th_date_
fi
[ "${th_random_}" = "${th_RANDOM}" ] && sleep 1
done
th_RANDOM=${th_random_}
unset th_random_
}
# this section returns the data section from the specified section of a file. a
# datasection is defined by a [header], one or more lines of data, and then a
# blank line.
th_getDataSect()
{
th_sgrep "\\[$1\\]" "$2" |sed '1d'
}
th_oneTimeSetUp()
{
# These files will be cleaned up automatically by shUnit2.
stdoutF="${SHUNIT_TMPDIR}/stdout"
stderrF="${SHUNIT_TMPDIR}/stderr"
expectedF="${SHUNIT_TMPDIR}/expected"
}
# this function greps a section from a file. a section is defined as a group of
# lines preceeded and followed by blank lines.
th_sgrep()
{
th_pattern_=$1
shift
sed -e '/./{H;$!d;}' -e "x;/${th_pattern_}/"'!d;' $@ |sed '1d'
unset th_pattern_
}
# Assert the success of an operation.
#
# If an operation is not successful (i.e. it returns a non-zero return code)
# dump the output of the stderrF to the screen.
#
# Args:
# message: string: message to output [optional]
# result: integer: operation result
assertSuccess()
{
if [ $# -eq 2 ]; then
th_message_=$1
shift
else
th_message_=''
fi
th_result_=$1
assertEquals "${th_message_}" ${SHUNIT_TRUE} ${th_result_}
[ ${th_result_} -eq ${SHUNIT_TRUE} ] || cat "${stderrF}"
unset th_message_ th_result_
}
assertError()
{
if [ $# -eq 2 ]; then
th_message_="$1: "
shift
else
th_message_=''
fi
th_error_=$1
th_file_=${stderrF}
grep "^log4sh:ERROR.*${th_error_}" "${th_file_}" >/dev/null
th_result_=$?
assertTrue "${th_message_}missing '${th_error_}' error" ${th_result_}
[ ${th_result_} -eq 0 ] || cat "${th_file_}"
unset th_file_ th_error_ th_message_ th_result_
}