-
Notifications
You must be signed in to change notification settings - Fork 3
/
bash-spec.sh
289 lines (256 loc) · 6.36 KB
/
bash-spec.sh
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env bash
##==================================================================================
## BDD-style testing framework for bash scripts.
##
## expect variable [not] to_be value Compare scalar values for equality
## expect variable [not] to_match regex Regex match
## expect array [not] to_contain value Look for a value in an array
## expect_array arrayname [not] to_contain value Look for a value in an array (by ref)
## expect_var variablename [not] to_contain value Look for a value in a variable (by ref)
## expect filename [not] to_exist Verify file existence
## expect filename [not] to_have_mode modestring Verify file mode (permissions)
## expect [not] to_be_true condition Verify exit mode as boolean
## [[ some_expression ]]
## should_succeed
## [[ some_expression ]]
## should_fail
##
# Original Author: Dave Nicolette
# Version 1.0.0 29 Jul 2014
# Release
# Author: Dave Nicolette
# License: MIT
# (GPL3 licence added to bash-spec after the bash-spec-2 fork)
# Modified by REA Group 2014
# License: MIT
# Modified by keithy@consultant.com 03/2019
# License: MIT
#==================================================================================
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -uo pipefail
IFS=$'\n\t'
shopt -s nullglob # make sure globs are empty arrays if nothing is found
LOUD=${LOUD:-true}
result_file=$(mktemp)
_passed_=0
_failed_=0
exec 6<&1
exec > "$result_file"
function show_help {
exec 1>&6 6>&-
rm -f -- "$result_file"
grep "^##" $BASH_SOURCE | sed 's/^##//'
}
function output_results {
exec 1>&6 6>&-
local results="$(<$result_file)"
rm -f -- "$result_file"
local passes=$(printf '%s' "$results" | grep -F PASS | wc -l)
local fails=$(printf '%s' "$results" | grep -F '**** FAIL' | wc -l )
printf '%s\n--SUMMARY\n%d PASSED\n%d FAILED\n' "$results" "$passes" "$fails"
[[ ${fails:-1} -eq 0 ]]
exit $?
}
# https://stackoverflow.com/a/50938224
_array_test() {
# no argument passed
[[ $# -ne 1 ]] && echo 'Supply a variable name as an argument'>&2 && return 2
local var=$1
# use a variable to avoid having to escape spaces
local regex="^declare -[aA] ${var}(=|$)"
[[ $(declare -p "$var" 2> /dev/null) =~ $regex ]] && return 0
}
# array is passed by name
# also supports glob match
function _array_by_ref_contains_ {
_arr_=$_actual_
for i in "${!_arr_[@]}"; do
if [[ "${_arr_[i]}" == $_expected_ ]]; then
unset $_actual_[i]
return 0
fi
done
return 1
}
# also supports glob match
function _array_contains_ {
_count_=0
for elem in "${_actual_[@]}"; do
[[ "$elem" == $_expected_ ]] && ((_count_++))
done
if [ -n "$_times_" ]; then
[ $_count_ -eq $_times_ ] && return 0
[ $_count_ -gt 0 ] && _expected_="$_expected_ (x$_times_ found x$_count_)" && return 1
else
[ $_count_ -gt 0 ] && return 0 || return 1
fi
}
function _negation_check_ {
if [[ "$_negation_" == true ]]; then
if [[ "$_pass_" == true ]]; then
_pass_=false
else
_pass_=true
fi
fi
if [[ "$_pass_" == true ]]; then
(( _passed_+=1 ))
pass
else
(( _failed_+=1 ))
fail
fi
}
function it {
echo " $1"
[ -z ${2+x} ] || echo " $2"
}
function describe {
echo "$1"
[ -z ${2+x} ] || echo "$2"
}
function context {
echo "$1"
[ -z ${2+x} ] || echo "$2"
}
function pass {
echo " PASS"
}
function fail {
echo "**** FAIL - expected:$( if [[ "$_negation_" == true ]]; then echo ' NOT'; fi; ) '$_expected_' | actual: '${_actual_[@]}'"
}
function should_succeed {
_actual_=$?
_expected_=0
_negation_=false
_pass_=false
[[ $_actual_ == $_expected_ ]] && _pass_=true
_expected_="0(success)"
_actual_="$_actual_(failed)"
_negation_check_
}
function should_fail {
_actual_=$?
_expected_=0
_negation_=false
_pass_=true
[[ $_actual_ == $_expected_ ]] && _pass_=false
_expected_="NOT 0(fail)"
_actual_="0(succeeded)"
_negation_check_
}
function expect {
_expected_=
_negation_=false
_pass_=false
declare -a _actual_
until [[ "${1:0:3}" == to_ || "$1" == not || -z ${1+x} ]]; do
_actual_+=("$1")
shift
done
#echo $( IFS=$'|'; echo "$*" )
"$@"
}
function expect_var {
_expected_=
_negation_=false
declare -a _actual_
declare -n ref="$1"
until [[ "${1:0:3}" == to_ || "$1" == not || -z ${1+x} ]]; do
_actual_+=("${!1}")
shift
done
"$@"
}
function expect_array {
_expected_=
_negation_=false
declare -n _actual_=$1
until [[ "${1:0:3}" == to_ || "$1" == not || -z ${1+x} ]]; do
shift
done
"$@"
}
function not {
_negation_=true
"$@"
}
function to_be {
_expected_=$( IFS=$'\n'; echo "$*" )
_pass_=false
[[ "${_actual_[0]}" == "$_expected_" ]] && _pass_=true
_negation_check_
}
function to_be_true {
_expected_="$@ IS TRUE"
_pass=false
_actual_="$@ IS FALSE"
if "$@"; then
_pass_=true
_actual_="$@ IS TRUE"
fi
_negation_check_
}
function to_match {
_expected_="$1"
_pass_=false
[[ "${_actual_[0]}" =~ $_expected_ ]] && _pass_=true
_negation_check_
}
function to_contain {
_expected_="$1"
_times_="${3:-}"
_pass_=false
_array_contains_ "$_expected_" "$_actual_" "$_times_" && _pass_=true
_negation_check_
}
function to_exist {
_pass_=false
_expected_="$_actual_ EXISTS"
if [[ -e "${_actual_[0]}" ]]; then
_pass_=true
[[ "$_negation_" == true ]] && _expected_="$_actual_ EXISTS"
else
_actual_="File not found"
fi
_negation_check_
}
function to_have_mode {
_filename_="${_actual_[0]}"
_expected_="$1"
_pass_=false
if [[ -e "$_filename_" ]]; then
_fullname_="$_filename_"
else
_fullname_="$(which $_filename_)"
fi
if [[ -e "$_fullname_" ]]; then
_os_="$(uname -s)"
if [[ "$_os_" == Linux ]]; then
_actual_="$(stat -c %A $_fullname_)"
else
_actual_="$(stat $_fullname_ | cut -f 3 -d ' ')"
fi
[[ "$_actual_" =~ "$_expected_" ]] && _pass_=true
else
echo "File not found: $_fullname_"
fi
_negation_check_
}
# pattern - user supplied return variable name
function capture {
mapfile -t "$1" < "$2"
}
#kph asks why?
TEMP="$(getopt -o h --long help \
-n 'javawrap' -- $@)"
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$TEMP"
while true; do
case "$1" in
h | help ) show_help; exit 0 ;;
-- ) break ;;
* ) break ;;
esac
done
trap output_results EXIT