Skip to content

Commit

Permalink
Code formatting for user specified directories (#7106)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
KumoLiu authored Oct 9, 2023
1 parent 825021d commit 5c6d199
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function print_usage {
echo "runtests.sh [--codeformat] [--autofix] [--black] [--isort] [--flake8] [--pylint] [--ruff]"
echo " [--clangformat] [--precommit] [--pytype] [-j number] [--mypy]"
echo " [--unittests] [--disttests] [--coverage] [--quick] [--min] [--net] [--build] [--list_tests]"
echo " [--dryrun] [--copyright] [--clean] [--help] [--version]"
echo " [--dryrun] [--copyright] [--clean] [--help] [--version] [--path] [--formatfix]"
echo ""
echo "MONAI unit testing utilities."
echo ""
Expand All @@ -74,6 +74,7 @@ function print_usage {
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 "./runtests.sh --formatfix -p /my/code # run automatic code formatting using \"isort\" and \"black\" in specified path."
echo ""
echo "Code style check options:"
echo " --autofix : format code using \"isort\" and \"black\""
Expand Down Expand Up @@ -107,6 +108,8 @@ function print_usage {
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 " -p, --path : specify the path used for formatting"
echo " --formatfix : format code using \"isort\" and \"black\" for user specified directories"
echo ""
echo "${separator}For bug reports and feature requests, please file an issue at:"
echo " https://github.com/Project-MONAI/MONAI/issues/new/choose"
Expand Down Expand Up @@ -208,7 +211,11 @@ function print_error_msg() {

function print_style_fail_msg() {
echo "${red}Check failed!${noColor}"
echo "Please run auto style fixes: ${green}./runtests.sh --autofix${noColor}"
if [ "$homedir" = "$currentdir" ]
then
echo "Please run auto style fixes: ${green}./runtests.sh --autofix${noColor}"
else :
fi
}

function list_unittests() {
Expand Down Expand Up @@ -280,6 +287,12 @@ do
doRuffFormat=true
doCopyRight=true
;;
--formatfix)
doIsortFix=true
doBlackFix=true
doIsortFormat=true
doBlackFormat=true
;;
--clangformat)
doClangFormat=true
;;
Expand Down Expand Up @@ -328,6 +341,10 @@ do
print_error_msg "nounittest option is deprecated, no unit tests is the default setting"
print_usage
;;
-p|--path)
testdir=$2
shift
;;
*)
print_error_msg "Incorrect commandline provided, invalid key: $key"
print_usage
Expand All @@ -337,7 +354,15 @@ do
done

# home directory
homedir="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
currentdir="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ -e "$testdir" ]
then
homedir=$testdir
else
print_error_msg "Incorrect path: $testdir provided, run under $currentdir"
homedir=$currentdir
fi
echo "run tests under $homedir"
cd "$homedir"

# python path
Expand Down Expand Up @@ -457,9 +482,9 @@ then

if [ $doIsortFix = true ]
then
${cmdPrefix}${PY_EXE} -m isort "$(pwd)"
${cmdPrefix}${PY_EXE} -m isort "$homedir"
else
${cmdPrefix}${PY_EXE} -m isort --check "$(pwd)"
${cmdPrefix}${PY_EXE} -m isort --check "$homedir"
fi

isort_status=$?
Expand Down Expand Up @@ -493,9 +518,9 @@ then

if [ $doBlackFix = true ]
then
${cmdPrefix}${PY_EXE} -m black --skip-magic-trailing-comma "$(pwd)"
${cmdPrefix}${PY_EXE} -m black --skip-magic-trailing-comma "$homedir"
else
${cmdPrefix}${PY_EXE} -m black --skip-magic-trailing-comma --check "$(pwd)"
${cmdPrefix}${PY_EXE} -m black --skip-magic-trailing-comma --check "$homedir"
fi

black_status=$?
Expand All @@ -522,7 +547,7 @@ then
fi
${cmdPrefix}${PY_EXE} -m flake8 --version

${cmdPrefix}${PY_EXE} -m flake8 "$(pwd)" --count --statistics
${cmdPrefix}${PY_EXE} -m flake8 "$homedir" --count --statistics

flake8_status=$?
if [ ${flake8_status} -ne 0 ]
Expand Down Expand Up @@ -582,9 +607,9 @@ then

if [ $doRuffFix = true ]
then
ruff check --fix "$(pwd)"
ruff check --fix "$homedir"
else
ruff check "$(pwd)"
ruff check "$homedir"
fi

ruff_status=$?
Expand Down Expand Up @@ -615,7 +640,7 @@ then
else
${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}')")" "$(pwd)"
${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"

pytype_status=$?
if [ ${pytype_status} -ne 0 ]
Expand All @@ -641,7 +666,7 @@ then
install_deps
fi
${cmdPrefix}${PY_EXE} -m mypy --version
${cmdPrefix}${PY_EXE} -m mypy "$(pwd)"
${cmdPrefix}${PY_EXE} -m mypy "$homedir"

mypy_status=$?
if [ ${mypy_status} -ne 0 ]
Expand Down

0 comments on commit 5c6d199

Please sign in to comment.