Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2016 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 | import os |
| 8 | import re |
| 9 | import subprocess |
| 10 | import sys |
| 11 | |
Yang Guo | d817698 | 2019-10-04 20:30:35 +0000 | [diff] [blame] | 12 | import devtools_paths |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 13 | |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame^] | 14 | is_cygwin = sys.platform == 'cygwin' |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 15 | chrome_binary = None |
| 16 | |
| 17 | if len(sys.argv) >= 2: |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame^] | 18 | chrome_binary = re.sub(r'^\-\-chrome-binary=(.*)', '\\1', sys.argv[1]) |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 19 | is_executable = os.path.exists(chrome_binary) and os.path.isfile(chrome_binary) and os.access(chrome_binary, os.X_OK) |
| 20 | if not is_executable: |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame^] | 21 | print('Unable to find a Chrome binary at \'%s\'' % chrome_binary) |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 22 | sys.exit(1) |
| 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 |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame^] | 32 | output, _ = popen(['cygpath', '-w', filepath]).communicate() |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 33 | # pylint: disable=E1103 |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame^] | 34 | return output.strip().replace('\\', '\\\\') |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | scripts_path = os.path.dirname(os.path.abspath(__file__)) |
| 38 | devtools_path = os.path.dirname(scripts_path) |
| 39 | |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame^] | 40 | print('Running tests with Karma...') |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 41 | if (chrome_binary is not None): |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame^] | 42 | print('Using custom Chrome Binary (%s)\n' % chrome_binary) |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 43 | else: |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame^] | 44 | print('Using system Chrome') |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | def run_tests(): |
| 48 | karma_errors_found = False |
| 49 | |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame^] | 50 | karmaconfig_path = os.path.join(devtools_path, 'karma.conf.js') |
| 51 | exec_command = [devtools_paths.node_path(), devtools_paths.karma_path(), 'start', to_platform_path_exact(karmaconfig_path)] |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 52 | |
Yang Guo | d817698 | 2019-10-04 20:30:35 +0000 | [diff] [blame] | 53 | env = {'NODE_PATH': devtools_paths.node_modules_path()} |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 54 | if (chrome_binary is not None): |
| 55 | env['CHROME_BIN'] = chrome_binary |
| 56 | |
| 57 | karma_proc = popen(exec_command, cwd=devtools_path, env=env) |
| 58 | |
| 59 | (karma_proc_out, _) = karma_proc.communicate() |
| 60 | if karma_proc.returncode != 0: |
| 61 | karma_errors_found = True |
| 62 | else: |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame^] | 63 | print('Karma exited successfully') |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 64 | |
| 65 | print(karma_proc_out) |
| 66 | return karma_errors_found |
| 67 | |
| 68 | |
| 69 | errors_found = run_tests() |
| 70 | |
| 71 | if errors_found: |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame^] | 72 | print('ERRORS DETECTED') |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 73 | sys.exit(1) |