-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-uts.bash
executable file
·151 lines (140 loc) · 3.15 KB
/
run-uts.bash
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
#!/bin/bash
# all-uts.sh
#
# Not a very elegant way of running all the UTs.
#
# Copyright 2015 David Park
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
typeset -a tests
PREPEND=sipparty.test
COLORIZE=1
while (( $# > 0 ))
do
if [[ $1 =~ (-l|--list) ]]
then
LIST=1
elif [[ $1 =~ ^(--no-color)$ ]]; then
COLORIZE=0
else
tests[${#tests[@]}]=${PREPEND}.$1
fi
shift
done
if (( LIST ))
then
for tpath in sipparty/test/test*.py
do
tfile=${tpath##*/}
tname=${tfile%.py}
echo "${tname}"
done
exit 0
fi
colorize () {
if (( ! COLORIZE ))
then
cat
return
fi
while IFS= read -r line
do
if [[ $line =~ ERROR ]]
then
echo -n $'\033[41m'
elif [[ $line =~ WARNING ]]
then
echo -n $'\033[43m'
fi
echo -n "$line"
echo -n $'\033[0m'
echo
done
}
killchildren () {
local ppid=$1
local signal=$2
local cpid
if [[ -z ${signal} ]]
then
signal=KILL
fi
local children=$(pgrep -P ${ppid})
echo "Children are $children"
for cpid in $children
do
killchildren "${cpid}" "$signal"
echo "killing $cpid..." >&2
kill_attempts=0
while kill -${signal} ${cpid}
do
(( kill_attempts++ ))
sleep 0.1
if ! kill -0 ${cpid}
then
echo "dead" >&2
break
fi
sleep 1
if (( kill_attempts > 3 ))
then
echo "Tried 3 times, using KILL" >&2
signal=KILL
else
echo "Child not dead yet, try again..." >&2
fi
done
done
}
# Get the python version
pyver=$(python --version 2>&1 | sed -nE 's/Python ([0-9]+.[0-9]+.[0-9]+)/\1/p')
pymajver=${pyver%%.*}
pyminver=${pyver#*.}
pypatchver=${pyminver#*.}
pyminver=${pyminver%.*}
for signal in INT TERM ABRT QUIT
do
trap \
"echo \"Kill children $signal\";"\
"killchildren $$ ${signal};"\
$signal
done
find . -name "*.pyc" -delete
if (( ${#tests} > 0 ))
then
run_tests () {
python -m unittest "${tests[@]}" 2>&1 | colorize >&2
return ${PIPESTATUS[0]}
}
else
if (( pymajver > 2 ))
then
run_tests () {
python -m unittest 2>&1 | colorize >&2
return ${PIPESTATUS[0]}
}
else
run_tests () {
python -m unittest discover 2>&1 | colorize >&2
return ${PIPESTATUS[0]}
}
fi
fi
run_tests &
child_pid=$!
echo "Waiting for child $child_pid"
wait "${child_pid}"
rc=$?
echo "child exited with $rc"
exit $rc