forked from Project-MONAI/MONAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtests.sh
executable file
·547 lines (474 loc) · 15.4 KB
/
runtests.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#! /bin/bash
# Copyright 2020 - 2021 MONAI Consortium
# 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.
# script for running all tests
set -e
# output formatting
separator=""
blue=""
green=""
red=""
noColor=""
if [[ -t 1 ]] # stdout is a terminal
then
separator=$'--------------------------------------------------------------------------------\n'
blue="$(tput bold; tput setaf 4)"
green="$(tput bold; tput setaf 2)"
red="$(tput bold; tput setaf 1)"
noColor="$(tput sgr0)"
fi
# configuration values
doCoverage=false
doQuickTests=false
doNetTests=false
doDryRun=false
doZooTests=false
doUnitTests=false
doBlackFormat=false
doBlackFix=false
doIsortFormat=false
doIsortFix=false
doFlake8Format=false
doClangFormat=false
doPytypeFormat=false
doMypyFormat=false
doCleanup=false
NUM_PARALLEL=1
PY_EXE=${MONAI_PY_EXE:-$(which python)}
function print_usage {
echo "runtests.sh [--codeformat] [--autofix] [--black] [--isort] [--flake8] [--clangformat] [--pytype] [--mypy]"
echo " [--unittests] [--coverage] [--quick] [--net] [--dryrun] [-j number] [--clean] [--help] [--version]"
echo ""
echo "MONAI unit testing utilities."
echo ""
echo "Examples:"
echo "./runtests.sh -f -u --net --coverage # run style checks, full tests, print code coverage (${green}recommended for pull requests${noColor})."
echo "./runtests.sh -f -u # run style checks and unit tests."
echo "./runtests.sh -f # run coding style and static type checking."
echo "./runtests.sh --quick --unittests # run minimal unit tests, for quick verification during code developments."
echo "./runtests.sh --autofix # run automatic code formatting using \"isort\" and \"black\"."
echo "./runtests.sh --clean # clean up temporary files and run \"${PY_EXE} setup.py develop --uninstall\"."
echo ""
echo "Code style check options:"
echo " --black : perform \"black\" code format checks"
echo " --autofix : format code using \"isort\" and \"black\""
echo " --isort : perform \"isort\" import sort checks"
echo " --flake8 : perform \"flake8\" code format checks"
echo " --clangformat : format csrc code using \"clang-format\""
echo ""
echo "Python type check options:"
echo " --pytype : perform \"pytype\" static type checks"
echo " --mypy : perform \"mypy\" static type checks"
echo " -j, --jobs : number of parallel jobs to run \"pytype\" (default $NUM_PARALLEL)"
echo ""
echo "MONAI unit testing options:"
echo " -u, --unittests : perform unit testing"
echo " --coverage : report testing code coverage, to be used with \"--net\", \"--unittests\""
echo " -q, --quick : skip long running unit tests and integration tests"
echo " --net : perform integration testing"
echo " --list_tests : list unit tests and exit"
echo ""
echo "Misc. options:"
echo " --dryrun : display the commands to the screen without running"
echo " -f, --codeformat : shorthand to run all code style and static analysis tests"
echo " -c, --clean : clean temporary files from tests and exit"
echo " -h, --help : show this help message and exit"
echo " -v, --version : show MONAI and system version information and exit"
echo ""
echo "${separator}For bug reports and feature requests, please file an issue at:"
echo " https://github.com/Project-MONAI/MONAI/issues/new/choose"
echo ""
echo "To choose an alternative python executable, set the environmental variable, \"MONAI_PY_EXE\"."
exit 1
}
function check_import {
echo "python: ${PY_EXE}"
${cmdPrefix}${PY_EXE} -c "import monai"
}
function print_version {
${cmdPrefix}${PY_EXE} -c 'import monai; monai.config.print_config()'
}
function install_deps {
echo "Pip installing MONAI development dependencies and compile MONAI cpp extensions..."
${cmdPrefix}${PY_EXE} -m pip install -r requirements-dev.txt
}
function compile_cpp {
echo "Compiling and installing MONAI cpp extensions..."
# depends on setup.py behaviour for building
# currently setup.py uses environment variables: BUILD_MONAI and FORCE_CUDA
${cmdPrefix}${PY_EXE} setup.py develop --user --uninstall
if [[ "$OSTYPE" == "darwin"* ]];
then # clang for mac os
CC=clang CXX=clang++ ${cmdPrefix}${PY_EXE} setup.py develop --user
else
${cmdPrefix}${PY_EXE} setup.py develop --user
fi
}
function clang_format {
echo "Running clang-format..."
${cmdPrefix}${PY_EXE} -m tests.clang_format_utils
clang_format_tool='.clang-format-bin/clang-format'
# Verify .
if ! type -p "$clang_format_tool" >/dev/null; then
echo "'clang-format' not found, skipping the formatting."
exit 1
fi
find monai/csrc -type f | while read i; do $clang_format_tool -style=file -i $i; done
}
function clean_py {
# remove coverage history
${cmdPrefix}${PY_EXE} -m coverage erase
# uninstall the development package
echo "Uninstalling MONAI development files..."
${cmdPrefix}${PY_EXE} setup.py develop --user --uninstall
# remove temporary files (in the directory of this script)
TO_CLEAN="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
echo "Removing temporary files in ${TO_CLEAN}"
find ${TO_CLEAN}/monai -type f -name "*.py[co]" -delete
find ${TO_CLEAN}/monai -type f -name "*.so" -delete
find ${TO_CLEAN}/monai -type d -name "__pycache__" -delete
find ${TO_CLEAN} -maxdepth 1 -type f -name ".coverage.*" -delete
find ${TO_CLEAN} -depth -maxdepth 1 -type d -name ".eggs" -exec rm -r "{}" +
find ${TO_CLEAN} -depth -maxdepth 1 -type d -name "monai.egg-info" -exec rm -r "{}" +
find ${TO_CLEAN} -depth -maxdepth 1 -type d -name "build" -exec rm -r "{}" +
find ${TO_CLEAN} -depth -maxdepth 1 -type d -name "dist" -exec rm -r "{}" +
find ${TO_CLEAN} -depth -maxdepth 1 -type d -name ".mypy_cache" -exec rm -r "{}" +
find ${TO_CLEAN} -depth -maxdepth 1 -type d -name ".pytype" -exec rm -r "{}" +
find ${TO_CLEAN} -depth -maxdepth 1 -type d -name ".coverage" -exec rm -r "{}" +
find ${TO_CLEAN} -depth -maxdepth 1 -type d -name "__pycache__" -exec rm -r "{}" +
}
function torch_validate {
${cmdPrefix}${PY_EXE} -c 'import torch; print(torch.__version__); print(torch.rand(5,3))'
}
function print_error_msg() {
echo "${red}Error: $1.${noColor}"
echo ""
}
function print_style_fail_msg() {
echo "${red}Check failed!${noColor}"
echo "Please run auto style fixes: ${green}./runtests.sh --autofix${noColor}"
}
function is_pip_installed() {
return $(${PY_EXE} -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader(sys.argv[1]) else 1)" $1)
}
function list_unittests() {
${PY_EXE} - << END
import unittest
def print_suite(suite):
if hasattr(suite, "__iter__"):
for x in suite:
print_suite(x)
else:
print(suite)
print_suite(unittest.defaultTestLoader.discover('./tests'))
END
exit 0
}
if [ -z "$1" ]
then
print_error_msg "Too few arguments to $0"
print_usage
fi
# parse arguments
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--coverage)
doCoverage=true
;;
-q|--quick)
doQuickTests=true
;;
--net)
doNetTests=true
;;
--list_tests)
list_unittests
;;
--dryrun)
doDryRun=true
;;
-u|--u*) # allow --unittest | --unittests | --unittesting etc.
doUnitTests=true
;;
-f|--codeformat)
doBlackFormat=true
doIsortFormat=true
doFlake8Format=true
doPytypeFormat=true
doMypyFormat=true
;;
--black)
doBlackFormat=true
;;
--autofix)
doIsortFix=true
doBlackFix=true
doIsortFormat=true
doBlackFormat=true
;;
--clangformat)
doClangFormat=true
;;
--isort)
doIsortFormat=true
;;
--flake8)
doFlake8Format=true
;;
--pytype)
doPytypeFormat=true
;;
--mypy)
doMypyFormat=true
;;
-j|--jobs)
NUM_PARALLEL=$2
shift
;;
-c|--clean)
doCleanup=true
;;
-h|--help)
print_usage
;;
-v|--version)
print_version
exit 1
;;
--nou*) # allow --nounittest | --nounittests | --nounittesting etc.
print_error_msg "nounittest option is deprecated, no unit tests is the default setting"
print_usage
;;
*)
print_error_msg "Incorrect commandline provided, invalid key: $key"
print_usage
;;
esac
shift
done
# home directory
homedir="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$homedir"
# python path
export PYTHONPATH="$homedir:$PYTHONPATH"
echo "PYTHONPATH: $PYTHONPATH"
# by default do nothing
cmdPrefix=""
if [ $doDryRun = true ]
then
echo "${separator}${blue}dryrun${noColor}"
# commands are echoed instead of ran
cmdPrefix="dryrun "
function dryrun { echo " " "$@"; }
else
check_import
fi
if [ $doCleanup = true ]
then
echo "${separator}${blue}clean${noColor}"
clean_py
echo "${green}done!${noColor}"
exit
fi
if [ $doClangFormat = true ]
then
echo "${separator}${blue}clang-formatting${noColor}"
clang_format
echo "${green}done!${noColor}"
exit
fi
# try to compile MONAI cpp
compile_cpp
# unconditionally report on the state of monai
print_version
if [ $doIsortFormat = true ]
then
set +e # disable exit on failure so that diagnostics can be given on failure
if [ $doIsortFix = true ]
then
echo "${separator}${blue}isort-fix${noColor}"
else
echo "${separator}${blue}isort${noColor}"
fi
# ensure that the necessary packages for code format testing are installed
if ! is_pip_installed isort
then
install_deps
fi
${cmdPrefix}isort --version
if [ $doIsortFix = true ]
then
${cmdPrefix}${PY_EXE} -m isort "$(pwd)"
else
${cmdPrefix}${PY_EXE} -m isort --check "$(pwd)"
fi
isort_status=$?
if [ ${isort_status} -ne 0 ]
then
print_style_fail_msg
exit ${isort_status}
else
echo "${green}passed!${noColor}"
fi
set -e # enable exit on failure
fi
if [ $doBlackFormat = true ]
then
set +e # disable exit on failure so that diagnostics can be given on failure
if [ $doBlackFix = true ]
then
echo "${separator}${blue}black-fix${noColor}"
else
echo "${separator}${blue}black${noColor}"
fi
# ensure that the necessary packages for code format testing are installed
if ! is_pip_installed black
then
install_deps
fi
${cmdPrefix}${PY_EXE} -m black --version
if [ $doBlackFix = true ]
then
${cmdPrefix}${PY_EXE} -m black "$(pwd)"
else
${cmdPrefix}${PY_EXE} -m black --check "$(pwd)"
fi
black_status=$?
if [ ${black_status} -ne 0 ]
then
print_style_fail_msg
exit ${black_status}
else
echo "${green}passed!${noColor}"
fi
set -e # enable exit on failure
fi
if [ $doFlake8Format = true ]
then
set +e # disable exit on failure so that diagnostics can be given on failure
echo "${separator}${blue}flake8${noColor}"
# ensure that the necessary packages for code format testing are installed
if ! is_pip_installed flake8
then
install_deps
fi
${cmdPrefix}${PY_EXE} -m flake8 --version
${cmdPrefix}${PY_EXE} -m flake8 "$(pwd)" --count --statistics
flake8_status=$?
if [ ${flake8_status} -ne 0 ]
then
print_style_fail_msg
exit ${flake8_status}
else
echo "${green}passed!${noColor}"
fi
set -e # enable exit on failure
fi
if [ $doPytypeFormat = true ]
then
set +e # disable exit on failure so that diagnostics can be given on failure
echo "${separator}${blue}pytype${noColor}"
# ensure that the necessary packages for code format testing are installed
if ! is_pip_installed pytype
then
install_deps
fi
${cmdPrefix}${PY_EXE} -m pytype --version
${cmdPrefix}${PY_EXE} -m pytype -j ${NUM_PARALLEL} --python-version="$(${PY_EXE} -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")"
pytype_status=$?
if [ ${pytype_status} -ne 0 ]
then
echo "${red}failed!${noColor}"
exit ${pytype_status}
else
echo "${green}passed!${noColor}"
fi
set -e # enable exit on failure
fi
if [ $doMypyFormat = true ]
then
set +e # disable exit on failure so that diagnostics can be given on failure
echo "${separator}${blue}mypy${noColor}"
# ensure that the necessary packages for code format testing are installed
if ! is_pip_installed mypy
then
install_deps
fi
${cmdPrefix}${PY_EXE} -m mypy --version
if [ $doDryRun = true ]
then
${cmdPrefix}MYPYPATH="$(pwd)"/monai ${PY_EXE} -m mypy "$(pwd)"
else
MYPYPATH="$(pwd)"/monai ${PY_EXE} -m mypy "$(pwd)" # cmdPrefix does not work with MYPYPATH
fi
mypy_status=$?
if [ ${mypy_status} -ne 0 ]
then
: # mypy output already follows format
exit ${mypy_status}
else
: # mypy output already follows format
fi
set -e # enable exit on failure
fi
# testing command to run
cmd="${PY_EXE}"
# When running --quick, require doCoverage as well and set QUICKTEST environmental
# variable to disable slow unit tests from running.
if [ $doQuickTests = true ]
then
echo "${separator}${blue}quick${noColor}"
doCoverage=true
export QUICKTEST=True
fi
# set coverage command
if [ $doCoverage = true ]
then
echo "${separator}${blue}coverage${noColor}"
cmd="${PY_EXE} -m coverage run --append"
fi
# # download test data if needed
# if [ ! -d testing_data ] && [ "$doDryRun" != 'true' ]
# then
# fi
# unit tests
if [ $doUnitTests = true ]
then
echo "${separator}${blue}unittests${noColor}"
torch_validate
${cmdPrefix}${cmd} ./tests/runner.py -p "test_((?!integration).)"
fi
# network training/inference/eval integration tests
if [ $doNetTests = true ]
then
echo "${separator}${blue}integration${noColor}"
for i in tests/*integration_*.py
do
echo "$i"
${cmdPrefix}${cmd} "$i"
done
fi
# run model zoo tests
if [ $doZooTests = true ]
then
echo "${separator}${blue}zoo${noColor}"
print_error_msg "--zoo option not yet implemented"
exit 255
fi
# report on coverage
if [ $doCoverage = true ]
then
echo "${separator}${blue}coverage${noColor}"
${cmdPrefix}${PY_EXE} -m coverage combine --append .coverage/
${cmdPrefix}${PY_EXE} -m coverage report
fi