generated from hackforla/.github-hackforla-base-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide options to speed up test script (#415)
* Add pytest-xdist for multi CPU parallel testing * Update instructions * Fix createsuperuser.sh * Add shell.sh, remove unneeded scripts
- Loading branch information
1 parent
b62f2ea
commit db91e3e
Showing
8 changed files
with
166 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ markdown | |
psycopg2-binary | ||
pytest-cov | ||
pytest-django | ||
pytest-xdist | ||
tzdata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/bin/bash | ||
|
||
# Note about -e: Intentionally not "set -e..." so that if there is a syntax error, | ||
# the shell will not close. Unless you are joining commands with || | ||
# | ||
# set -o pipefail will catch any failing command unless commands joined by || | ||
# -u : errors if variable is not set | ||
# -o pipefail : script terminates if any command fails | ||
echo starting | ||
|
||
# Handle errors gracefully without exiting the shell session. | ||
|
||
|
||
# Main function to contain the logic. | ||
main() { | ||
|
||
echo In main | ||
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then | ||
echo "The script was sourced." | ||
else | ||
echo "Error: the script must be sourced." | ||
return 1 | ||
fi | ||
ORIGINAL_DIR=$PWD | ||
|
||
if [ -f "path.sh" ]; then | ||
echo path.sh is in current directory | ||
elif [ -f "../scripts/path.sh" ]; then | ||
echo "cd ../scripts" | ||
cd ../scripts || return 1 | ||
elif [ -f "scripts/path.sh" ]; then | ||
echo "cd scripts" | ||
cd scripts || return 1 | ||
elif [ ! -f "path.sh" ]; then | ||
echo "Could not find path.sh relative to the current directory." | ||
return 1 | ||
fi | ||
|
||
echo Checking path | ||
|
||
if [[ "$PATH" = *"$PWD"* ]]; then | ||
echo Path is already set | ||
else | ||
echo "Adding $PWD to PATH" | ||
export PATH="$PATH:$PWD" | ||
fi | ||
|
||
echo "cd $ORIGINAL_DIR" | ||
cd "$ORIGINAL_DIR" || return 1 | ||
|
||
echo "Script completed successfully." | ||
} | ||
|
||
# Run the main function. | ||
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
|
||
echo "\q to quit" | ||
|
||
set -x | ||
docker-compose exec web run /bin/sh -e .env.docker |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,86 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -x | ||
# Default options | ||
COVERAGE="--no-cov" | ||
EXEC_COMMAND=true | ||
CHECK_MIGRATIONS=true | ||
N_CPU="auto" | ||
PYTEST_ARGS=("") | ||
|
||
# check for missing migration files | ||
# https://adamj.eu/tech/2024/06/23/django-test-pending-migrations/ | ||
docker-compose exec -T web python manage.py makemigrations --check | ||
# Function to display help | ||
show_help() { | ||
cat << EOF | ||
Usage: ${0##*/} [OPTIONS] [pytest-args] | ||
# run tests and show code coverage | ||
# filter tests using -k <filter> | ||
# ex: test.sh -k program_area --no-cov | ||
docker-compose exec -T web pytest "$@" | ||
Options: | ||
--coverage Run tests with coverage (default: without coverage, using --no-cov). | ||
--skip-migrations Skip checking for pending migrations before running tests (default: check migrations). | ||
-n Remove the default --nauto option for running tests (default: -n auto). There must be | ||
a space after -n and the value. (If you use 1 the script changes the value to 0.) | ||
--help Display this help message and exit. | ||
--help-pytest Display pytest help. | ||
Other parameters passed to the script will be forwarded to pytest as specified. | ||
By default: | ||
- Tests run without coverage. | ||
- Migrations are checked before running tests. | ||
- Tests are run using --n auto for optimal parallel execution. | ||
EOF | ||
} | ||
|
||
# Parse arguments | ||
while [[ $# -gt 0 ]]; do | ||
arg="$1" # Use $1 as the current argument | ||
case $arg in | ||
--help) | ||
show_help | ||
exit 0 | ||
;; | ||
--help-pytest) | ||
pytest --help | ||
exit 0 | ||
;; | ||
--no-exec) | ||
EXEC_COMMAND=false | ||
;; | ||
--coverage) | ||
COVERAGE="" # Enable coverage | ||
echo "Coverage enabled" | ||
;; | ||
--skip-migrations) | ||
CHECK_MIGRATIONS=false # Skip migration checks | ||
echo "Skipping migration checks" | ||
;; | ||
-n) | ||
shift | ||
N_CPU="$1" | ||
if [ "$N_CPU" == "1" ]; then | ||
N_CPU=0 | ||
fi | ||
;; | ||
*) | ||
PYTEST_ARGS+=("$arg") # Preserve other arguments for pytest | ||
echo "Positional argument added: $arg" | ||
echo "Current python args: ${PYTEST_ARGS[*]}" | ||
;; | ||
esac | ||
shift # Shift to the next argument | ||
done | ||
|
||
# Check for missing migration files if not skipped | ||
if [ "$CHECK_MIGRATIONS" = true ]; then | ||
echo "Checking for missing migrations..." | ||
set -x | ||
docker-compose exec -T web python manage.py makemigrations --check | ||
set +x | ||
fi | ||
|
||
if [ "$EXEC_COMMAND" = true ]; then | ||
set -x | ||
docker-compose exec -T web pytest -n "$N_CPU" $COVERAGE "${PYTEST_ARGS[@]}" | ||
set +x | ||
else | ||
echo docker-compose exec -T web pytest -n "$N_CPU" $COVERAGE "${PYTEST_ARGS[@]}" | ||
fi |