OLD | NEW |
---|---|
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
msw
2015/03/04 00:49:50
This file is so similar to gtest.py, I assume we'l
| |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import logging | 5 import logging |
6 import os | 6 import os |
7 import re | 7 import re |
8 import subprocess | 8 import subprocess |
9 import sys | 9 import sys |
10 | 10 |
11 _logging = logging.getLogger() | 11 _logging = logging.getLogger() |
12 | 12 |
13 from mopy import android | |
14 from mopy.config import Config | 13 from mopy.config import Config |
15 from mopy.paths import Paths | 14 from mopy.paths import Paths |
16 from mopy.print_process_error import print_process_error | 15 from mopy.print_process_error import print_process_error |
17 | 16 |
18 | 17 |
19 def set_color(): | |
20 """Run gtests with color if we're on a TTY (and we're not being told | |
21 explicitly what to do).""" | |
22 if sys.stdout.isatty() and "GTEST_COLOR" not in os.environ: | |
23 _logging.debug("Setting GTEST_COLOR=yes") | |
24 os.environ["GTEST_COLOR"] = "yes" | |
25 | |
26 | |
27 def run_test(config, shell_args, apps_and_args=None, run_launcher=False): | 18 def run_test(config, shell_args, apps_and_args=None, run_launcher=False): |
28 """Runs a command line and checks the output for signs of gtest failure. | 19 """Runs a command line and checks the output for signs of gtest failure. |
29 | 20 |
30 Args: | 21 Args: |
31 config: The mopy.config.Config object for the build. | 22 config: The mopy.config.Config object for the build. |
32 shell_args: The arguments for mojo_shell. | 23 shell_args: The arguments for mojo_shell. |
33 apps_and_args: A Dict keyed by application URL associated to the | 24 apps_and_args: A Dict keyed by application URL associated to the |
34 application's specific arguments. | 25 application's specific arguments. |
35 run_launcher: |True| is mojo_launcher must be used instead of mojo_shell. | 26 run_launcher: |True| is mojo_launcher must be used instead of mojo_shell. |
36 """ | 27 """ |
37 apps_and_args = apps_and_args or {} | 28 apps_and_args = apps_and_args or {} |
38 output = _try_run_test(config, shell_args, apps_and_args, run_launcher) | 29 output = _try_run_test(config, shell_args, apps_and_args, run_launcher) |
39 # Fail on output with gtest's "[ FAILED ]" or a lack of "[ PASSED ]". | 30 # Fail on output with dart unittests' "FAIL:" |
40 # The latter condition ensures failure on broken command lines or output. | 31 if (output is None or output.find("FAIL:") != -1): |
msw
2015/03/04 00:49:50
This should fail on lack of "PASS:" or w/e. The GT
| |
41 # Check output instead of exit codes because mojo_shell always exits with 0. | |
42 if (output is None or | |
43 (output.find("[ FAILED ]") != -1 or output.find("[ PASSED ]") == -1)): | |
44 print "Failed test:" | 32 print "Failed test:" |
45 print_process_error( | 33 print_process_error( |
46 _build_command_line(config, shell_args, apps_and_args, run_launcher), | 34 _build_command_line(config, shell_args, apps_and_args, run_launcher), |
47 output) | 35 output) |
48 return False | 36 return False |
49 _logging.debug("Succeeded with output:\n%s" % output) | 37 _logging.debug("Succeeded with output:\n%s" % output) |
50 return True | 38 return True |
51 | 39 |
52 | 40 |
53 def get_fixtures(config, shell_args, apptest): | |
54 """Returns the "Test.Fixture" list from an apptest using mojo_shell. | |
55 | |
56 Tests are listed by running the given apptest in mojo_shell and passing | |
57 --gtest_list_tests. The output is parsed and reformatted into a list like | |
58 [TestSuite.TestFixture, ... ] | |
59 An empty list is returned on failure, with errors logged. | |
60 | |
61 Args: | |
62 config: The mopy.config.Config object for the build. | |
63 apptest: The URL of the test application to run. | |
64 """ | |
65 try: | |
66 apps_and_args = {apptest: ["--gtest_list_tests"]} | |
67 list_output = _run_test(config, shell_args, apps_and_args) | |
68 _logging.debug("Tests listed:\n%s" % list_output) | |
69 return _gtest_list_tests(list_output) | |
70 except Exception as e: | |
71 print "Failed to get test fixtures:" | |
72 print_process_error( | |
73 _build_command_line(config, shell_args, apps_and_args), e) | |
74 return [] | |
75 | |
76 | |
77 def build_shell_arguments(shell_args, apps_and_args=None): | 41 def build_shell_arguments(shell_args, apps_and_args=None): |
78 """Build the list of arguments for the shell. |shell_args| are the base | 42 """Build the list of arguments for the shell. |shell_args| are the base |
79 arguments, |apps_and_args| is a dictionary that associates each application to | 43 arguments, |apps_and_args| is a dictionary that associates each application to |
80 its specific arguments|. Each app included will be run by the shell. | 44 its specific arguments|. Each app included will be run by the shell. |
81 """ | 45 """ |
82 result = shell_args[:] | 46 result = shell_args[:] |
83 if apps_and_args: | 47 if apps_and_args: |
84 for (application, args) in apps_and_args.items(): | 48 for (application, args) in apps_and_args.items(): |
85 result += ["--args-for=%s %s" % (application, " ".join(args))] | 49 result += ["--args-for=%s %s" % (application, " ".join(args))] |
86 result += apps_and_args.keys() | 50 result += apps_and_args.keys() |
(...skipping 11 matching lines...) Expand all Loading... | |
98 return paths.mojo_shell_path | 62 return paths.mojo_shell_path |
99 | 63 |
100 | 64 |
101 def _build_command_line(config, shell_args, apps_and_args, run_launcher=False): | 65 def _build_command_line(config, shell_args, apps_and_args, run_launcher=False): |
102 executable = _get_shell_executable(config, run_launcher) | 66 executable = _get_shell_executable(config, run_launcher) |
103 return "%s %s" % (executable, " ".join(["%r" % x for x in | 67 return "%s %s" % (executable, " ".join(["%r" % x for x in |
104 build_shell_arguments( | 68 build_shell_arguments( |
105 shell_args, apps_and_args)])) | 69 shell_args, apps_and_args)])) |
106 | 70 |
107 | 71 |
108 def _run_test_android(shell_args, apps_and_args): | |
109 """Run the given test on the single/default android device.""" | |
110 (r, w) = os.pipe() | |
111 with os.fdopen(r, "r") as rf: | |
112 with os.fdopen(w, "w") as wf: | |
113 arguments = build_shell_arguments(shell_args, apps_and_args) | |
114 android.StartShell(arguments, wf, wf.close) | |
115 return rf.read() | |
116 | |
117 | |
118 def _run_test(config, shell_args, apps_and_args, run_launcher=False): | 72 def _run_test(config, shell_args, apps_and_args, run_launcher=False): |
119 """Run the given test, using mojo_launcher if |run_launcher| is True.""" | 73 """Run the given test, using mojo_launcher if |run_launcher| is True.""" |
120 if (config.target_os == Config.OS_ANDROID): | 74 executable = _get_shell_executable(config, run_launcher) |
121 return _run_test_android(shell_args, apps_and_args) | 75 command = ([executable] + build_shell_arguments(shell_args, apps_and_args)) |
122 else: | 76 return subprocess.check_output(command, stderr=subprocess.STDOUT) |
123 executable = _get_shell_executable(config, run_launcher) | |
124 command = ([executable] + build_shell_arguments(shell_args, apps_and_args)) | |
125 return subprocess.check_output(command, stderr=subprocess.STDOUT) | |
126 | 77 |
127 | 78 |
128 def _try_run_test(config, shell_args, apps_and_args, run_launcher): | 79 def _try_run_test(config, shell_args, apps_and_args, run_launcher): |
129 """Returns the output of a command line or an empty string on error.""" | 80 """Returns the output of a command line or an empty string on error.""" |
130 command_line = _build_command_line(config, shell_args, apps_and_args, | 81 command_line = _build_command_line(config, shell_args, apps_and_args, |
131 run_launcher=run_launcher) | 82 run_launcher=run_launcher) |
132 _logging.debug("Running command line: %s" % command_line) | 83 _logging.debug("Running command line: %s" % command_line) |
133 try: | 84 try: |
134 return _run_test(config, shell_args, apps_and_args, run_launcher) | 85 return _run_test(config, shell_args, apps_and_args, run_launcher) |
135 except Exception as e: | 86 except Exception as e: |
136 print_process_error(command_line, e) | 87 print_process_error(command_line, e) |
137 return None | 88 return None |
138 | |
139 | |
140 def _gtest_list_tests(gtest_list_tests_output): | |
141 """Returns a list of strings formatted as TestSuite.TestFixture from the | |
142 output of running --gtest_list_tests on a GTEST application.""" | |
143 | |
144 # Remove log lines. | |
145 gtest_list_tests_output = ( | |
146 re.sub("^\[.*\n", "", gtest_list_tests_output, flags=re.MULTILINE)) | |
147 | |
148 if not re.match("^(\w*\.\r?\n( \w*\r?\n)+)+", gtest_list_tests_output): | |
149 raise Exception("Unrecognized --gtest_list_tests output:\n%s" % | |
150 gtest_list_tests_output) | |
151 | |
152 output_lines = gtest_list_tests_output.split("\n") | |
153 | |
154 test_list = [] | |
155 for line in output_lines: | |
156 if not line: | |
157 continue | |
158 if line[0] != " ": | |
159 suite = line.strip() | |
160 continue | |
161 test_list.append(suite + line.strip()) | |
162 | |
163 return test_list | |
OLD | NEW |