Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code formatting for user specified directories #7106

Merged
merged 8 commits into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
wyli marked this conversation as resolved.
Show resolved Hide resolved
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