run.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/sh
  2. # basedir is the root of the test directory in the package
  3. basedir="$(dirname "$0")"
  4. [ "$(echo "$basedir" | cut -c1)" = '/' ] || basedir="$PWD/$basedir"
  5. # rootdir is the root of the package
  6. rootdir="$basedir/.."
  7. die()
  8. {
  9. [ -n "$*" ] && echo "$*" >&2
  10. exit 1
  11. }
  12. # $1=interpreter
  13. # $2=test_dir
  14. run_pyunit()
  15. {
  16. local interpreter="$1"
  17. local test_dir="$2"
  18. (
  19. echo
  20. echo "==="
  21. echo "= Running $interpreter..."
  22. echo "==="
  23. export PYTHONPATH="$rootdir/tests:$PYTHONPATH"
  24. cd "$rootdir" || die "Failed to cd to rootdir."
  25. "$interpreter" -m unittest --failfast --buffer --catch "$test_dir" ||\
  26. die "Test failed"
  27. ) || die
  28. }
  29. test_interpreter()
  30. {
  31. "$1" -c 'import serial' >/dev/null 2>&1
  32. }
  33. # $1=test_dir
  34. run_testdir()
  35. {
  36. local test_dir="$1"
  37. unset PYTHONPATH
  38. unset PYTHONSTARTUP
  39. unset PYTHONY2K
  40. unset PYTHONOPTIMIZE
  41. unset PYTHONDEBUG
  42. export PYTHONDONTWRITEBYTECODE=1
  43. unset PYTHONINSPECT
  44. unset PYTHONIOENCODING
  45. unset PYTHONNOUSERSITE
  46. unset PYTHONUNBUFFERED
  47. unset PYTHONVERBOSE
  48. export PYTHONWARNINGS=once
  49. export PYTHONHASHSEED=random
  50. local exec_any=0
  51. if test_interpreter python2; then
  52. run_pyunit python2 "$test_dir"
  53. local exec_any=1
  54. fi
  55. if test_interpreter python3; then
  56. run_pyunit python3 "$test_dir"
  57. local exec_any=1
  58. fi
  59. if test_interpreter pypy3; then
  60. run_pyunit pypy3 "$test_dir"
  61. local exec_any=1
  62. fi
  63. if [ $exec_any -eq 0 ]; then
  64. die "Failed to find any usable Python interpreter."
  65. fi
  66. }
  67. run_tests()
  68. {
  69. run_testdir tests
  70. }
  71. run_tests