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 | |
| 12 | import local_node |
| 13 | |
| 14 | is_cygwin = sys.platform == "cygwin" |
| 15 | chrome_binary = None |
| 16 | |
| 17 | if len(sys.argv) >= 2: |
| 18 | chrome_binary = re.sub(r"^\-\-chrome-binary=(.*)", "\\1", sys.argv[1]) |
| 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: |
| 21 | print("Unable to find a Chrome binary at \"%s\"" % chrome_binary) |
| 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 |
| 32 | output, _ = popen(["cygpath", "-w", filepath]).communicate() |
| 33 | # pylint: disable=E1103 |
| 34 | return output.strip().replace("\\", "\\\\") |
| 35 | |
| 36 | |
| 37 | scripts_path = os.path.dirname(os.path.abspath(__file__)) |
| 38 | devtools_path = os.path.dirname(scripts_path) |
| 39 | |
| 40 | print("Running tests with Karma...") |
| 41 | if (chrome_binary is not None): |
| 42 | print("Using custom Chrome Binary (%s)\n" % chrome_binary) |
| 43 | else: |
| 44 | print("Using system Chrome") |
| 45 | |
| 46 | |
| 47 | def run_tests(): |
| 48 | karma_errors_found = False |
| 49 | |
| 50 | karmaconfig_path = os.path.join(devtools_path, "karma.conf.js") |
| 51 | exec_command = [local_node.node_path(), local_node.karma_path(), "start", to_platform_path_exact(karmaconfig_path)] |
| 52 | |
| 53 | env = {'NODE_PATH': local_node.node_modules_path()} |
| 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: |
| 63 | print("Karma exited successfully") |
| 64 | |
| 65 | print(karma_proc_out) |
| 66 | return karma_errors_found |
| 67 | |
| 68 | |
| 69 | errors_found = run_tests() |
| 70 | |
| 71 | if errors_found: |
| 72 | print("ERRORS DETECTED") |
| 73 | sys.exit(1) |