Yang Guo | 75beda9 | 2019-10-28 08:29:25 +0100 | [diff] [blame^] | 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 | """ |
| 7 | Run Karma unit tests on a pre-built chrome or one specified via --chrome-binary. |
| 8 | """ |
| 9 | |
| 10 | import os |
| 11 | import re |
| 12 | import subprocess |
| 13 | import sys |
| 14 | |
| 15 | scripts_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 16 | sys.path.append(scripts_path) |
| 17 | |
| 18 | import devtools_paths |
| 19 | |
| 20 | |
| 21 | def check_chrome_binary(chrome_binary): |
| 22 | return os.path.exists(chrome_binary) and os.path.isfile(chrome_binary) and os.access(chrome_binary, os.X_OK) |
| 23 | |
| 24 | |
| 25 | def popen(arguments, cwd=None, env=None): |
| 26 | return subprocess.Popen(arguments, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env) |
| 27 | |
| 28 | |
| 29 | def to_platform_path_exact(filepath): |
| 30 | if not is_cygwin: |
| 31 | return filepath |
| 32 | output, _ = popen(['cygpath', '-w', filepath]).communicate() |
| 33 | # pylint: disable=E1103 |
| 34 | return output.strip().replace('\\', '\\\\') |
| 35 | |
| 36 | |
| 37 | def run_tests(): |
| 38 | karma_errors_found = False |
| 39 | karmaconfig_path = os.path.join(devtools_path, 'karma.conf.js') |
| 40 | exec_command = [devtools_paths.node_path(), devtools_paths.karma_path(), 'start', to_platform_path_exact(karmaconfig_path)] |
| 41 | |
| 42 | env = {'NODE_PATH': devtools_paths.node_modules_path()} |
| 43 | if (chrome_binary is not None): |
| 44 | env['CHROME_BIN'] = chrome_binary |
| 45 | |
| 46 | karma_proc = popen(exec_command, cwd=devtools_path, env=env) |
| 47 | |
| 48 | (karma_proc_out, _) = karma_proc.communicate() |
| 49 | if karma_proc.returncode != 0: |
| 50 | karma_errors_found = True |
| 51 | else: |
| 52 | print('Karma exited successfully') |
| 53 | |
| 54 | print(karma_proc_out) |
| 55 | return karma_errors_found |
| 56 | |
| 57 | |
| 58 | devtools_path = devtools_paths.devtools_root_path() |
| 59 | is_cygwin = sys.platform == 'cygwin' |
| 60 | chrome_binary = None |
| 61 | DOWNLOADED_CHROME_BINARY = os.path.abspath(os.path.join(devtools_path, 'third_party', 'chrome', 'chrome-linux', 'chrome')) |
| 62 | |
| 63 | if check_chrome_binary(DOWNLOADED_CHROME_BINARY): |
| 64 | chrome_binary = DOWNLOADED_CHROME_BINARY |
| 65 | |
| 66 | if len(sys.argv) >= 2: |
| 67 | chrome_binary = re.sub(r'^\-\-chrome-binary=(.*)', '\\1', sys.argv[1]) |
| 68 | if not check_chrome_binary(chrome_binary): |
| 69 | print('Unable to find a Chrome binary at \'%s\'' % chrome_binary) |
| 70 | sys.exit(1) |
| 71 | print('Running tests with Karma...') |
| 72 | if (chrome_binary is not None): |
| 73 | print('Using custom Chrome Binary (%s)\n' % chrome_binary) |
| 74 | else: |
| 75 | print('Using system Chrome') |
| 76 | |
| 77 | |
| 78 | def main(): |
| 79 | errors_found = run_tests() |
| 80 | |
| 81 | if errors_found: |
| 82 | print('ERRORS DETECTED') |
| 83 | sys.exit(1) |
| 84 | |
| 85 | |
| 86 | if __name__ == '__main__': |
| 87 | main() |