OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | |
jamesr
2015/01/29 20:12:08
2015
#! please
ppi
2015/01/29 23:41:24
Fixed the year. As discussed off-thread, this is n
| |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import logging | |
6 import os | |
7 import re | |
8 import subprocess | |
9 import sys | |
10 | |
11 _logging = logging.getLogger() | |
12 | |
13 def _print_process_error(command_line, error): | |
14 """Properly format an exception raised from a failed command execution.""" | |
15 | |
16 if command_line: | |
17 print 'Failed command: %r' % command_line | |
18 else: | |
19 print 'Failed command:' | |
20 print 72 * '-' | |
21 | |
22 if hasattr(error, 'returncode'): | |
23 print ' with exit code %d' % error.returncode | |
24 print 72 * '-' | |
25 | |
26 if hasattr(error, 'output'): | |
27 print error.output | |
28 else: | |
29 print error | |
30 print 72 * '-' | |
31 | |
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' | |
38 | |
39 | |
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 | |
48 | |
49 | |
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 | |
63 | |
64 | |
65 def get_fixtures(mojo_shell, apptest): | |
66 """Returns the "Test.Fixture" list from an apptest using mojo_shell. | |
67 | |
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 [] | |
84 | |
85 | |
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.""" | |
89 | |
90 if not re.match("^(\w*\.\r?\n( \w*\r?\n)+)+", gtest_list_tests_output): | |
91 raise Exception("Unrecognized --gtest_list_tests output:\n%s" % | |
92 gtest_list_tests_output) | |
93 | |
94 output_lines = gtest_list_tests_output.split('\n') | |
95 | |
96 test_list = [] | |
97 for line in output_lines: | |
98 if not line: | |
99 continue | |
100 if line[0] != ' ': | |
101 suite = line.strip() | |
102 continue | |
103 test_list.append(suite + line.strip()) | |
104 | |
105 return test_list | |
OLD | NEW |