forked from etetoolkit/ete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tests.sh
executable file
·287 lines (246 loc) · 9.34 KB
/
run_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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
# run_tests.sh -- created 2016-04-28, Renato Alves
export TOP_PID=$$
# If a command in a pipe fails, the whole command fails
set -o pipefail
usage() {
echo >&2 ""
echo >&2 "This script runs the test suite provided with ete3."
echo >&2 "Without any optional parameters it defaults to running the api tests"
echo >&2 "on a Python 3.5 environment."
echo >&2 ""
echo >&2 "Usage:"
echo >&2 " $0 [option] [-v version] [testset]"
echo >&2 ""
echo >&2 "Optional parameters:"
echo >&2 " -h --help = shows the current information"
echo >&2 " --setup-only = configures and updates the testing environment but skips tests"
echo >&2 " --test-only = skips configuration of environment and runs tests"
echo >&2 " -l = enable logging to tests.log"
echo >&2 " -s = show the content of tests.log at the end of execution (implies -l)"
echo >&2 " -x = install ete3 external apps in the test environment"
echo >&2 " -v = python version to use for running tests. (default: 3.5)"
echo >&2 " testset = set of tests to run. (default: api) (for full testsuite use all)"
echo >&2 ""
}
valid_version() {
if [ -z "$1" ]; then
echo "$2" # No version was specified
else
if ! [[ $1 =~ ^[2-3](\.[0-9]|\.[1-9][0-9]){0,2}$ ]]; then
echo >&2 -e "\nERROR: Python version should be in one of the following formats: 3, 3.5, 3.5.12 not $1"
usage
kill -s TERM $TOP_PID
else
echo "$1"
fi
fi
}
handle_error() {
if [ "$1" != "0" ]; then
echo >&2 -e "\n$2"
echo >&2 "$3"
kill -s TERM $TOP_PID
fi
}
optional() {
if [ "x$1" == "x" ]; then
echo "$2"
else
echo "$1"
fi
}
setup_miniconda() {
if ! [ -f "${CONDA}/bin/conda" ]; then
echo -n ">>> Downloading miniconda... "
wget_output=$(wget -nv https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh 2>&1 | tee -a ${LOG})
handle_error "$?" "ERROR: Failed to download miniconda installation script" "$wget_output"
echo "DONE"
echo -n ">>> Installing miniconda... "
install_output=$(bash miniconda.sh -b -p ${CONDA} 2>&1 | tee -a ${LOG})
handle_error "$?" "ERROR: Failed to install miniconda" "$install_output"
echo "DONE"
echo -n ">>> Updating miniconda packages and package information... "
conda_update_output=$(${CONDA}/bin/conda update -q -y conda 2>&1 | tee -a ${LOG})
handle_error "$?" "ERROR: Failed to update conda packages" "$conda_update_output"
echo "DONE"
echo -n ">>> Cleaning up miniconda installation files... "
clean_output=$(rm -f miniconda.sh 2>&1 | tee -a ${LOG})
handle_error "$?" "ERROR: Failed to remove miniconda.sh" "$clean_output"
echo "DONE"
fi
echo -n ">>> Collecting information about conda... "
info_output=$(${CONDA}/bin/conda info -a 2>&1 | tee -a ${LOG})
handle_error "$?" "ERROR: Failed to collect information about conda" "$info_output"
echo "DONE"
}
create_env() {
env_output=$(${CONDA}/bin/conda env list | grep test_${VERSION})
if [ $? == 0 ]; then
# Env already created. Nothing to do
echo >&2 "### Using existing conda environment test_${VERSION}."
return 0
fi
echo -n ">>> Creating test environment for version ${VERSION}... "
create_output=$(${CONDA}/bin/conda create -q -y -n test_${VERSION} python=${VERSION} pip pyqt numpy six lxml coverage scikit-bio biopython scipy 2>&1 | tee -a ${LOG})
handle_error "$?" "ERROR: Failed to create a new conda environment for python ${VERSION}" "$create_output"
echo "DONE"
}
install_external_apps() {
if [ "$EXTERNAL_APPS" == "1" ]; then
echo -n ">>> Installing external packages in environment test_${VERSION}... "
external_apps_output=$(source ${CONDA}/bin/activate test_${VERSION} 2>&1 && ${CONDA}/bin/conda install -c etetoolkit ete3_external_apps -y 2>&1 | tee -a ${LOG})
handle_error "$?" "ERROR: Failed to install ete3 external apps in the conda environment" "$external_apps_output"
echo "DONE"
fi
}
update_env() {
echo -n ">>> Updating conda packages in environment test_${VERSION}... "
update_conda_output=$(source ${CONDA}/bin/activate test_${VERSION} 2>&1 && ${CONDA}/bin/conda update -y --all 2>&1 | tee -a ${LOG})
handle_error "$?" "ERROR: Failed to update packages in the conda environment" "$update_conda_output"
echo "DONE"
echo -n ">>> Installing latest ete in test environment... "
update_output=$(source ${CONDA}/bin/activate test_${VERSION} 2>&1 && python setup.py install --donottrackinstall 2>&1 | tee -a ${LOG})
handle_error "$?" "ERROR: Failed to install/update ete to the latest commit on test_${VERSION}" "$update_output"
echo "DONE"
}
# Find a free X server number by looking at .X*-lock files in /tmp.
find_free_servernum() {
# Start on display 99
i=99
while [ -f /tmp/.X$i-lock ]; do
i=$(($i + 1))
done
echo $i
}
start_xvfb() {
echo -n ">>> Starting Xvfb on display ${DISPLAY}... "
# NOTE -noreset is needed in some versions of Xvfb.
# The default is -reset but this causes the server to crash when the last client disconnects
Xvfb ${DISPLAY} -noreset -screen 0 1280x800x16 &>> ${LOG} &
# Giving xvfb some time to start
sleep $SLEEP
# Checking if Xvfb is still running
kill -0 $!
handle_error "$?" "ERROR: Xvfb didn't start properly" "Please re-run the test script with option -l and check the log file"
echo "DONE"
}
shutdown_xvfb() {
# Don't trap exit codes any longer
trap - EXIT HUP INT QUIT TERM KILL
if [ "$!" != "0" ] && [ "x$!" != "x" ]; then
echo -n ">>> Stopping Xvfb... "
xvfb_stop_output=$(kill $! 2>&1 | tee -a ${LOG})
handle_error "$?" "ERROR: Failed to stop Xvfb" "$xvfb_stop_output"
echo "DONE"
fi
}
showlog() {
if [ "$SHOWLOG" == "1" ]; then
echo -e ">>> Showing contents of test log:\n"
[ -f "${LOG}" ] && cat ${LOG}
fi
}
run_tests() {
echo -n ">>> Obtaining deployed python version... "
py_version_output=$(source ${CONDA}/bin/activate test_${VERSION} &>/dev/null && python --version 2>&1 | tee -a ${LOG})
handle_error "$?" "ERROR: couldn't obtain python version" "$py_version_output"
echo "DONE"
echo -n ">>> Obtaining ete3 version... "
ete_version_output=$(source ${CONDA}/bin/activate test_${VERSION} &>/dev/null && ete3 version 2>&1 | tee -a ${LOG})
handle_error "$?" "ERROR: couldn't obtain ete3 version" "$ete_version_output"
echo "DONE"
echo -n ">>> Running tests on ete ${ete_version_output} using ${py_version_output}... "
tests_output=$(source ${CONDA}/bin/activate test_${VERSION} 2>&1 && coverage run -m ete3.test.test_${TESTSET} 2>&1 | tee -a ${LOG})
handle_error "$?" "ERROR: One or more tests failed on test_${VERSION}" "$tests_output"
echo "DONE"
echo "### All tests passed successfully"
}
# Location of current script
FILEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
CONDA="${FILEDIR}/test_tmp/miniconda"
EXTERNAL_APPS=0
SETUP=1
TEST=1
# Logfile
NULL_LOG="/dev/null"
LOG="${NULL_LOG}"
DEFAULT_LOG="${FILEDIR}/tests.log"
SHOWLOG=0
# Parse args using getopt (instead of getopts) to allow arguments before options
ARGS="$(getopt -o v:lshx -l help,test-only,setup-only -n "$0" -- "$@" 2>/dev/null)"
if [ "$?" != "0" ]; then
usage
handle_error "1" "ERROR: Check the arguments passed. At least one was not valid."
fi
# reorganize arguments as returned by getopt
eval set -- "$ARGS"
while true; do
case "$1" in
# Shift before to throw away option
# Shift after if option has a required positional argument
-l)
shift
LOG="${DEFAULT_LOG}"
;;
-s)
shift
SHOWLOG=1
LOG="${DEFAULT_LOG}"
;;
-v)
shift
# Validate provided python version to use for tests
VERSION="$1"
shift
;;
-x)
shift
EXTERNAL_APPS=1
;;
--setup-only)
shift
TEST=0
;;
--test-only)
shift
SETUP=0
;;
-h|--help)
usage
exit
;;
--)
shift
break
;;
esac
done
VERSION="$(valid_version ${VERSION} 3.5)"
TESTSET="$(optional $1 api)"
export DISPLAY=":$(find_free_servernum)"
SLEEP=2 # Time to wait for xvfb to start and be functional
[ ! -f "ete3/test/test_${TESTSET}.py" ] && usage && handle_error "1" "ERROR: Invalid testset selected ${TESTSET}"
if [ "${LOG}" == "${NULL_LOG}" ]; then
echo "### Running ${TESTSET} tests with Python ${VERSION}"
else
echo "### Running ${TESTSET} tests with Python ${VERSION} and logging to ${LOG}"
fi
# Empty logfile
echo -n > ${LOG}
# At any of these signals shutdown xvfb first and conditionally show the
# contents of the logfile
trap 'exitcode=$? ; shutdown_xvfb ; showlog ; exit $exitcode' EXIT HUP INT QUIT TERM KILL
if [ "${SETUP}" == "1" ]; then
setup_miniconda
create_env
install_external_apps
update_env
fi
if [ "${TEST}" == "1" ]; then
start_xvfb
run_tests
shutdown_xvfb
fi
showlog
# vi: