169ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert#!/usr/bin/env python 269ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# 369ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# Copyright (C) 2016 The Android Open Source Project 469ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# 569ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# Licensed under the Apache License, Version 2.0 (the "License"); 669ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# you may not use this file except in compliance with the License. 769ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# You may obtain a copy of the License at 869ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# 969ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# http://www.apache.org/licenses/LICENSE-2.0 1069ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# 1169ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# Unless required by applicable law or agreed to in writing, software 1269ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# distributed under the License is distributed on an "AS IS" BASIS, 1369ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1469ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# See the License for the specific language governing permissions and 1569ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# limitations under the License. 1669ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# 1769ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert"""Runs all tests for gtest/gmock.""" 1869ab970c6946798c04e4bf4bbd2311e08a69051fDan Albertimport argparse 1969ab970c6946798c04e4bf4bbd2311e08a69051fDan Albertimport logging 2069ab970c6946798c04e4bf4bbd2311e08a69051fDan Albertimport os 2169ab970c6946798c04e4bf4bbd2311e08a69051fDan Albertimport sys 2269ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 2369ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 2469ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert# pylint: disable=design 2569ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 2669ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 2769ab970c6946798c04e4bf4bbd2311e08a69051fDan Albertdef logger(): 2869ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert """Return the default logger for the module.""" 2969ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert return logging.getLogger(__name__) 3069ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 3169ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 3269ab970c6946798c04e4bf4bbd2311e08a69051fDan Albertdef call(cmd, *args, **kwargs): 3369ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert """Proxy for subprocess.call with logging.""" 3469ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert import subprocess 3569ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert logger().info('call `%s`', ' '.join(cmd)) 3669ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert return subprocess.call(cmd, *args, **kwargs) 3769ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 3869ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 3969ab970c6946798c04e4bf4bbd2311e08a69051fDan Albertdef parse_args(): 4069ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert "Parse and return command line arguments.""" 4169ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert parser = argparse.ArgumentParser() 4269ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert parser.add_argument('--host', action='store_true') 4369ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert parser.add_argument('-v', '--verbose', action='store_true') 4469ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert return parser.parse_args() 4569ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 4669ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 4769ab970c6946798c04e4bf4bbd2311e08a69051fDan Albertdef main(): 4869ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert "Program entry point.""" 4969ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert args = parse_args() 5069ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert log_level = logging.INFO 5169ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert if args.verbose: 5269ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert log_level = logging.DEBUG 5369ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert logging.basicConfig(level=log_level) 5469ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 5569ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert if args.host: 5669ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert test_location = os.path.join(os.environ['ANDROID_HOST_OUT'], 'bin') 5769ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert else: 5869ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert data_dir = os.path.join(os.environ['OUT'], 'data') 5969ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert test_location = os.path.join(data_dir, 'nativetest64') 6069ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert if not os.path.exists(test_location): 6169ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert test_location = os.path.join(data_dir, 'nativetest') 6269ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 6369ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert num_tests = 0 6469ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert failures = [] 6569ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert logger().debug('Scanning %s for tests', test_location) 6669ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert for test in os.listdir(test_location): 6769ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert if not test.startswith('gtest') and not test.startswith('gmock'): 6869ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert logger().debug('Skipping %s', test) 6969ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert continue 7069ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert num_tests += 1 7169ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 7269ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert if args.host: 7369ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert cmd = [os.path.join(test_location, test)] 7469ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert if call(cmd) != 0: 7569ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert failures.append(test) 7669ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert else: 7769ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert device_dir = test_location.replace(os.environ['OUT'], '') 7869ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert cmd = ['adb', 'shell', 'cd {} && ./{}'.format(device_dir, test)] 7969ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert if call(cmd) != 0: 8069ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert failures.append(test) 8169ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 8269ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert if num_tests == 0: 8369ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert logger().error('No tests found!') 8469ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert sys.exit(1) 8569ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 8669ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert num_failures = len(failures) 8769ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert num_passes = num_tests - num_failures 8869ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert logger().info('%d/%d tests passed', num_passes, num_tests) 8969ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert if len(failures) > 0: 9069ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert logger().error('Failures:\n%s', '\n'.join(failures)) 9169ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert else: 9269ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert logger().info('All tests passed!') 9369ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert sys.exit(num_failures) 9469ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 9569ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert 9669ab970c6946798c04e4bf4bbd2311e08a69051fDan Albertif __name__ == '__main__': 9769ab970c6946798c04e4bf4bbd2311e08a69051fDan Albert main() 98