-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.sh
executable file
·270 lines (242 loc) · 7.42 KB
/
tests.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
#!/bin/sh
# wrapper to peform unit tests
#
ME_ABOUT='wrapper to peform unit tests'
ME_USAGE='[<...OPTIONS>] [<TEST-SUITE>] [[--]<...passthru args>]'
ME_COPYRIGHT='Copyright (c) 2016-2018, Doug Bird. All Rights Reserved.'
ME_NAME='tests.sh'
ME_DIR="/$0"; ME_DIR=${ME_DIR%/*}; ME_DIR=${ME_DIR:-.}; ME_DIR=${ME_DIR#/}/; ME_DIR=$(cd "$ME_DIR"; pwd)
ME_SOURCE="$ME_DIR/$0"
#
# paths
#
HTML_ROOT=$ME_DIR/web
PHPUNIT_BIN=$ME_DIR/vendor/bin/phpunit
PHPUNIT_TESTS_ROOT=$ME_DIR/tests
#
# exit codes
#
ME_ERROR_USAGE=2
ME_ERROR_ONE_OR_MORE_TESTS_FAILED=4
ME_ERROR_MISSING_DEP=3
CMD_STATUS_DONTUSE="255 $ME_ERROR_USAGE $ME_ERROR_ONE_OR_MORE_TESTS_FAILED $ME_ERROR_MISSING_DEP"
print_hint() {
echo " Hint, try: $ME_NAME --usage"
}
PRINT_COVERAGE=0
HTML_COVERAGE_REPORT=0
SKIP_COVERAGE_REPORT=0
OPTION_STATUS=0
while getopts :?qhua-: arg; do { case $arg in
h|u|a) HELP_MODE=1;;
-) LONG_OPTARG="${OPTARG#*=}"; case $OPTARG in
help|usage|about) HELP_MODE=1;;
skip-coverage) SKIP_COVERAGE_REPORT=1;;
html-coverage) HTML_COVERAGE_REPORT=1;;
print-coverage) PRINT_COVERAGE=1;;
show-coverage) PRINT_COVERAGE=1;;
coverage) PRINT_COVERAGE=1;;
*) >&2 echo "$ME_NAME: unrecognized long option --$OPTARG"; OPTION_STATUS=$ME_ERROR_USAGE;;
esac ;;
*) >&2 echo "$ME_NAME: unrecognized option -$OPTARG"; OPTION_STATUS=$ME_ERROR_USAGE;;
esac } done
shift $((OPTIND-1)) # remove parsed options and args from $@ list
[ "$OPTION_STATUS" != "0" ] && { >&2 echo "$ME_NAME: (FATAL) one or more invalid options"; >&2 print_hint; exit $OPTION_STATUS; }
if [ "$HELP_MODE" ]; then
echo "$ME_NAME"
echo "$ME_ABOUT"
echo "$ME_COPYRIGHT"
echo ""
echo "Usage:"
echo " $ME_NAME $ME_USAGE"
echo ""
echo "Arguments:"
echo " <TEST-SUITE>"
echo " Optionally specify a test suite; otherwise all test suites are performed."
echo " Acceptable Values: phpunit"
echo " Test Suite Descriptions:"
echo " phpunit: \"Unit\" phpunit test suite; see phpunit.xml"
echo " If xdebug is available, a coverage report in text format is (re)generated unless the '--skip-coverage' option is provided."
echo " Coverage report path: $ME_DIR/coverage.txt"
echo " HTML coverage report dir: $HTML_ROOT/.coverage"
echo ""
echo "Options:"
echo " --skip-coverage"
echo " Always skip creating coverage reports."
echo ""
echo " --html-coverage"
echo " Creates a coverage report in HTML format in a hidden folder in the project's 'web' directory."
echo " Ignored if xdebug is not available."
echo ""
echo " --print-coverage"
echo " Outputs a text coverage report after unit test completion."
echo " Ignored if xdebug is not available."
echo ""
echo "Exit code meanings:"
echo " $ME_ERROR_USAGE: command-line usage error"
echo " $ME_ERROR_MISSING_DEP: missing required dependency"
echo " $ME_ERROR_ONE_OR_MORE_TESTS_FAILED: one or more tests failed"
exit 0
fi
if [ "$ME_DIR" != "$(pwd)" ]; then
cd $ME_DIR || {
>&2 echo "$ME_NAME: failed to change to app root directory"
exit 1
}
fi
cmd_status_filter() {
cmd_status=$1
! [ "$cmd_status" -eq "$cmd_status" ] 2> /dev/null && return 1
test "${CMD_STATUS_DONTUSE#*$cmd_status}" != "$CMD_STATUS_DONTUSE" && return 1
( [ "$cmd_status" -lt "126" ] || [ "$cmd_status" -gt "165" ] ) && return $cmd_status
return 1
}
PHPUNIT_STATUS=-1
phpunit_sanity_check() {
[ "$PHPUNIT_STATUS" != "-1" ] && return $PHPUNIT_STATUS
if [ ! -f "$PHPUNIT_BIN" ]; then
>&2 echo "$ME_NAME: phpunit binary '$PHPUNIT_BIN' is missing or inaccessible, have you run composer?"
PHPUNIT_STATUS=$ME_ERROR_MISSING_DEP
return $ME_ERROR_MISSING_DEP
fi
if [ ! -x "$PHPUNIT_BIN" ]; then
>&2 echo "$ME_NAME: phpunit binary '$PHPUNIT_BIN' is not executable"
PHPUNIT_STATUS=$ME_ERROR_MISSING_DEP
return $ME_ERROR_MISSING_DEP
fi
PHPUNIT_STATUS=0
}
#
# phpunit wrapper function
#
phpunit() {
$PHPUNIT_BIN "$@" || {
cmd_status=$?
>&2 echo "$ME_NAME: phpunit failed with exit code $cmd_status"
cmd_status_filter $cmd_status
return
}
return 0
}
XDEBUG_STATUS=-1
xdebug_sanity_check() {
[ "$XDEBUG_STATUS" != "-1" ] && return $XDEBUG_STATUS
php -m 2> /dev/null | grep xdebug > /dev/null 2>&1
XDEBUG_STATUS=$?
[ "$XDEBUG_STATUS" = "0" ] || {
>&2 echo "$ME_NAME: (NOTICE) xdebug is not available, will skip coverage reports"
}
return $XDEBUG_STATUS
}
phpunit_coverage_check() {
[ "$SKIP_COVERAGE_REPORT" = "0" ] || return 0
xdebug_sanity_check || return 0
}
print_phpunit_text_coverage_path() {
local test_suffix=$1
if [ -z "$test_suffix" ]; then
printf "coverage.txt"
else
printf "coverage-$test_suffix.txt"
fi
}
print_phpunit_html_coverage_path() {
local test_suffix=$1
if [ -z "$test_suffix" ]; then
printf "$HTML_ROOT/.coverage"
else
printf "$HTML_ROOT/.coverage-$test_suffix"
fi
}
print_phpunit_coverage_opt() {
local test_suffix=$1
[ "$SKIP_COVERAGE_REPORT" = "0" ] || return 0
xdebug_sanity_check || return 0
if [ "$HTML_COVERAGE_REPORT" = "1" ]; then
printf " --coverage-html=$(print_phpunit_html_coverage_path $test_suffix) "
fi
printf " --coverage-text=$(print_phpunit_text_coverage_path $test_suffix) "
}
print_phpunit_coverage_report() {
local test_suffix=$1
phpunit_coverage_check || return 0
[ "$PRINT_COVERAGE" = "1" ] || return 0
[ -f "$(print_phpunit_text_coverage_path $test_suffix)" ] || return 0
printf "\n$(print_phpunit_text_coverage_path):\n"
cat $(print_phpunit_text_coverage_path)
}
TEST_SUITE=$1
#
# determine if wrapper mode specified by TEST_SUITE
#
if [ -n "$TEST_SUITE" ]; then
shift
#
# apply phpunit wrapper mode
#
if [ "$TEST_SUITE" = "phpunit" ]; then
phpunit_sanity_check || exit
phpunit $(print_phpunit_coverage_opt) "$@" || {
cmd_status_filter $?
exit
}
print_phpunit_coverage_report
exit 0
fi
case $TEST_SUITE in
phpunit-*)
if [ -f "$TEST_SUITE.xml" ]; then
TEST_SUFFIX=$(echo $file | sed -e 's/phpunit-//g')
TEST_SUFFIX=$(echo $TEST_SUFFIX | sed -e 's/.xml//g')
phpunit_sanity_check || exit
phpunit $(print_phpunit_coverage_opt $TEST_SUFFIX) -c "$TEST_SUITE.xml" "$@" || {
cmd_status_filter $?
exit
}
print_phpunit_coverage_report $TEST_SUFFIX
exit 0
fi
;;
esac
>&2 echo "$ME_NAME: (FATAL) unrecognized test suite: $TEST_SUITE"
>&2 print_hint
exit $ME_ERROR_USAGE
fi
#
# no TEST_SUITE specified: perform ALL tests
#
#
# sanity check test commands
#
phpunit_sanity_check || exit
#
# tests status
#
TESTS_STATUS=0
echo "print_phpunit_coverage_opt: $(print_phpunit_coverage_opt)"
#
# run all phpunit tests
#
phpunit $(print_phpunit_coverage_opt)
CMD_STATUS=$?
if [ "$CMD_STATUS" = "0" ]; then
print_phpunit_coverage_report
else
TESTS_STATUS=$ME_ERROR_ONE_OR_MORE_TESTS_FAILED
fi
for file in phpunit-*.xml; do
[ -f "$file" ] || continue
TEST_SUFFIX=$(echo $file | sed -e 's/phpunit-//g')
TEST_SUFFIX=$(echo $TEST_SUFFIX | sed -e 's/.xml//g')
phpunit $(print_phpunit_coverage_opt $TEST_SUFFIX) -c $(basename $file)
CMD_STATUS=$?
if [ "$CMD_STATUS" = "0" ]; then
print_phpunit_coverage_report $TEST_SUFFIX
else
TESTS_STATUS=$ME_ERROR_ONE_OR_MORE_TESTS_FAILED
fi
done
[ "$TESTS_STATUS" -eq "0" ] || {
>&2 echo "$ME_NAME: one or more tests failed"
exit $TESTS_STATUS
}