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 os
8import subprocess
9import sys
10
11FAIL_EMOJI = u'\U0001F631'.encode('utf-8')
12PASS_EMOJI = u'\U0001F601'.encode('utf-8')
13
14GREEN = '\033[92m'
15RED = '\033[91m'
16END_CODE = '\033[0m'
17
18
19def _Color(s, color):
20  """Adds ANSI escape codes to color a string printed to the terminal."""
21  return color + s + END_CODE
22
23
24def _RunTest(test, chrome_command):
25  if sys.platform in test.get('disabled_platforms', {}):
26    return 0
27  command = [test['path']]
28  if sys.platform == 'win32':
29    command = ['python'] + command
30  if test.get('chrome_path_arg') and chrome_command:
31    command += ['--chrome_path', chrome_command]
32  return subprocess.call(command)
33
34
35def Main(name, tests, argv):
36  parser = argparse.ArgumentParser(
37      description='Run all tests of %s project.' % name)
38  parser.add_argument(
39      '--chrome_path', type=str,
40      help='Path to Chrome browser binary for dev_server tests.')
41  args = parser.parse_args(argv[1:])
42
43  exit_code = 0
44  errors = []
45  for test in tests:
46    new_exit_code = _RunTest(test, args.chrome_path)
47    if new_exit_code != 0:
48      exit_code |= new_exit_code
49      errors += '%s failed some tests. Re-run %s script to see those.\n' % (
50          os.path.basename(test['path']), test['path'])
51
52  if exit_code:
53    print _Color('Oops! Some tests failed.', RED), FAIL_EMOJI
54    sys.stderr.writelines(errors)
55  else:
56    print _Color('Woho! All tests passed.', GREEN), PASS_EMOJI
57
58  sys.exit(exit_code)
59