pre-commit 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/sh
  2. # get the list of modified files
  3. files=$(git diff --cached --name-only)
  4. # check if api or web directory is modified
  5. api_modified=false
  6. web_modified=false
  7. for file in $files
  8. do
  9. # Use POSIX compliant pattern matching
  10. case "$file" in
  11. api/*.py)
  12. # set api_modified flag to true
  13. api_modified=true
  14. ;;
  15. web/*)
  16. # set web_modified flag to true
  17. web_modified=true
  18. ;;
  19. esac
  20. done
  21. # run linters based on the modified modules
  22. if $api_modified; then
  23. echo "Running Ruff linter on api module"
  24. # python style checks rely on `ruff` in path
  25. if ! command -v ruff > /dev/null 2>&1; then
  26. echo "Installing linting tools (Ruff, dotenv-linter ...) ..."
  27. poetry install -C api --only lint
  28. fi
  29. # run Ruff linter auto-fixing
  30. ruff check --fix ./api
  31. # run Ruff linter checks
  32. ruff check ./api || status=$?
  33. status=${status:-0}
  34. if [ $status -ne 0 ]; then
  35. echo "Ruff linter on api module error, exit code: $status"
  36. echo "Please run 'dev/reformat' to fix the fixable linting errors."
  37. exit 1
  38. fi
  39. fi
  40. if $web_modified; then
  41. echo "Running ESLint on web module"
  42. cd ./web || exit 1
  43. lint-staged
  44. echo "Running unit tests check"
  45. modified_files=$(git diff --cached --name-only -- utils | grep -v '\.spec\.ts$' || true)
  46. if [ -n "$modified_files" ]; then
  47. for file in $modified_files; do
  48. test_file="${file%.*}.spec.ts"
  49. echo "Checking for test file: $test_file"
  50. # check if the test file exists
  51. if [ -f "../$test_file" ]; then
  52. echo "Detected changes in $file, running corresponding unit tests..."
  53. pnpm run test "../$test_file"
  54. if [ $? -ne 0 ]; then
  55. echo "Unit tests failed. Please fix the errors before committing."
  56. exit 1
  57. fi
  58. echo "Unit tests for $file passed."
  59. else
  60. echo "Warning: $file does not have a corresponding test file."
  61. fi
  62. done
  63. echo "All unit tests for modified web/utils files have passed."
  64. fi
  65. cd ../
  66. fi