Update FirstRunPageDelegate interface.
[chromium-blink-merge.git] / mojo / tools / gtest.py
blobc03b62f49cc4b922022761bc756c35dc3ca45d1e
1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import logging
6 import os
7 import re
8 import subprocess
9 import sys
11 _logging = logging.getLogger()
13 def _print_process_error(command_line, error):
14 """Properly format an exception raised from a failed command execution."""
16 if command_line:
17 print 'Failed command: %r' % command_line
18 else:
19 print 'Failed command:'
20 print 72 * '-'
22 if hasattr(error, 'returncode'):
23 print ' with exit code %d' % error.returncode
24 print 72 * '-'
26 if hasattr(error, 'output'):
27 print error.output
28 else:
29 print error
30 print 72 * '-'
32 def set_color():
33 """Run gtests with color if we're on a TTY (and we're not being told
34 explicitly what to do)."""
35 if sys.stdout.isatty() and 'GTEST_COLOR' not in os.environ:
36 _logging.debug("Setting GTEST_COLOR=yes")
37 os.environ['GTEST_COLOR'] = 'yes'
40 def _try_command_line(command_line):
41 """Returns the output of a command line or an empty string on error."""
42 _logging.debug("Running command line: %s" % command_line)
43 try:
44 return subprocess.check_output(command_line, stderr=subprocess.STDOUT)
45 except Exception as e:
46 _print_process_error(command_line, e)
47 return None
50 def run_test(command_line):
51 """Runs a command line and checks the output for signs of gtest failure."""
52 output = _try_command_line(command_line)
53 # Fail on output with gtest's "[ FAILED ]" or a lack of "[ PASSED ]".
54 # The latter condition ensures failure on broken command lines or output.
55 # Check output instead of exit codes because mojo_shell always exits with 0.
56 if (output is None or
57 (output.find("[ FAILED ]") != -1 or output.find("[ PASSED ]") == -1)):
58 print "Failed test:"
59 _print_process_error(command_line, output)
60 return False
61 _logging.debug("Succeeded with output:\n%s" % output)
62 return True
65 def get_fixtures(mojo_shell, apptest):
66 """Returns the "Test.Fixture" list from an apptest using mojo_shell.
68 Tests are listed by running the given apptest in mojo_shell and passing
69 --gtest_list_tests. The output is parsed and reformatted into a list like
70 [TestSuite.TestFixture, ... ]
71 An empty list is returned on failure, with errors logged.
72 """
73 command = [mojo_shell,
74 "--args-for={0} --gtest_list_tests".format(apptest),
75 apptest]
76 try:
77 list_output = subprocess.check_output(command, stderr=subprocess.STDOUT)
78 _logging.debug("Tests listed:\n%s" % list_output)
79 return _gtest_list_tests(list_output)
80 except Exception as e:
81 print "Failed to get test fixtures:"
82 _print_process_error(command, e)
83 return []
86 def _gtest_list_tests(gtest_list_tests_output):
87 """Returns a list of strings formatted as TestSuite.TestFixture from the
88 output of running --gtest_list_tests on a GTEST application."""
90 # Remove log lines.
91 gtest_list_tests_output = (
92 re.sub("^\[.*\n", "", gtest_list_tests_output, flags=re.MULTILINE))
94 if not re.match("^(\w*\.\r?\n( \w*\r?\n)+)+", gtest_list_tests_output):
95 raise Exception("Unrecognized --gtest_list_tests output:\n%s" %
96 gtest_list_tests_output)
98 output_lines = gtest_list_tests_output.split('\n')
100 test_list = []
101 for line in output_lines:
102 if not line:
103 continue
104 if line[0] != ' ':
105 suite = line.strip()
106 continue
107 test_list.append(suite + line.strip())
109 return test_list