blob: da9fcd82bdbc492901f819647c9dbb7b16b022d2 [file] [log] [blame]
Paul Lewise441d1d2019-09-16 14:40:20 +00001#!/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
7import os
8import re
9import subprocess
10import sys
11
Yang Guod8176982019-10-04 20:30:35 +000012import devtools_paths
Paul Lewise441d1d2019-09-16 14:40:20 +000013
Yang Guod584b902019-10-14 12:17:30 +000014is_cygwin = sys.platform == 'cygwin'
Paul Lewise441d1d2019-09-16 14:40:20 +000015chrome_binary = None
16
17if len(sys.argv) >= 2:
Yang Guod584b902019-10-14 12:17:30 +000018 chrome_binary = re.sub(r'^\-\-chrome-binary=(.*)', '\\1', sys.argv[1])
Paul Lewise441d1d2019-09-16 14:40:20 +000019 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 Guod584b902019-10-14 12:17:30 +000021 print('Unable to find a Chrome binary at \'%s\'' % chrome_binary)
Paul Lewise441d1d2019-09-16 14:40:20 +000022 sys.exit(1)
23
24
25def popen(arguments, cwd=None, env=None):
26 return subprocess.Popen(arguments, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
27
28
29def to_platform_path_exact(filepath):
30 if not is_cygwin:
31 return filepath
Yang Guod584b902019-10-14 12:17:30 +000032 output, _ = popen(['cygpath', '-w', filepath]).communicate()
Paul Lewise441d1d2019-09-16 14:40:20 +000033 # pylint: disable=E1103
Yang Guod584b902019-10-14 12:17:30 +000034 return output.strip().replace('\\', '\\\\')
Paul Lewise441d1d2019-09-16 14:40:20 +000035
36
37scripts_path = os.path.dirname(os.path.abspath(__file__))
38devtools_path = os.path.dirname(scripts_path)
39
Yang Guod584b902019-10-14 12:17:30 +000040print('Running tests with Karma...')
Paul Lewise441d1d2019-09-16 14:40:20 +000041if (chrome_binary is not None):
Yang Guod584b902019-10-14 12:17:30 +000042 print('Using custom Chrome Binary (%s)\n' % chrome_binary)
Paul Lewise441d1d2019-09-16 14:40:20 +000043else:
Yang Guod584b902019-10-14 12:17:30 +000044 print('Using system Chrome')
Paul Lewise441d1d2019-09-16 14:40:20 +000045
46
47def run_tests():
48 karma_errors_found = False
49
Yang Guod584b902019-10-14 12:17:30 +000050 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 Lewise441d1d2019-09-16 14:40:20 +000052
Yang Guod8176982019-10-04 20:30:35 +000053 env = {'NODE_PATH': devtools_paths.node_modules_path()}
Paul Lewise441d1d2019-09-16 14:40:20 +000054 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 Guod584b902019-10-14 12:17:30 +000063 print('Karma exited successfully')
Paul Lewise441d1d2019-09-16 14:40:20 +000064
65 print(karma_proc_out)
66 return karma_errors_found
67
68
69errors_found = run_tests()
70
71if errors_found:
Yang Guod584b902019-10-14 12:17:30 +000072 print('ERRORS DETECTED')
Paul Lewise441d1d2019-09-16 14:40:20 +000073 sys.exit(1)