1#!/usr/bin/env python
2#
3# Copyright 2019 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6"""
7Run unit tests on a pinned version of chrome.
8"""
9
10import os
11import platform
12import re
13from subprocess import Popen
14import sys
15import signal
16import argparse
17
18scripts_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
19sys.path.append(scripts_path)
20
21import test_helpers
22import devtools_paths
23
24
25def run_tests(chrome_binary, target, no_text_coverage, coverage):
26    cwd = devtools_paths.devtools_root_path()
27    karmaconfig_path = os.path.join(cwd, 'out', target, 'gen', 'test',
28                                    'unittests', 'karma.conf.js')
29
30    if not os.path.exists(karmaconfig_path):
31        print('Unable to find Karma config at ' + karmaconfig_path)
32        print('Make sure to set the --ninja-build-name argument to the folder name of "out/target"')
33        sys.exit(1)
34
35    print('Using karma config ' + karmaconfig_path)
36
37    exec_command = [devtools_paths.node_path(), devtools_paths.karma_path(), 'start', test_helpers.to_platform_path_exact(karmaconfig_path)]
38
39    env = os.environ.copy()
40    env['NODE_PATH'] = devtools_paths.node_path()
41    if (no_text_coverage is not False):
42        env['NO_TEXT_COVERAGE'] = '1'
43    if (coverage is True):
44        env['COVERAGE'] = '1'
45    if (chrome_binary is not None):
46        env['CHROME_BIN'] = chrome_binary
47    exit_code = test_helpers.popen(exec_command, cwd=cwd, env=env)
48    if exit_code == 1:
49        return True
50
51    return False
52
53
54def run_unit_tests_on_ninja_build_target(target,
55                                         no_text_coverage=True,
56                                         coverage=False,
57                                         chrome_binary=None):
58    if chrome_binary and not test_helpers.check_chrome_binary(chrome_binary):
59        print(
60            'Chrome binary argument path does not exist or is not executable, reverting to downloaded binary'
61        )
62        chrome_binary = None
63
64    if not chrome_binary:
65        # Default to the downloaded / pinned Chromium binary
66        downloaded_chrome_binary = devtools_paths.downloaded_chrome_binary_path(
67        )
68        if test_helpers.check_chrome_binary(downloaded_chrome_binary):
69            chrome_binary = downloaded_chrome_binary
70
71    if (chrome_binary is None):
72        print('Unable to run, no Chrome binary provided')
73        sys.exit(1)
74
75    print('Using Chromium binary (%s)\n' % chrome_binary)
76
77    errors_found = run_tests(chrome_binary, target, no_text_coverage, coverage)
78    if errors_found:
79        print('ERRORS DETECTED')
80        sys.exit(1)
81
82
83def main():
84    parser = argparse.ArgumentParser(description='Run unittests on Ninja targets.')
85    parser.add_argument(
86        '--target', '-t', default='Default', dest='target', help='The name of the Ninja output directory. Defaults to "Default"')
87    parser.add_argument(
88        '--no-text-coverage', action='store_true', default=False, dest='no_text_coverage', help='Whether to output text coverage')
89    parser.add_argument('--coverage',
90                        action='store_true',
91                        default=False,
92                        dest='coverage',
93                        help='Whether to output coverage')
94    parser.add_argument('--chrome-binary',
95                        dest='chrome_binary',
96                        help='Path to Chromium binary')
97    args = parser.parse_args(sys.argv[1:])
98
99    run_unit_tests_on_ninja_build_target(args.target, args.no_text_coverage,
100                                         args.coverage, args.chrome_binary)
101
102
103if __name__ == '__main__':
104    main()
105