Skip to content

Commit 5c6d199

Browse files
authored
Code formatting for user specified directories (#7106)
Fixes #7099 ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: KumoLiu <yunl@nvidia.com>
1 parent 825021d commit 5c6d199

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

runtests.sh

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function print_usage {
6363
echo "runtests.sh [--codeformat] [--autofix] [--black] [--isort] [--flake8] [--pylint] [--ruff]"
6464
echo " [--clangformat] [--precommit] [--pytype] [-j number] [--mypy]"
6565
echo " [--unittests] [--disttests] [--coverage] [--quick] [--min] [--net] [--build] [--list_tests]"
66-
echo " [--dryrun] [--copyright] [--clean] [--help] [--version]"
66+
echo " [--dryrun] [--copyright] [--clean] [--help] [--version] [--path] [--formatfix]"
6767
echo ""
6868
echo "MONAI unit testing utilities."
6969
echo ""
@@ -74,6 +74,7 @@ function print_usage {
7474
echo "./runtests.sh --quick --unittests # run minimal unit tests, for quick verification during code developments."
7575
echo "./runtests.sh --autofix # run automatic code formatting using \"isort\" and \"black\"."
7676
echo "./runtests.sh --clean # clean up temporary files and run \"${PY_EXE} setup.py develop --uninstall\"."
77+
echo "./runtests.sh --formatfix -p /my/code # run automatic code formatting using \"isort\" and \"black\" in specified path."
7778
echo ""
7879
echo "Code style check options:"
7980
echo " --autofix : format code using \"isort\" and \"black\""
@@ -107,6 +108,8 @@ function print_usage {
107108
echo " -c, --clean : clean temporary files from tests and exit"
108109
echo " -h, --help : show this help message and exit"
109110
echo " -v, --version : show MONAI and system version information and exit"
111+
echo " -p, --path : specify the path used for formatting"
112+
echo " --formatfix : format code using \"isort\" and \"black\" for user specified directories"
110113
echo ""
111114
echo "${separator}For bug reports and feature requests, please file an issue at:"
112115
echo " https://github.com/Project-MONAI/MONAI/issues/new/choose"
@@ -208,7 +211,11 @@ function print_error_msg() {
208211

209212
function print_style_fail_msg() {
210213
echo "${red}Check failed!${noColor}"
211-
echo "Please run auto style fixes: ${green}./runtests.sh --autofix${noColor}"
214+
if [ "$homedir" = "$currentdir" ]
215+
then
216+
echo "Please run auto style fixes: ${green}./runtests.sh --autofix${noColor}"
217+
else :
218+
fi
212219
}
213220

214221
function list_unittests() {
@@ -280,6 +287,12 @@ do
280287
doRuffFormat=true
281288
doCopyRight=true
282289
;;
290+
--formatfix)
291+
doIsortFix=true
292+
doBlackFix=true
293+
doIsortFormat=true
294+
doBlackFormat=true
295+
;;
283296
--clangformat)
284297
doClangFormat=true
285298
;;
@@ -328,6 +341,10 @@ do
328341
print_error_msg "nounittest option is deprecated, no unit tests is the default setting"
329342
print_usage
330343
;;
344+
-p|--path)
345+
testdir=$2
346+
shift
347+
;;
331348
*)
332349
print_error_msg "Incorrect commandline provided, invalid key: $key"
333350
print_usage
@@ -337,7 +354,15 @@ do
337354
done
338355

339356
# home directory
340-
homedir="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
357+
currentdir="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
358+
if [ -e "$testdir" ]
359+
then
360+
homedir=$testdir
361+
else
362+
print_error_msg "Incorrect path: $testdir provided, run under $currentdir"
363+
homedir=$currentdir
364+
fi
365+
echo "run tests under $homedir"
341366
cd "$homedir"
342367

343368
# python path
@@ -457,9 +482,9 @@ then
457482

458483
if [ $doIsortFix = true ]
459484
then
460-
${cmdPrefix}${PY_EXE} -m isort "$(pwd)"
485+
${cmdPrefix}${PY_EXE} -m isort "$homedir"
461486
else
462-
${cmdPrefix}${PY_EXE} -m isort --check "$(pwd)"
487+
${cmdPrefix}${PY_EXE} -m isort --check "$homedir"
463488
fi
464489

465490
isort_status=$?
@@ -493,9 +518,9 @@ then
493518

494519
if [ $doBlackFix = true ]
495520
then
496-
${cmdPrefix}${PY_EXE} -m black --skip-magic-trailing-comma "$(pwd)"
521+
${cmdPrefix}${PY_EXE} -m black --skip-magic-trailing-comma "$homedir"
497522
else
498-
${cmdPrefix}${PY_EXE} -m black --skip-magic-trailing-comma --check "$(pwd)"
523+
${cmdPrefix}${PY_EXE} -m black --skip-magic-trailing-comma --check "$homedir"
499524
fi
500525

501526
black_status=$?
@@ -522,7 +547,7 @@ then
522547
fi
523548
${cmdPrefix}${PY_EXE} -m flake8 --version
524549

525-
${cmdPrefix}${PY_EXE} -m flake8 "$(pwd)" --count --statistics
550+
${cmdPrefix}${PY_EXE} -m flake8 "$homedir" --count --statistics
526551

527552
flake8_status=$?
528553
if [ ${flake8_status} -ne 0 ]
@@ -582,9 +607,9 @@ then
582607

583608
if [ $doRuffFix = true ]
584609
then
585-
ruff check --fix "$(pwd)"
610+
ruff check --fix "$homedir"
586611
else
587-
ruff check "$(pwd)"
612+
ruff check "$homedir"
588613
fi
589614

590615
ruff_status=$?
@@ -615,7 +640,7 @@ then
615640
else
616641
${cmdPrefix}${PY_EXE} -m pytype --version
617642

618-
${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}')")" "$(pwd)"
643+
${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}')")" "$homedir"
619644

620645
pytype_status=$?
621646
if [ ${pytype_status} -ne 0 ]
@@ -641,7 +666,7 @@ then
641666
install_deps
642667
fi
643668
${cmdPrefix}${PY_EXE} -m mypy --version
644-
${cmdPrefix}${PY_EXE} -m mypy "$(pwd)"
669+
${cmdPrefix}${PY_EXE} -m mypy "$homedir"
645670

646671
mypy_status=$?
647672
if [ ${mypy_status} -ne 0 ]

0 commit comments

Comments
 (0)