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