1#!/usr/bin/env python
2# Copyright (c) 2015 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import argparse
7import logging
8import os
9import subprocess
10import sys
11
12
13_RUN_PY_TESTS_DIR = os.path.join(os.path.dirname(__file__), 'run_py_tests')
14_RUN_D8_TESTS_DIR = os.path.join(os.path.dirname(__file__), 'run_d8_tests')
15_RUN_DEV_SERVER_DIR = os.path.join(os.path.dirname(__file__), 'run_dev_server')
16
17
18class bcolors(object):
19    HEADER = '\033[95m'
20    OKBLUE = '\033[94m'
21    OKGREEN = '\033[92m'
22    WARNING = '\033[93m'
23    FAIL = '\033[91m'
24    ENDC = '\033[0m'
25    BOLD = '\033[1m'
26    UNDERLINE = '\033[4m'
27
28
29def _RunPyTests():
30  return subprocess.call([_RUN_PY_TESTS_DIR])
31
32
33def _RunD8Tests():
34  return subprocess.call([_RUN_D8_TESTS_DIR])
35
36
37def _RunDevServer(chrome_command):
38  if (sys.platform.startswith('linux') and os.getenv('DISPLAY') is None):
39    logging.warning(
40      'Browser found but cannot run browser tests because you do not have a '
41      'DISPLAY environment set.')
42    return 0
43  args = [
44    chrome_command, '--no-sandbox', '--no-experiments', '--no-first-run',
45    '--noerrdialogs',
46    'http://localhost:8003/base/tests.html?headless=true&testTypeToRun=all']
47  chrome_proc = subprocess.Popen(args)
48  exit_code = subprocess.call([_RUN_DEV_SERVER_DIR])
49  chrome_proc.terminate()
50  if chrome_proc.poll() is None:
51    chrome_proc.kill()
52  return exit_code
53
54
55def _TryGettingChromeCommand():
56  if sys.platform.startswith('darwin'):  # Mac
57    chrome_path = (
58      '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome')
59    if os.path.isfile(chrome_path):
60      return chrome_path
61  elif sys.platform.startswith('linux'):
62    found = False
63    try:
64      with open(os.devnull, 'w') as devnull:
65        found = subprocess.call(['google-chrome', '--version'],
66                                stdout=devnull, stderr=devnull) == 0
67    except OSError:
68      pass
69    if found:
70      return 'google-chrome'
71  # TODO(nedn): Find the default path to chrome on win platform.
72  return None
73
74
75if __name__ == '__main__':
76  parser = argparse.ArgumentParser(
77      description='Run all tests of tracing project.')
78  parser.add_argument('--chrome_path', type=str,
79                      help='Path to Chrome browser binary.')
80  args = parser.parse_args()
81  chrome_command = args.chrome_path
82  if not chrome_command:
83    chrome_command = _TryGettingChromeCommand()
84
85  run_py_tests_exit_code = _RunPyTests()
86  run_d8_tests_exit_code = _RunD8Tests()
87  if chrome_command:
88    run_dev_server_exit_code = _RunDevServer(chrome_command)
89  else:
90    logging.warning(
91      'Could not run browser tests because chrome is missing.')
92    run_dev_server_exit_code = 0
93
94  exit_code = (run_py_tests_exit_code | run_dev_server_exit_code |
95               run_d8_tests_exit_code)
96  if exit_code:
97    print (bcolors.FAIL +
98           'Oooops! Looks like some of your tests have failed.' +
99           bcolors.ENDC), u'\U0001F631'.encode('utf-8')
100  else:
101    print (bcolors.OKGREEN +
102           'Woho! All the tests have passed. You are awesome!' +
103           bcolors.ENDC), u'\U0001F601'.encode('utf-8')
104
105  if run_py_tests_exit_code:
106    sys.stderr.write(
107      'run_py_tests have some failed tests. Rerun run_py_tests script '
108      'to see those.\n')
109  if run_d8_tests_exit_code:
110    sys.stderr.write(
111      'run_d8_tests have some failed tests. Rerun run_d8_tests script '
112      'to see those.\n')
113  if run_dev_server_exit_code:
114    sys.stderr.write(
115      'run_dev_server have some failed tests. Rerun run_dev_server script '
116      'and open the browser to see those.\n')
117
118  sys.exit(exit_code)
119