-
Notifications
You must be signed in to change notification settings - Fork 180
/
runtest.sh
executable file
·323 lines (276 loc) · 9.54 KB
/
runtest.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
#!/bin/bash
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
WORK_DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
NUM_PARALLEL=1
DIR_TO_CHECK=("nvflare" "examples" "tests")
target="${@: -1}"
if [[ "${target}" == -* ]] ;then
target=""
fi
function install_deps {
if [[ $(uname) == "Darwin" ]]; then
python3 -m pip install -e .[dev_mac]
else
python3 -m pip install -e .[dev]
fi;
echo "dependencies installed"
}
function clean {
echo "remove coverage history"
python3 -m coverage erase
echo "uninstalling nvflare development files..."
python3 setup.py develop --user --uninstall
echo "removing temporary files in ${WORK_DIR}"
find "${WORK_DIR}/nvflare" -type d -name "__pycache__" -exec rm -r "{}" \;
find "${WORK_DIR}/nvflare" -type f -name "*.py[co]" -exec rm -r "{}" \;
find "${WORK_DIR}" -depth -maxdepth 1 -type d -name ".eggs" -exec rm -r "{}" \;
find "${WORK_DIR}" -depth -maxdepth 1 -type d -name "nvflare.egg-info" -exec rm -r "{}" \;
find "${WORK_DIR}" -depth -maxdepth 1 -type d -name "build" -exec rm -r "{}" \;
find "${WORK_DIR}" -depth -maxdepth 1 -type d -name "dist" -exec rm -r "{}" \;
find "${WORK_DIR}" -depth -maxdepth 1 -type d -name ".mypy_cache" -exec rm -r "{}" \;
find "${WORK_DIR}" -depth -maxdepth 1 -type d -name ".pytype" -exec rm -r "{}" \;
find "${WORK_DIR}" -depth -maxdepth 1 -type d -name ".coverage" -exec rm -r "{}" \;
find "${WORK_DIR}" -depth -maxdepth 1 -type f -name ".coverage.*" -exec rm -r "{}" \;
find "${WORK_DIR}" -depth -maxdepth 1 -type d -name "__pycache__" -exec rm -r "{}" \;
}
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 -f {noColor}"
}
function report_status() {
status="$1"
if [ "${status}" -ne 0 ]
then
print_style_fail_msg
exit "${status}"
else
echo "${green}passed!${noColor}"
fi
}
function is_pip_installed() {
echo "check target installed by pip"
return $(python3 -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader(sys.argv[1]) else 1)" $1)
}
function dry_run() {
echo "${separator}${blue}dryrun${noColor}"
echo " " "$1"
}
function check_license() {
folders_to_check_license="nvflare examples tests integration research"
echo "checking license header in folder: $folders_to_check_license"
(grep -r --include "*.py" --exclude-dir "*protos*" --exclude "modeling_roberta.py" -L \
"\(# Copyright (c) \(2021\|2022\|2023\|2024\), NVIDIA CORPORATION. All rights reserved.\)\|\(This file is released into the public domain.\)" \
${folders_to_check_license} || true) > no_license.lst
if [ -s no_license.lst ]; then
# The file is not-empty.
cat no_license.lst
echo "License text not found on the above files."
echo "Please fix them."
rm -f no_license.lst
exit 1
else
echo "All Python files in folder ${folders_to_check_license} have license header"
rm -f no_license.lst
fi
echo "finished checking license header"
}
function flake8_check() {
echo "${separator}${blue}flake8${noColor}"
python3 -m flake8 --version
python3 -m flake8 "$@" --count --statistics
report_status "$?"
}
function black_check() {
echo "${separator}${blue}black-check${noColor}"
python3 -m black --check "$@"
report_status "$?"
echo "Done with black code style checks on $@"
}
function black_fix() {
echo "${separator}${blue}black-fix${noColor}"
python3 -m black "$@"
echo "Done with black code style fix on $@"
}
function isort_check() {
echo "${separator}${blue}isort-check${noColor}"
python3 -m isort --check "$@"
report_status "$?"
}
function isort_fix() {
echo "${separator}${blue}isort-fix${noColor}"
python3 -m isort "$@"
}
# wait for approval
#function pylint_check() {
# echo "${separator}${blue}pylint${noColor}"
# python3 -m pylint --version
# ignore_codes="E1101,E1102,E0601,E1130,E1123,E0102,E1120,E1137,E1136"
# python3 -m pylint "$1" -E --disable=$ignore_codes -j $NUM_PARALLEL
# report_status "$?"
#}
function pytype_check() {
echo "${separator}${blue}pytype${noColor}"
pytype_ver=$(python3 -m pytype --version)
if [[ "$OSTYPE" == "darwin"* && "$pytype_ver" == "2021."* ]]; then
echo "${red}pytype not working on macOS 2021 (https://github.com/google/pytype/issues/661). Please upgrade to 2022*.${noColor}"
exit 1
else
python3 -m pytype --version
python3 -m pytype -j ${NUM_PARALLEL} --python-version="$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")" "$@"
report_status "$?"
fi
}
function mypy_check() {
echo "${separator}${blue}mypy${noColor}"
python3 -m mypy --version
python3 -m mypy "$1"
report_status "$?"
}
function check_style_type_import() {
# remove pylint for now
# pylint_check "$@"
black_check "$@"
isort_check "$@"
flake8_check "$@"
# pytype causing check fails, comment for now
# pytype_check "$@"
# gives a lot false alarm, comment out for now
# mypy_check "$@"
}
function fix_style_import() {
black_fix "$@"
isort_fix "$@"
}
################################################################################
export PYTHONPATH=${WORK_DIR}:${PYTHONPATH} && echo "PYTHONPATH is ${PYTHONPATH}"
function help() {
echo "Add description of the script functions here."
echo
echo "Syntax: runtests.sh [-h|--help]
[-l|--check-license]
[-s|--check-format]
[-f|--fix-format]
[-u|--unit-tests]
[-r|--test-report]
[-c|--coverage]
<target> "
echo "<target> : target directories or files used for unit tests, or license check or format checks etc. "
echo " "
echo "options:"
echo ""
echo " -h | --help : display usage help"
echo " -l | --check-license : check copy license"
echo " -s | --check-format : check code styles, formatting, typing, import"
echo " -f | --fix-format : auto fix style formats, import"
echo " -u | --unit-tests : unit tests"
echo " -r | --test-report : used with -u command, turn on unit test report flag. It has no effect without -u "
echo " -p | --dependencies : only install dependencies"
echo " -c | --coverage : used with -u command, turn on coverage flag, It has no effect without -u "
echo " -d | --dry-run : set dry run flag, print out command"
echo " --clean : clean py and other artifacts generated, clean flag to allow re-install dependencies"
# echo " -i | --integration-tests : integration tests"
exit 1
}
coverage_report=false
unit_test_report=false
dry_run_flag=false
# parse arguments
cmd=""
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
help
exit
;;
-l|--check-license)
cmd="check_license"
;;
-s |--check-format) # check format and styles
cmd="check_style_type_import"
if [[ -z $target ]]; then
target="${DIR_TO_CHECK[@]}"
fi
;;
-f |--fix-format)
cmd="fix_style_import"
if [[ -z $target ]]; then
target="${DIR_TO_CHECK[@]}"
fi
;;
-c|--coverage)
coverage_report=true
;;
-p|--dependencies)
dependencies=true
cmd=" "
;;
-r|--test-report)
echo "set unit test flag"
unit_test_report=true
;;
-u |--unit*)
cmd_prefix="python3 -m pytest --numprocesses=8 -v "
echo "coverage_report=" ${coverage_report}
if [ "${coverage_report}" == true ]; then
cmd_prefix="${cmd_prefix} --cov=${target} --cov-report html:cov_html --cov-report xml:cov.xml"
fi
if [ "${unit_test_report}" == true ]; then
cmd_prefix="${cmd_prefix} --junitxml=unit_test.xml "
fi
cmd="$cmd_prefix"
if [[ -z $target ]]; then
target="tests/unit_test"
fi
;;
--clean)
cmd="clean"
;;
-d|--dry-run)
dry_run_flag=true
;;
-*)
help
;;
esac
shift
done
if [[ -z $cmd ]]; then
cmd="check_license;
check_style_type_import "${DIR_TO_CHECK[@]}";
fix_style_import "${DIR_TO_CHECK[@]}";
python3 -m pytest --numprocesses=8 -v --cov=nvflare --cov-report html:cov_html --cov-report xml:cov.xml --junitxml=unit_test.xml --dist loadgroup tests/unit_test;
"
else
cmd="$cmd $target"
fi
echo "running command: "
echo " install_deps;"
echo " $cmd"
echo " "
if [[ $dry_run_flag = "true" ]]; then
dry_run "$cmd"
else
install_deps
eval "$cmd"
fi
echo "Done"