-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.sh
More file actions
208 lines (188 loc) · 4.92 KB
/
test.sh
File metadata and controls
208 lines (188 loc) · 4.92 KB
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
#
# testing
#
# bashfoo testing library
#
# use cases
# 1) set of *_test.sh files
# 2) tests.sh
# bashfoo_require testing
# bashfoo_autotest *_test.sh
#
# bashfoo_autotest will
# for each file it will
# if file is executable it till execute xxx_test
# if file is shell script it will source it
#
# after all it will by introspection execute all bash
# functions named test_xxx in subprocess as tests
#
# default test_timeout is 60 seconds unless redefined as follows
#
# BASHFOO_TESTING_TEST_TIMEOUT=<seconds>
# BASHFOO_TESTING_TEST_TIMEOUT=disabled
bashfoo_require assert
bashfoo_require log
bashfoo_require run
bashfoo_require path
bashfoo_require introspection
test_invoke()
{
bashfoo_test_last_command="$@"
( "$@" 2>&1 ) | tee stdout
}
skip_test()
{
echo "$PNAME: skipped ($*)"
exit 0
}
mark_test_result()
{
local test_name="$1"
local exit_code="$2"
if [ "$exit_code" != 0 ] ; then
echo "---- $test_name failed! (exit_code=$exit_code)"
something_failed=1
failed_tests="$failed_tests $test_name"
fi
}
execute_test_script()
{
local test_script_file="$test_src_dir/$1"
local test_name="${2-$(basename $1)}"
local test_exec_dir=".bashfoo_test/$test_name"
echo "TEST ${test_name} ... (script)"
rm -rf "$test_exec_dir"
mkdir -p "$test_exec_dir"
(
local dir="$(dirname $test_script_file)"
export test_src_dir="$(abspath $dir)"
cd "$test_exec_dir"
export test_name
export test_src_dir
quiet_if_success $test_script_file
)
local r=$?
mark_test_result "$test_name" "$r"
return $r
}
execute_test_function_int()
(
cd "$test_exec_dir"
export test_name
export test_src_dir
quiet_if_success $test_function
)
execute_test_function()
{
local test_function="$1"
local test_name="$(basename $1)"
local test_exec_dir=".bashfoo_test/$test_name"
echo "TEST $test_name ... (function)"
rm -rf "$test_exec_dir"
mkdir -p "$test_exec_dir"
if execute_test_function_int ; then
mark_test_result "$test_name" "0"
else
mark_test_result "$test_name" "$?"
fi
}
bashfoo_run_test_suite()
{
while read cmd name dir ; do
if [ "$cmd" = standalone ] ; then
test_src_dir="$(abspath $dir)"
execute_test_script "$name"
elif [ "$cmd" = function ] ; then
test_src_dir="$(abspath $dir)"
execute_test_function "$name"
elif [ "$cmd" = load ] ; then
echo "SUITE ${name}"
source "$(abspath $name)"
else
log_error "unknown test command '$cmd' (name=$name, dir=$dir)"
fi
done
}
#
# bashfoo_autotest
#
bashfoo_autotest_add_cmd()
{
if [ -z "$bashfoo_autotest_tmpfile" ] ; then
bashfoo_autotest_tmpfile="$(bashfoo.mktemp test_suite)"
rm -rf "$bashfoo_autotest_tmpfile"
fi
log_debug "_bashfoo_add_test: $@"
echo "$@" >> "$bashfoo_autotest_tmpfile"
}
_bashfoo_get_annotated_test_names()
{
local tab=`echo -en '\t'`
local bashfoo_test_re="@bashfoo.test[ $tab]+([a-zA-Z_0-9]*)"
while read line ; do
if [[ $line =~ $bashfoo_test_re ]] ; then
echo "${BASH_REMATCH[1]}"
fi
done
}
bashfoo_autotest_add_function_tests()
# adds all functions tagged @bashfoo.test to test suite
{
for f in "$@" ; do
local dir="$(abspath $(dirname $f))"
local loaded=0
for match in $(_bashfoo_get_annotated_test_names < $f) ; do
fun=$match
if [ "$loaded" = 0 ] ; then
bashfoo_autotest_add_cmd "load $f $dir"
loaded=1
fi
bashfoo_autotest_add_cmd "function $fun $dir"
done
done
}
bashfoo_autotest_add_script_test()
# adds all script files to test suite
{
for f in "$@" ; do
local dir="$(abspath $(dirname $f))"
local name="$(basename $f)"
if [ -f "$f" -a -x "$f" ] ; then
bashfoo_autotest_add_cmd "standalone $name $dir"
elif [ -d "$f" -a -x $f/test_driver.sh ] ; then
bashfoo_autotest_add_cmd "standalone $f/test_driver.sh $f"
fi
done
}
bashfoo_test_run_summary()
{
if [ -n "$something_failed" ] ; then
log_info "some tests failed ($failed_tests)"
else
log_info "all tests succeded"
fi
exit $something_failed
}
bashfoo_autotest_run()
{
if [ -z "$bashfoo_autotest_tmpfile" ] ; then
log_info "bashfoo_test_run: no tests to run!"
return 1
else
export bashfoo_testing_suspended=1
bashfoo_run_test_suite < "$bashfoo_autotest_tmpfile"
unset bashfoo_testing_suspended
bashfoo_test_run_summary
fi
}
autotest()
{
if [ -n "$bashfoo_testing_suspended" ] ; then
return
else
bashfoo_autotest_add_function_tests ${BASH_SOURCE[1]}
bashfoo_autotest_run
bashfoo_test_run_summary
fi
}