|
| 1 | +# Copyright 2016 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Script to run unit tests for the entire project. |
| 16 | +
|
| 17 | +This script orchestrates stepping into each sub-package and |
| 18 | +running tests. It also allows running a limited subset of tests |
| 19 | +in cases where such a limited subset can be identified. |
| 20 | +""" |
| 21 | + |
| 22 | +from __future__ import print_function |
| 23 | + |
| 24 | +import argparse |
| 25 | +import os |
| 26 | +import subprocess |
| 27 | +import sys |
| 28 | + |
| 29 | + |
| 30 | +PROJECT_ROOT = os.path.abspath( |
| 31 | + os.path.join(os.path.dirname(__file__), '..')) |
| 32 | +IGNORED_DIRECTORIES = ( |
| 33 | + 'appveyor', |
| 34 | + 'docs', |
| 35 | + 'scripts', |
| 36 | + 'system_tests', |
| 37 | +) |
| 38 | + |
| 39 | + |
| 40 | +def check_output(*args): |
| 41 | + """Run a command on the operation system. |
| 42 | +
|
| 43 | + :type args: tuple |
| 44 | + :param args: Keyword arguments to pass to ``subprocess.check_output``. |
| 45 | +
|
| 46 | + :rtype: str |
| 47 | + :returns: The raw STDOUT from the command (converted from bytes |
| 48 | + if necessary). |
| 49 | + """ |
| 50 | + cmd_output = subprocess.check_output(args) |
| 51 | + # On Python 3, this returns bytes (from STDOUT), so we |
| 52 | + # convert to a string. |
| 53 | + cmd_output = cmd_output.decode('utf-8') |
| 54 | + # Also strip the output since it usually has a trailing newline. |
| 55 | + return cmd_output.strip() |
| 56 | + |
| 57 | + |
| 58 | +def get_package_directories(): |
| 59 | + """Get a list of directories containing sub-packages. |
| 60 | +
|
| 61 | + :rtype: list |
| 62 | + :returns: A list of all sub-package directories. |
| 63 | + """ |
| 64 | + # Run ls-tree with |
| 65 | + # -d: For directories only |
| 66 | + # --name-only: No extra info like the type/hash/permission bits. |
| 67 | + # --full-name: Give path relative to root, rather than cwd. |
| 68 | + ls_tree_out = check_output( |
| 69 | + 'git', 'ls-tree', |
| 70 | + '-d', '--name-only', '--full-name', |
| 71 | + 'HEAD', PROJECT_ROOT) |
| 72 | + result = [] |
| 73 | + for package in ls_tree_out.split('\n'): |
| 74 | + if package not in IGNORED_DIRECTORIES: |
| 75 | + result.append(package) |
| 76 | + return result |
| 77 | + |
| 78 | + |
| 79 | +def run_package(package, tox_env): |
| 80 | + """Run tox environment for a given package. |
| 81 | +
|
| 82 | + :type package: str |
| 83 | + :param package: The name of the subdirectory which holds the sub-package. |
| 84 | + This will be a path relative to ``PROJECT_ROOT``. |
| 85 | +
|
| 86 | + :type tox_env: str |
| 87 | + :param tox_env: The ``tox`` environment(s) to run in each sub-package. |
| 88 | +
|
| 89 | + :rtype: bool |
| 90 | + :returns: Flag indicating if the test run succeeded. |
| 91 | + """ |
| 92 | + curr_dir = os.getcwd() |
| 93 | + package_dir = os.path.join(PROJECT_ROOT, package) |
| 94 | + try: |
| 95 | + os.chdir(package_dir) |
| 96 | + return_code = subprocess.call(['tox', '-e', tox_env]) |
| 97 | + return return_code == 0 |
| 98 | + finally: |
| 99 | + os.chdir(curr_dir) |
| 100 | + |
| 101 | + |
| 102 | +def get_parser(): |
| 103 | + """Get simple ``argparse`` parser to determine configuration. |
| 104 | +
|
| 105 | + :rtype: :class:`argparse.ArgumentParser` |
| 106 | + :returns: The parser for this script. |
| 107 | + """ |
| 108 | + description = 'Run tox environment(s) in all sub-packages.' |
| 109 | + parser = argparse.ArgumentParser(description=description) |
| 110 | + parser.add_argument( |
| 111 | + '--tox-env', dest='tox_env', default='py27', |
| 112 | + help='The tox environment(s) to run in sub-packages.') |
| 113 | + return parser |
| 114 | + |
| 115 | + |
| 116 | +def get_tox_env(): |
| 117 | + """Get the environment to be used with ``tox``. |
| 118 | +
|
| 119 | + :rtype: str |
| 120 | + :returns: The current ``tox`` environment to be used, e.g. ``"py27"``. |
| 121 | + """ |
| 122 | + parser = get_parser() |
| 123 | + args = parser.parse_args() |
| 124 | + return args.tox_env |
| 125 | + |
| 126 | + |
| 127 | +def main(): |
| 128 | + """Run all the unit tests that need to be run.""" |
| 129 | + packages_to_run = get_package_directories() |
| 130 | + if not packages_to_run: |
| 131 | + print('No tests to run.') |
| 132 | + return |
| 133 | + |
| 134 | + tox_env = get_tox_env() |
| 135 | + failed_packages = [] |
| 136 | + for package in packages_to_run: |
| 137 | + succeeded = run_package(package, tox_env) |
| 138 | + if not succeeded: |
| 139 | + failed_packages.append(package) |
| 140 | + |
| 141 | + if failed_packages: |
| 142 | + msg_parts = ['Sub-packages failed:'] |
| 143 | + for package in failed_packages: |
| 144 | + msg_parts.append('- ' + package) |
| 145 | + msg = '\n'.join(msg_parts) |
| 146 | + print(msg, file=sys.stderr) |
| 147 | + sys.exit(len(failed_packages)) |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == '__main__': |
| 151 | + main() |
0 commit comments