Dan Albert | 69ab970 | 2016-03-01 17:50:19 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2016 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | """Runs all tests for gtest/gmock.""" |
| 18 | import argparse |
| 19 | import logging |
| 20 | import os |
| 21 | import sys |
| 22 | |
| 23 | |
| 24 | # pylint: disable=design |
| 25 | |
| 26 | |
| 27 | def logger(): |
| 28 | """Return the default logger for the module.""" |
| 29 | return logging.getLogger(__name__) |
| 30 | |
| 31 | |
| 32 | def call(cmd, *args, **kwargs): |
| 33 | """Proxy for subprocess.call with logging.""" |
| 34 | import subprocess |
| 35 | logger().info('call `%s`', ' '.join(cmd)) |
| 36 | return subprocess.call(cmd, *args, **kwargs) |
| 37 | |
| 38 | |
| 39 | def parse_args(): |
| 40 | "Parse and return command line arguments.""" |
| 41 | parser = argparse.ArgumentParser() |
| 42 | parser.add_argument('--host', action='store_true') |
| 43 | parser.add_argument('-v', '--verbose', action='store_true') |
| 44 | return parser.parse_args() |
| 45 | |
| 46 | |
| 47 | def main(): |
| 48 | "Program entry point.""" |
| 49 | args = parse_args() |
| 50 | log_level = logging.INFO |
| 51 | if args.verbose: |
| 52 | log_level = logging.DEBUG |
| 53 | logging.basicConfig(level=log_level) |
| 54 | |
| 55 | if args.host: |
Dan Willemsen | 6c55922 | 2019-05-10 17:17:17 -0700 | [diff] [blame] | 56 | test_location = os.path.join(os.environ['ANDROID_HOST_OUT'], 'nativetest64') |
Dan Albert | 69ab970 | 2016-03-01 17:50:19 -0800 | [diff] [blame] | 57 | else: |
| 58 | data_dir = os.path.join(os.environ['OUT'], 'data') |
| 59 | test_location = os.path.join(data_dir, 'nativetest64') |
| 60 | if not os.path.exists(test_location): |
| 61 | test_location = os.path.join(data_dir, 'nativetest') |
| 62 | |
| 63 | num_tests = 0 |
| 64 | failures = [] |
Dan Willemsen | 6c55922 | 2019-05-10 17:17:17 -0700 | [diff] [blame] | 65 | for test_dir in ['gtest_tests', 'gtest_ndk_tests', 'gmock_tests']: |
| 66 | test_dir = os.path.join(test_location, test_dir) |
| 67 | if not os.path.isdir(test_dir): |
| 68 | logger().debug('Skipping %s', test_dir) |
Dan Albert | 69ab970 | 2016-03-01 17:50:19 -0800 | [diff] [blame] | 69 | continue |
Dan Albert | 69ab970 | 2016-03-01 17:50:19 -0800 | [diff] [blame] | 70 | |
Dan Willemsen | 6c55922 | 2019-05-10 17:17:17 -0700 | [diff] [blame] | 71 | logger().debug('Scanning %s for tests', test_dir) |
| 72 | for test in os.listdir(test_dir): |
| 73 | if not test.startswith('gtest') and not test.startswith('gmock'): |
| 74 | logger().debug('Skipping %s', test) |
| 75 | continue |
| 76 | num_tests += 1 |
| 77 | |
| 78 | if args.host: |
| 79 | cmd = [os.path.join(test_dir, test)] |
| 80 | if call(cmd) != 0: |
| 81 | failures.append(test) |
| 82 | else: |
| 83 | device_dir = test_dir.replace(os.environ['OUT'], '') |
| 84 | cmd = ['adb', 'shell', 'cd {} && ./{}'.format(device_dir, test)] |
| 85 | if call(cmd) != 0: |
| 86 | failures.append(test) |
Dan Albert | 69ab970 | 2016-03-01 17:50:19 -0800 | [diff] [blame] | 87 | |
| 88 | if num_tests == 0: |
| 89 | logger().error('No tests found!') |
| 90 | sys.exit(1) |
| 91 | |
| 92 | num_failures = len(failures) |
| 93 | num_passes = num_tests - num_failures |
| 94 | logger().info('%d/%d tests passed', num_passes, num_tests) |
| 95 | if len(failures) > 0: |
| 96 | logger().error('Failures:\n%s', '\n'.join(failures)) |
| 97 | else: |
| 98 | logger().info('All tests passed!') |
| 99 | sys.exit(num_failures) |
| 100 | |
| 101 | |
| 102 | if __name__ == '__main__': |
| 103 | main() |