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.
11 _logging
= logging
.getLogger()
13 def _print_process_error(command_line
, error
):
14 """Properly format an exception raised from a failed command execution."""
17 print 'Failed command: %r' % command_line
19 print 'Failed command:'
22 if hasattr(error
, 'returncode'):
23 print ' with exit code %d' % error
.returncode
26 if hasattr(error
, 'output'):
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
)
44 return subprocess
.check_output(command_line
, stderr
=subprocess
.STDOUT
)
45 except Exception as e
:
46 _print_process_error(command_line
, e
)
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.
57 (output
.find("[ FAILED ]") != -1 or output
.find("[ PASSED ]") == -1)):
59 _print_process_error(command_line
, output
)
61 _logging
.debug("Succeeded with output:\n%s" % output
)
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.
73 command
= [mojo_shell
,
74 "--args-for={0} --gtest_list_tests".format(apptest
),
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
)
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."""
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')
101 for line
in output_lines
:
107 test_list
.append(suite
+ line
.strip())