test_backend.sh 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/sh
  2. cd backend
  3. ruff check && ruff format --check
  4. # Type check against the pinned environment (see CONTRIBUTING.md). The venv is
  5. # rebuilt whenever the lock changes, so it holds what CI installs (on Python
  6. # 3.11 exactly; newer Pythons add a few unpinned backports).
  7. if [ -d ../.typecheck-venv ]; then
  8. (
  9. cd .. || exit 1
  10. lock_hash=$( (sha256sum requirements-typecheck.lock 2>/dev/null || shasum -a 256 requirements-typecheck.lock) | cut -d' ' -f1)
  11. if [ -z "$lock_hash" ]; then
  12. echo "Type check: neither sha256sum nor shasum is available."
  13. exit 1
  14. fi
  15. if ! .typecheck-venv/bin/python -c '' 2>/dev/null; then
  16. echo "Type check: the python in .typecheck-venv no longer runs (interpreter removed or upgraded); delete .typecheck-venv and recreate it (see CONTRIBUTING.md)."
  17. exit 1
  18. fi
  19. if [ ! -x .typecheck-venv/bin/basedpyright ] || [ "$(cat .typecheck-venv/.lock-hash 2>/dev/null)" != "$lock_hash" ]; then
  20. echo "Rebuilding .typecheck-venv from requirements-typecheck.lock"
  21. # Rebuild from the interpreter the venv was created from (pyvenv.cfg
  22. # "home"), not a fully resolved path, so a stable link stays stable.
  23. py="$(sed -n 's/^home *= *//p' .typecheck-venv/pyvenv.cfg)/python3"
  24. "$py" -m venv --clear .typecheck-venv || exit 1
  25. .typecheck-venv/bin/pip install -q -r requirements-typecheck.lock || exit 1
  26. echo "$lock_hash" > .typecheck-venv/.lock-hash
  27. fi
  28. if ! out=$(.typecheck-venv/bin/pip install --dry-run -r requirements.txt 2>&1); then
  29. echo "$out"
  30. echo "pip could not resolve requirements.txt against .typecheck-venv."
  31. exit 1
  32. fi
  33. if echo "$out" | grep '^Would install'; then
  34. echo "requirements-typecheck.lock is missing packages from requirements.txt; regenerate it (see requirements-typecheck.txt)."
  35. exit 1
  36. fi
  37. .typecheck-venv/bin/basedpyright --pythonpath .typecheck-venv/bin/python
  38. ) || exit 1
  39. else
  40. echo "Skipping the type check: no .typecheck-venv (see CONTRIBUTING.md)."
  41. fi
  42. # PostgreSQL upgrade check, as CI runs it, in a throwaway container (the tests
  43. # below run on SQLite only). Needs Docker and the release tags.
  44. if ! command -v docker >/dev/null 2>&1 || ! docker info >/dev/null 2>&1; then
  45. echo "Skipping the PostgreSQL upgrade check: Docker is not available."
  46. elif [ -z "$(git tag --list v0.2.3)" ]; then
  47. echo "Skipping the PostgreSQL upgrade check: no release tags in this clone (git fetch --tags)."
  48. else
  49. (
  50. cd .. || exit 1
  51. name="bambuddy-pg-check-$$"
  52. port=$(venv/bin/python -c 'import socket; s = socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1])') || exit 1
  53. docker run -d --rm --name "$name" --tmpfs /var/lib/postgresql/data -e POSTGRES_PASSWORD=check \
  54. -p "127.0.0.1:$port:5432" postgres:16-alpine >/dev/null || exit 1
  55. # dash runs no EXIT trap when interrupted, so remove the container on a
  56. # signal too (and then exit as interrupted).
  57. trap 'docker rm -f "$name" >/dev/null 2>&1' EXIT
  58. trap 'docker rm -f "$name" >/dev/null 2>&1; exit 130' INT TERM
  59. ready=0
  60. for _ in $(seq 60); do
  61. if docker exec "$name" pg_isready -U postgres -h 127.0.0.1 >/dev/null 2>&1; then ready=1; break; fi
  62. sleep 1
  63. done
  64. if [ "$ready" != 1 ]; then
  65. echo "PostgreSQL upgrade check: the container did not become ready."
  66. exit 1
  67. fi
  68. venv/bin/python scripts/check_postgres_upgrade.py --url "postgresql://postgres:check@127.0.0.1:$port/postgres"
  69. ) || exit 1
  70. fi
  71. if [ "$1" = "--full" ]; then
  72. ../venv/bin/python3 -m pytest tests/ -v -n 30
  73. else
  74. ../venv/bin/python3 -m pytest tests/ -v -n 30 --ignore=tests/unit/services/test_bambu_ftp.py
  75. fi