Skip to content

Commit

Permalink
Abstracted cleanup_before_exit into shared functions
Browse files Browse the repository at this point in the history
Added venv activation and deactivation around the django tests
added missing logic to append django errors to the total error count
  • Loading branch information
christophertubbs authored Sep 19, 2023
1 parent 66338dd commit 444355d
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 113 deletions.
18 changes: 0 additions & 18 deletions scripts/code_style.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,6 @@ run_autofix()
"${1:-.}"
}

# Make sure we end up in the same starting directory, and deactivate venv if it was activated
cleanup_before_exit()
{
# Make sure we don't accidentally run this more than once
CLEANUP_DONE=$((${CLEANUP_DONE:=0}+1))
if [ ${CLEANUP_DONE} -gt 1 ]; then
>&2 echo "Warning: cleanup function being run for ${CLEANUP_DONE} time"
fi
# Go back to shell starting dir
cd "${STARTING_DIR:?}"

# If the flag is set that a virtual environment was activated, then deactivate it
if [ ${VENV_WAS_ACTIVATED:-1} -eq 0 ]; then
>&2 echo "Deactiving active virtual env at ${VIRTUAL_ENV}"
deactivate
fi
}

# Options args loop
while [[ ${#} -gt 0 ]]; do
case "${1}" in
Expand Down
18 changes: 0 additions & 18 deletions scripts/dist_package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,6 @@ Options:
echo "${_O}" 2>&1
}

# Make sure we end up in the same starting directory, and deactivate venv if it was activated
cleanup_before_exit()
{
# Make sure we don't accidentally run this more than once
CLEANUP_DONE=$((${CLEANUP_DONE:=0}+1))
if [ ${CLEANUP_DONE} -gt 1 ]; then
>&2 echo "Warning: cleanup function being run for ${CLEANUP_DONE} time"
fi
# Go back to shell starting dir
cd "${STARTING_DIR:?}"

# If the flag is set that a virtual environment was activated, then deactivate it
if [ ${VENV_WAS_ACTIVATED:-1} -eq 0 ]; then
>&2 echo "Deactiving active virtual env at ${VIRTUAL_ENV}"
deactivate
fi
}

while [ ${#} -gt 0 ]; do
case "${1}" in
-h|--help|-help)
Expand Down
8 changes: 8 additions & 0 deletions scripts/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,22 @@ done

if [ -n "${TEST_DJANGO_SERVICES:-}" ]; then
if [ -n "${DO_QUIET:-}" ]; then
py_dev_activate_venv
echo "Running django tests in quiet mode"
${DJANGO_TEST_SCRIPT_PATH} --quiet
DJANGO_ERROR_COUNT=$?
elif [ -z "${SET_VERBOSE:-}" ]; then
py_dev_activate_venv
${DJANGO_TEST_SCRIPT_PATH}
DJANGO_ERROR_COUNT=$?
else
echo "-----------------------------------"
echo ""
echo "Django Tests:"
echo ""
py_dev_activate_venv
VERBOSE_DJANGO_OUTPUT=$("${DJANGO_TEST_SCRIPT_PATH}" --verbose)
DJANGO_ERROR_COUNT=$?
echo "$VERBOSE_DJANGO_OUTPUT"

# Extract the lines containing the summary to print later
Expand All @@ -297,6 +303,8 @@ if [ -n "${TEST_DJANGO_SERVICES:-}" ]; then
echo ""
echo ""
fi
TOTAL_RESULT=$((TOTAL_RESULT+$DJANGO_ERROR_COUNT))
py_dev_deactivate_venv
fi


Expand Down
4 changes: 2 additions & 2 deletions scripts/secrets/setup_git_secrets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ OPTIONS:
echo "${_INFO_OUT}" | less
}

cleanup_before_exit()
cleanup_repo()
{
# Only do this deletion if both the flag is set and we have established at some point it is safe to do so
if [ ${SAFE_REMOVE_REPO:-1} -eq 0 ] && [ -n "${DO_REMOVE_REPO:-}" ]; then
Expand Down Expand Up @@ -292,7 +292,7 @@ while [ ${#} -gt 0 ]; do
done

# Trap to make sure we "clean up" script activity before exiting
trap cleanup_before_exit 0 1 2 3 6 15
trap cleanup_repo 0 1 2 3 6 15

# Make sure a repo directory was set
if [ -z "${REPO_DIR:-}" ]; then
Expand Down
28 changes: 28 additions & 0 deletions scripts/shared/py_dev_func.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,32 @@ py_dev_activate_venv()
else
VENV_WAS_ACTIVATED=1
fi
}

# If appropriate, deactivate the virtual env set within VENV_DIR, and note whether this is done in VENV_WAS_ACTIVATED.
py_dev_deactivate_venv()
{
# If the flag is set that a virtual environment was activated, then deactivate it
if [ ${VENV_WAS_ACTIVATED:-1} -eq 0 ]; then
>&2 echo "Deactiving active virtual env at ${VIRTUAL_ENV}"
>&2 echo ""
deactivate
VENV_WAS_ACTIVATED=1
fi
}


# Make sure we end up in the same starting directory, and deactivate venv if it was activated
cleanup_before_exit()
{
# Make sure we don't accidentally run this more than once
CLEANUP_DONE=$((${CLEANUP_DONE:=0}+1))
if [ ${CLEANUP_DONE} -gt 1 ]; then
>&2 echo "Warning: cleanup function being run for ${CLEANUP_DONE} time"
fi

# Go back to shell starting dir
cd "${STARTING_DIR:?}"

py_dev_deactivate_venv
}
Loading

0 comments on commit 444355d

Please sign in to comment.