run_tests.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/bin/bash
  2. set -eu
  3. function usage {
  4. echo "Usage: $0 [OPTION]..."
  5. echo "Run swifts3 test suite"
  6. echo ""
  7. echo " -V, --virtual-env Always use virtualenv. Install automatically if not present"
  8. echo " -N, --no-virtual-env Don't use virtualenv. Run tests in local environment"
  9. echo " -s, --no-site-packages Isolate the virtualenv from the global Python environment"
  10. echo " -x, --stop Stop running tests after the first error or failure."
  11. echo " -f, --force Force a clean re-build of the virtual environment. Useful when dependencies have been added."
  12. echo " -p, --pep8 Just run pep8"
  13. echo " -P, --no-pep8 Don't run pep8"
  14. echo " -c, --coverage Generate coverage report"
  15. echo " -h, --help Print this usage message"
  16. echo " --hide-elapsed Don't print the elapsed time for each test along with slow test list"
  17. echo ""
  18. echo "Note: with no options specified, the script will try to run the tests in a virtual environment,"
  19. echo " If no virtualenv is found, the script will ask if you would like to create one. If you "
  20. echo " prefer to run tests NOT in a virtual environment, simply pass the -N option."
  21. exit
  22. }
  23. function process_option {
  24. case "$1" in
  25. -h|--help) usage;;
  26. -V|--virtual-env) always_venv=1; never_venv=0;;
  27. -N|--no-virtual-env) always_venv=0; never_venv=1;;
  28. -s|--no-site-packages) no_site_packages=1;;
  29. -f|--force) force=1;;
  30. -p|--pep8) just_pep8=1;;
  31. -P|--no-pep8) no_pep8=1;;
  32. -c|--coverage) coverage=1;;
  33. -*) noseopts="$noseopts $1";;
  34. *) noseargs="$noseargs $1"
  35. esac
  36. }
  37. venv=.venv
  38. with_venv=tools/with_venv.sh
  39. always_venv=0
  40. never_venv=0
  41. force=0
  42. no_site_packages=0
  43. installvenvopts=
  44. noseargs=
  45. noseopts=
  46. wrapper=""
  47. just_pep8=0
  48. no_pep8=0
  49. coverage=0
  50. for arg in "$@"; do
  51. process_option $arg
  52. done
  53. # If enabled, tell nose to collect coverage data
  54. if [ $coverage -eq 1 ]; then
  55. noseopts="$noseopts --with-coverage --cover-package=swifts3"
  56. fi
  57. if [ $no_site_packages -eq 1 ]; then
  58. installvenvopts="--no-site-packages"
  59. fi
  60. function run_tests {
  61. # Just run the test suites in current environment
  62. ${wrapper} $NOSETESTS
  63. # If we get some short import error right away, print the error log directly
  64. RESULT=$?
  65. return $RESULT
  66. }
  67. function run_pep8 {
  68. echo "Running pep8 ..."
  69. srcfiles="swifts3 tests"
  70. # Just run PEP8 in current environment
  71. #
  72. # NOTE(sirp): W602 (deprecated 3-arg raise) is being ignored for the
  73. # following reasons:
  74. #
  75. # 1. It's needed to preserve traceback information when re-raising
  76. # exceptions; this is needed b/c Eventlet will clear exceptions when
  77. # switching contexts.
  78. #
  79. # 2. There doesn't appear to be an alternative, "pep8-tool" compatible way of doing this
  80. # in Python 2 (in Python 3 `with_traceback` could be used).
  81. #
  82. # 3. Can find no corroborating evidence that this is deprecated in Python 2
  83. # other than what the PEP8 tool claims. It is deprecated in Python 3, so,
  84. # perhaps the mistake was thinking that the deprecation applied to Python 2
  85. # as well.
  86. pep8_opts="--ignore=E202,W602 --repeat"
  87. ${wrapper} pep8 ${pep8_opts} ${srcfiles}
  88. }
  89. NOSETESTS="nosetests $noseopts $noseargs"
  90. if [ $never_venv -eq 0 ]
  91. then
  92. # Remove the virtual environment if --force used
  93. if [ $force -eq 1 ]; then
  94. echo "Cleaning virtualenv..."
  95. rm -rf ${venv}
  96. fi
  97. if [ -e ${venv} ]; then
  98. wrapper="${with_venv}"
  99. else
  100. if [ $always_venv -eq 1 ]; then
  101. # Automatically install the virtualenv
  102. python tools/install_venv.py $installvenvopts
  103. wrapper="${with_venv}"
  104. else
  105. echo -e "No virtual environment found...create one? (Y/n) \c"
  106. read use_ve
  107. if [ "x$use_ve" = "xY" -o "x$use_ve" = "x" -o "x$use_ve" = "xy" ]; then
  108. # Install the virtualenv and run the test suite in it
  109. python tools/install_venv.py $installvenvopts
  110. wrapper=${with_venv}
  111. fi
  112. fi
  113. fi
  114. fi
  115. # Delete old coverage data from previous runs
  116. if [ $coverage -eq 1 ]; then
  117. ${wrapper} coverage erase
  118. fi
  119. if [ $just_pep8 -eq 1 ]; then
  120. run_pep8
  121. exit
  122. fi
  123. run_tests
  124. # NOTE(sirp): we only want to run pep8 when we're running the full-test suite,
  125. # not when we're running tests individually. To handle this, we need to
  126. # distinguish between options (noseopts), which begin with a '-', and
  127. # arguments (noseargs).
  128. if [ -z "$noseargs" ]; then
  129. if [ $no_pep8 -eq 0 ]; then
  130. run_pep8
  131. fi
  132. fi
  133. if [ $coverage -eq 1 ]; then
  134. echo "Generating coverage report in covhtml/"
  135. ${wrapper} coverage html -d covhtml -i
  136. fi