OLD | NEW |
---|---|
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 |
msw
2015/03/04 21:04:32
nit: no longer needed
| |
9 import sys | 9 import sys |
10 | 10 |
11 _logging = logging.getLogger() | 11 _logging = logging.getLogger() |
12 | 12 |
13 from mopy import android | 13 from mopy import android |
msw
2015/03/04 21:04:32
nit: no longer needed
| |
14 from mopy import test_util | |
14 from mopy.config import Config | 15 from mopy.config import Config |
15 from mopy.paths import Paths | 16 from mopy.paths import Paths |
msw
2015/03/04 21:04:32
nit: no longer needed
| |
16 from mopy.print_process_error import print_process_error | 17 from mopy.print_process_error import print_process_error |
17 | 18 |
18 | 19 |
19 def set_color(): | 20 def set_color(): |
20 """Run gtests with color if we're on a TTY (and we're not being told | 21 """Run gtests with color if we're on a TTY (and we're not being told |
21 explicitly what to do).""" | 22 explicitly what to do).""" |
22 if sys.stdout.isatty() and "GTEST_COLOR" not in os.environ: | 23 if sys.stdout.isatty() and "GTEST_COLOR" not in os.environ: |
23 _logging.debug("Setting GTEST_COLOR=yes") | 24 _logging.debug("Setting GTEST_COLOR=yes") |
24 os.environ["GTEST_COLOR"] = "yes" | 25 os.environ["GTEST_COLOR"] = "yes" |
25 | 26 |
26 | 27 |
27 def run_test(config, shell_args, apps_and_args=None, run_launcher=False): | 28 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. | 29 """Runs a command line and checks the output for signs of gtest failure. |
29 | 30 |
30 Args: | 31 Args: |
31 config: The mopy.config.Config object for the build. | 32 config: The mopy.config.Config object for the build. |
32 shell_args: The arguments for mojo_shell. | 33 shell_args: The arguments for mojo_shell. |
33 apps_and_args: A Dict keyed by application URL associated to the | 34 apps_and_args: A Dict keyed by application URL associated to the |
34 application's specific arguments. | 35 application's specific arguments. |
35 run_launcher: |True| is mojo_launcher must be used instead of mojo_shell. | 36 run_launcher: |True| is mojo_launcher must be used instead of mojo_shell. |
36 """ | 37 """ |
37 apps_and_args = apps_and_args or {} | 38 apps_and_args = apps_and_args or {} |
38 output = _try_run_test(config, shell_args, apps_and_args, run_launcher) | 39 output = test_util.try_run_test(config, shell_args, apps_and_args, |
40 run_launcher) | |
39 # Fail on output with gtest's "[ FAILED ]" or a lack of "[ PASSED ]". | 41 # Fail on output with gtest's "[ FAILED ]" or a lack of "[ PASSED ]". |
40 # The latter condition ensures failure on broken command lines or output. | 42 # The latter condition ensures failure on broken command lines or output. |
41 # Check output instead of exit codes because mojo_shell always exits with 0. | 43 # Check output instead of exit codes because mojo_shell always exits with 0. |
42 if (output is None or | 44 if (output is None or |
43 (output.find("[ FAILED ]") != -1 or output.find("[ PASSED ]") == -1)): | 45 (output.find("[ FAILED ]") != -1 or output.find("[ PASSED ]") == -1)): |
44 print "Failed test:" | 46 print "Failed test:" |
45 print_process_error( | 47 print_process_error( |
46 _build_command_line(config, shell_args, apps_and_args, run_launcher), | 48 test_util.build_command_line(config, shell_args, apps_and_args, |
49 run_launcher), | |
47 output) | 50 output) |
48 return False | 51 return False |
49 _logging.debug("Succeeded with output:\n%s" % output) | 52 _logging.debug("Succeeded with output:\n%s" % output) |
50 return True | 53 return True |
51 | 54 |
52 | 55 |
53 def get_fixtures(config, shell_args, apptest): | 56 def get_fixtures(config, shell_args, apptest): |
54 """Returns the "Test.Fixture" list from an apptest using mojo_shell. | 57 """Returns the "Test.Fixture" list from an apptest using mojo_shell. |
55 | 58 |
56 Tests are listed by running the given apptest in mojo_shell and passing | 59 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 | 60 --gtest_list_tests. The output is parsed and reformatted into a list like |
58 [TestSuite.TestFixture, ... ] | 61 [TestSuite.TestFixture, ... ] |
59 An empty list is returned on failure, with errors logged. | 62 An empty list is returned on failure, with errors logged. |
60 | 63 |
61 Args: | 64 Args: |
62 config: The mopy.config.Config object for the build. | 65 config: The mopy.config.Config object for the build. |
63 apptest: The URL of the test application to run. | 66 apptest: The URL of the test application to run. |
64 """ | 67 """ |
65 try: | 68 try: |
66 apps_and_args = {apptest: ["--gtest_list_tests"]} | 69 apps_and_args = {apptest: ["--gtest_list_tests"]} |
67 list_output = _run_test(config, shell_args, apps_and_args) | 70 list_output = test_util.run_test(config, shell_args, apps_and_args) |
68 _logging.debug("Tests listed:\n%s" % list_output) | 71 _logging.debug("Tests listed:\n%s" % list_output) |
69 return _gtest_list_tests(list_output) | 72 return _gtest_list_tests(list_output) |
70 except Exception as e: | 73 except Exception as e: |
71 print "Failed to get test fixtures:" | 74 print "Failed to get test fixtures:" |
72 print_process_error( | 75 print_process_error( |
73 _build_command_line(config, shell_args, apps_and_args), e) | 76 test_util.build_command_line(config, shell_args, apps_and_args), e) |
74 return [] | 77 return [] |
75 | 78 |
76 | 79 |
77 def build_shell_arguments(shell_args, apps_and_args=None): | |
78 """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 | |
80 its specific arguments|. Each app included will be run by the shell. | |
81 """ | |
82 result = shell_args[:] | |
83 if apps_and_args: | |
84 for (application, args) in apps_and_args.items(): | |
85 result += ["--args-for=%s %s" % (application, " ".join(args))] | |
86 result += apps_and_args.keys() | |
87 return result | |
88 | |
89 | |
90 def _get_shell_executable(config, run_launcher): | |
91 paths = Paths(config=config) | |
92 if config.target_os == Config.OS_ANDROID: | |
93 return os.path.join(paths.src_root, "mojo", "tools", | |
94 "android_mojo_shell.py") | |
95 elif run_launcher: | |
96 return paths.mojo_launcher_path | |
97 else: | |
98 return paths.mojo_shell_path | |
99 | |
100 | |
101 def _build_command_line(config, shell_args, apps_and_args, run_launcher=False): | |
102 executable = _get_shell_executable(config, run_launcher) | |
103 return "%s %s" % (executable, " ".join(["%r" % x for x in | |
104 build_shell_arguments( | |
105 shell_args, apps_and_args)])) | |
106 | |
107 | |
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): | |
119 """Run the given test, using mojo_launcher if |run_launcher| is True.""" | |
120 if (config.target_os == Config.OS_ANDROID): | |
121 return _run_test_android(shell_args, apps_and_args) | |
122 else: | |
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 | |
127 | |
128 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.""" | |
130 command_line = _build_command_line(config, shell_args, apps_and_args, | |
131 run_launcher=run_launcher) | |
132 _logging.debug("Running command line: %s" % command_line) | |
133 try: | |
134 return _run_test(config, shell_args, apps_and_args, run_launcher) | |
135 except Exception as e: | |
136 print_process_error(command_line, e) | |
137 return None | |
138 | |
139 | |
140 def _gtest_list_tests(gtest_list_tests_output): | 80 def _gtest_list_tests(gtest_list_tests_output): |
141 """Returns a list of strings formatted as TestSuite.TestFixture from the | 81 """Returns a list of strings formatted as TestSuite.TestFixture from the |
142 output of running --gtest_list_tests on a GTEST application.""" | 82 output of running --gtest_list_tests on a GTEST application.""" |
143 | 83 |
144 # Remove log lines. | 84 # Remove log lines. |
145 gtest_list_tests_output = ( | 85 gtest_list_tests_output = ( |
146 re.sub("^\[.*\n", "", gtest_list_tests_output, flags=re.MULTILINE)) | 86 re.sub("^\[.*\n", "", gtest_list_tests_output, flags=re.MULTILINE)) |
147 | 87 |
148 if not re.match("^(\w*\.\r?\n( \w*\r?\n)+)+", gtest_list_tests_output): | 88 if not re.match("^(\w*\.\r?\n( \w*\r?\n)+)+", gtest_list_tests_output): |
149 raise Exception("Unrecognized --gtest_list_tests output:\n%s" % | 89 raise Exception("Unrecognized --gtest_list_tests output:\n%s" % |
150 gtest_list_tests_output) | 90 gtest_list_tests_output) |
151 | 91 |
152 output_lines = gtest_list_tests_output.split("\n") | 92 output_lines = gtest_list_tests_output.split("\n") |
153 | 93 |
154 test_list = [] | 94 test_list = [] |
155 for line in output_lines: | 95 for line in output_lines: |
156 if not line: | 96 if not line: |
157 continue | 97 continue |
158 if line[0] != " ": | 98 if line[0] != " ": |
159 suite = line.strip() | 99 suite = line.strip() |
160 continue | 100 continue |
161 test_list.append(suite + line.strip()) | 101 test_list.append(suite + line.strip()) |
162 | 102 |
163 return test_list | 103 return test_list |
OLD | NEW |