| OLD | NEW |
| (Empty) |
| 1 # Copyright 2014 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. | |
| 4 | |
| 5 """Apptest runner specific to the particular gtest-based apptest framework in | |
| 6 /mojo/public/cpp/application/tests/, built on top of the general apptest | |
| 7 runner.""" | |
| 8 | |
| 9 import logging | |
| 10 import re | |
| 11 | |
| 12 from pylib.apptest import run_apptest | |
| 13 | |
| 14 | |
| 15 _logger = logging.getLogger() | |
| 16 | |
| 17 | |
| 18 def _gtest_apptest_output_test(output): | |
| 19 # Fail on output with gtest's "[ FAILED ]" or a lack of "[ PASSED ]". | |
| 20 # The latter condition ensures failure on broken command lines or output. | |
| 21 # Check output instead of exit codes because mojo_shell always exits with 0. | |
| 22 if (output is None or | |
| 23 (output.find("[ FAILED ]") != -1 or output.find("[ PASSED ]") == -1)): | |
| 24 return False | |
| 25 return True | |
| 26 | |
| 27 | |
| 28 def run_gtest_apptest(shell, shell_args, apptest_url, apptest_args, isolate): | |
| 29 """Runs a gtest apptest. | |
| 30 | |
| 31 Args: | |
| 32 shell: Wrapper around concrete Mojo shell, implementing devtools Shell | |
| 33 interface. | |
| 34 shell_args: The arguments for mojo_shell. | |
| 35 apptest_url: Url of the apptest app to run. | |
| 36 apptest_args: Parameters to be passed to the apptest app. | |
| 37 isolate: Iff True, each test in the app will be run in a separate shell run. | |
| 38 | |
| 39 Returns: | |
| 40 True iff the test succeeded, False otherwise. | |
| 41 """ | |
| 42 | |
| 43 if not isolate: | |
| 44 return run_apptest(shell, shell_args, apptest_url, apptest_args, | |
| 45 _gtest_apptest_output_test) | |
| 46 | |
| 47 # List the apptest fixtures so they can be run independently for isolation. | |
| 48 fixtures = get_fixtures(shell, shell_args, apptest_url) | |
| 49 if not fixtures: | |
| 50 print "No tests to run found in %s." % apptest_url | |
| 51 return False | |
| 52 | |
| 53 apptest_result = True | |
| 54 for fixture in fixtures: | |
| 55 isolated_apptest_args = apptest_args + ["--gtest_filter=%s" % fixture] | |
| 56 success = run_apptest(shell, shell_args, apptest_url, isolated_apptest_args, | |
| 57 _gtest_apptest_output_test) | |
| 58 | |
| 59 if not success: | |
| 60 apptest_result = False | |
| 61 | |
| 62 return apptest_result | |
| 63 | |
| 64 | |
| 65 def get_fixtures(shell, shell_args, 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 Args: | |
| 74 apptest: The URL of the test application to run. | |
| 75 """ | |
| 76 arguments = [] | |
| 77 arguments.extend(shell_args) | |
| 78 arguments.append("--args-for=%s %s" % (apptest, "--gtest_list_tests")) | |
| 79 arguments.append(apptest) | |
| 80 | |
| 81 (exit_code, output) = shell.RunAndGetOutput(arguments) | |
| 82 if exit_code: | |
| 83 command_line = "mojo_shell " + " ".join(["%r" % x for x in arguments]) | |
| 84 print "Failed to get test fixtures: %r" % command_line | |
| 85 print 72 * '-' | |
| 86 print output | |
| 87 print 72 * '-' | |
| 88 return [] | |
| 89 | |
| 90 _logger.debug("Tests listed:\n%s" % output) | |
| 91 return _gtest_list_tests(output) | |
| 92 | |
| 93 | |
| 94 def _gtest_list_tests(gtest_list_tests_output): | |
| 95 """Returns a list of strings formatted as TestSuite.TestFixture from the | |
| 96 output of running --gtest_list_tests on a GTEST application.""" | |
| 97 | |
| 98 # Remove log lines. | |
| 99 gtest_list_tests_output = re.sub("^(\[|WARNING: linker:).*\n", | |
| 100 "", | |
| 101 gtest_list_tests_output, | |
| 102 flags=re.MULTILINE) | |
| 103 | |
| 104 if not re.match("^(\w*\.\r?\n( \w*\r?\n)+)+", gtest_list_tests_output): | |
| 105 raise Exception("Unrecognized --gtest_list_tests output:\n%s" % | |
| 106 gtest_list_tests_output) | |
| 107 | |
| 108 output_lines = gtest_list_tests_output.split("\n") | |
| 109 | |
| 110 test_list = [] | |
| 111 for line in output_lines: | |
| 112 if not line: | |
| 113 continue | |
| 114 if line[0] != " ": | |
| 115 suite = line.strip() | |
| 116 continue | |
| 117 test_list.append(suite + line.strip()) | |
| 118 | |
| 119 return test_list | |
| OLD | NEW |