blob: 393b5c34535ff14863de395636afeea53099a193 [file] [log] [blame]
Paul Lewise441d1d2019-09-16 14:40:20 +00001#!/usr/bin/env python
2#
Yang Guo4fd355c2019-09-19 10:59:03 +02003# Copyright 2019 The Chromium Authors. All rights reserved.
Paul Lewise441d1d2019-09-16 14:40:20 +00004# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
Yang Guo4fd355c2019-09-19 10:59:03 +02006"""
7Run Karma unit tests on a pre-built chrome or one specified via --chrome-binary.
8"""
9
Paul Lewise441d1d2019-09-16 14:40:20 +000010import os
11import re
12import subprocess
13import sys
14
Yang Guod8176982019-10-04 20:30:35 +000015import devtools_paths
Paul Lewise441d1d2019-09-16 14:40:20 +000016
Paul Lewise441d1d2019-09-16 14:40:20 +000017
Yang Guo4fd355c2019-09-19 10:59:03 +020018def 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 Lewise441d1d2019-09-16 14:40:20 +000020
21
22def popen(arguments, cwd=None, env=None):
23 return subprocess.Popen(arguments, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
24
Mathias Bynens032591d2019-10-21 11:51:31 +020025
Paul Lewise441d1d2019-09-16 14:40:20 +000026def to_platform_path_exact(filepath):
27 if not is_cygwin:
28 return filepath
Yang Guod584b902019-10-14 12:17:30 +000029 output, _ = popen(['cygpath', '-w', filepath]).communicate()
Paul Lewise441d1d2019-09-16 14:40:20 +000030 # pylint: disable=E1103
Yang Guod584b902019-10-14 12:17:30 +000031 return output.strip().replace('\\', '\\\\')
Paul Lewise441d1d2019-09-16 14:40:20 +000032
33
Paul Lewise441d1d2019-09-16 14:40:20 +000034def run_tests():
35 karma_errors_found = False
36
Yang Guod584b902019-10-14 12:17:30 +000037 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 Lewise441d1d2019-09-16 14:40:20 +000039
Yang Guod8176982019-10-04 20:30:35 +000040 env = {'NODE_PATH': devtools_paths.node_modules_path()}
Paul Lewise441d1d2019-09-16 14:40:20 +000041 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 Guod584b902019-10-14 12:17:30 +000050 print('Karma exited successfully')
Paul Lewise441d1d2019-09-16 14:40:20 +000051
52 print(karma_proc_out)
53 return karma_errors_found
54
55
Yang Guo4fd355c2019-09-19 10:59:03 +020056is_cygwin = sys.platform == "cygwin"
57chrome_binary = None
58DOWNLOADED_CHROME_BINARY = os.path.abspath(
59 os.path.join(os.path.dirname(__file__), '..', 'third_party', 'chrome', 'chrome-linux', 'chrome'))
60
61if check_chrome_binary(DOWNLOADED_CHROME_BINARY):
62 chrome_binary = DOWNLOADED_CHROME_BINARY
63
64if 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
70scripts_path = os.path.dirname(os.path.abspath(__file__))
71devtools_path = os.path.dirname(scripts_path)
72
73print("Running tests with Karma...")
74if (chrome_binary is not None):
75 print("Using custom Chrome Binary (%s)\n" % chrome_binary)
76else:
77 print("Using system Chrome")
78
Paul Lewise441d1d2019-09-16 14:40:20 +000079errors_found = run_tests()
80
81if errors_found:
Yang Guod584b902019-10-14 12:17:30 +000082 print('ERRORS DETECTED')
Paul Lewise441d1d2019-09-16 14:40:20 +000083 sys.exit(1)