Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 3 | # Copyright 2019 The Chromium Authors. All rights reserved. |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 6 | """ |
| 7 | Run Karma unit tests on a pre-built chrome or one specified via --chrome-binary. |
| 8 | """ |
| 9 | |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 10 | import os |
| 11 | import re |
| 12 | import subprocess |
| 13 | import sys |
| 14 | |
Yang Guo | d817698 | 2019-10-04 20:30:35 +0000 | [diff] [blame] | 15 | import devtools_paths |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 16 | |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 17 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 18 | def check_chrome_binary(chrome_binary): |
| 19 | return os.path.exists(chrome_binary) and os.path.isfile(chrome_binary) and os.access(chrome_binary, os.X_OK) |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | def popen(arguments, cwd=None, env=None): |
| 23 | return subprocess.Popen(arguments, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env) |
| 24 | |
Mathias Bynens | 032591d | 2019-10-21 11:51:31 +0200 | [diff] [blame^] | 25 | |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 26 | def to_platform_path_exact(filepath): |
| 27 | if not is_cygwin: |
| 28 | return filepath |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame] | 29 | output, _ = popen(['cygpath', '-w', filepath]).communicate() |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 30 | # pylint: disable=E1103 |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame] | 31 | return output.strip().replace('\\', '\\\\') |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 32 | |
| 33 | |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 34 | def run_tests(): |
| 35 | karma_errors_found = False |
| 36 | |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame] | 37 | karmaconfig_path = os.path.join(devtools_path, 'karma.conf.js') |
| 38 | 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] | 39 | |
Yang Guo | d817698 | 2019-10-04 20:30:35 +0000 | [diff] [blame] | 40 | env = {'NODE_PATH': devtools_paths.node_modules_path()} |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 41 | if (chrome_binary is not None): |
| 42 | env['CHROME_BIN'] = chrome_binary |
| 43 | |
| 44 | karma_proc = popen(exec_command, cwd=devtools_path, env=env) |
| 45 | |
| 46 | (karma_proc_out, _) = karma_proc.communicate() |
| 47 | if karma_proc.returncode != 0: |
| 48 | karma_errors_found = True |
| 49 | else: |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame] | 50 | print('Karma exited successfully') |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 51 | |
| 52 | print(karma_proc_out) |
| 53 | return karma_errors_found |
| 54 | |
| 55 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 56 | is_cygwin = sys.platform == "cygwin" |
| 57 | chrome_binary = None |
| 58 | DOWNLOADED_CHROME_BINARY = os.path.abspath( |
| 59 | os.path.join(os.path.dirname(__file__), '..', 'third_party', 'chrome', 'chrome-linux', 'chrome')) |
| 60 | |
| 61 | if check_chrome_binary(DOWNLOADED_CHROME_BINARY): |
| 62 | chrome_binary = DOWNLOADED_CHROME_BINARY |
| 63 | |
| 64 | if len(sys.argv) >= 2: |
| 65 | chrome_binary = re.sub(r"^\-\-chrome-binary=(.*)", "\\1", sys.argv[1]) |
| 66 | if not check_chrome_binary(chrome_binary): |
| 67 | print("Unable to find a Chrome binary at \"%s\"" % chrome_binary) |
| 68 | sys.exit(1) |
| 69 | |
| 70 | scripts_path = os.path.dirname(os.path.abspath(__file__)) |
| 71 | devtools_path = os.path.dirname(scripts_path) |
| 72 | |
| 73 | print("Running tests with Karma...") |
| 74 | if (chrome_binary is not None): |
| 75 | print("Using custom Chrome Binary (%s)\n" % chrome_binary) |
| 76 | else: |
| 77 | print("Using system Chrome") |
| 78 | |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 79 | errors_found = run_tests() |
| 80 | |
| 81 | if errors_found: |
Yang Guo | d584b90 | 2019-10-14 12:17:30 +0000 | [diff] [blame] | 82 | print('ERRORS DETECTED') |
Paul Lewis | e441d1d | 2019-09-16 14:40:20 +0000 | [diff] [blame] | 83 | sys.exit(1) |