OLD | NEW |
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2013 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 """Helper class for instrumenation test jar.""" | 5 """Helper class for instrumenation test jar.""" |
6 # pylint: disable=W0702 | 6 # pylint: disable=W0702 |
7 | 7 |
8 import logging | 8 import logging |
9 import os | 9 import os |
10 import pickle | 10 import pickle |
11 import re | 11 import re |
12 import sys | 12 import sys |
13 import tempfile | |
14 | 13 |
15 from pylib import cmd_helper | 14 from pylib import cmd_helper |
16 from pylib import constants | 15 from pylib import constants |
17 from pylib.device import device_utils | 16 from pylib.device import device_utils |
| 17 from pylib.utils import proguard |
18 | 18 |
19 sys.path.insert(0, | 19 sys.path.insert(0, |
20 os.path.join(constants.DIR_SOURCE_ROOT, | 20 os.path.join(constants.DIR_SOURCE_ROOT, |
21 'build', 'util', 'lib', 'common')) | 21 'build', 'util', 'lib', 'common')) |
22 | 22 |
23 import unittest_util # pylint: disable=F0401 | 23 import unittest_util # pylint: disable=F0401 |
24 | 24 |
25 # If you change the cached output of proguard, increment this number | 25 # If you change the cached output of proguard, increment this number |
26 PICKLE_FORMAT_VERSION = 3 | 26 PICKLE_FORMAT_VERSION = 4 |
27 | 27 |
28 | 28 |
29 class TestJar(object): | 29 class TestJar(object): |
30 _ANNOTATIONS = frozenset( | 30 _ANNOTATIONS = frozenset( |
31 ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest', 'EnormousTest', | 31 ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest', 'EnormousTest', |
32 'FlakyTest', 'DisabledTest', 'Manual', 'PerfTest', 'HostDrivenTest', | 32 'FlakyTest', 'DisabledTest', 'Manual', 'PerfTest', 'HostDrivenTest', |
33 'IntegrationTest']) | 33 'IntegrationTest']) |
34 _DEFAULT_ANNOTATION = 'SmallTest' | 34 _DEFAULT_ANNOTATION = 'SmallTest' |
35 _PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$') | 35 _PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$') |
36 _PROGUARD_SUPERCLASS_RE = re.compile(r'\s*? Superclass:\s*([\S]+)$') | 36 _PROGUARD_SUPERCLASS_RE = re.compile(r'\s*? Superclass:\s*([\S]+)$') |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 d['VERSION'] == PICKLE_FORMAT_VERSION): | 79 d['VERSION'] == PICKLE_FORMAT_VERSION): |
80 self._test_methods = d['TEST_METHODS'] | 80 self._test_methods = d['TEST_METHODS'] |
81 return True | 81 return True |
82 except: | 82 except: |
83 logging.warning('PICKLE_FORMAT_VERSION has changed, ignoring cache') | 83 logging.warning('PICKLE_FORMAT_VERSION has changed, ignoring cache') |
84 return False | 84 return False |
85 | 85 |
86 def _GetProguardData(self): | 86 def _GetProguardData(self): |
87 logging.info('Retrieving test methods via proguard.') | 87 logging.info('Retrieving test methods via proguard.') |
88 | 88 |
89 with tempfile.NamedTemporaryFile() as proguard_output: | 89 p = proguard.Dump(self._jar_path) |
90 cmd_helper.RunCmd(['java', '-jar', | |
91 self._PROGUARD_PATH, | |
92 '-injars', self._jar_path, | |
93 '-dontshrink', | |
94 '-dontoptimize', | |
95 '-dontobfuscate', | |
96 '-dontpreverify', | |
97 '-dump', proguard_output.name]) | |
98 | 90 |
99 clazzez = {} | 91 class_lookup = dict((c['class'], c) for c in p['classes']) |
| 92 def recursive_get_annotations(c): |
| 93 s = c['superclass'] |
| 94 if s in class_lookup: |
| 95 a = recursive_get_annotations(class_lookup[s]) |
| 96 else: |
| 97 a = {} |
| 98 a.update(c['annotations']) |
| 99 return a |
100 | 100 |
101 annotation = None | 101 test_classes = (c for c in p['classes'] |
102 annotation_has_value = False | 102 if c['class'].endswith('Test')) |
103 clazz = None | 103 for c in test_classes: |
104 method = None | 104 class_annotations = recursive_get_annotations(c) |
105 | 105 test_methods = (m for m in c['methods'] |
106 for line in proguard_output: | 106 if m['method'].startswith('test')) |
107 if len(line) == 0: | 107 for m in test_methods: |
108 annotation = None | 108 qualified_method = '%s#%s' % (c['class'], m['method']) |
109 annotation_has_value = False | 109 annotations = dict(class_annotations) |
110 method = None | 110 annotations.update(m['annotations']) |
111 continue | 111 self._test_methods[qualified_method] = m |
112 | 112 self._test_methods[qualified_method]['annotations'] = annotations |
113 m = self._PROGUARD_CLASS_RE.match(line) | |
114 if m: | |
115 clazz = m.group(1).replace('/', '.') | |
116 clazzez[clazz] = { | |
117 'methods': {}, | |
118 'annotations': {} | |
119 } | |
120 annotation = None | |
121 annotation_has_value = False | |
122 method = None | |
123 continue | |
124 | |
125 if not clazz: | |
126 continue | |
127 | |
128 m = self._PROGUARD_SUPERCLASS_RE.match(line) | |
129 if m: | |
130 clazzez[clazz]['superclass'] = m.group(1).replace('/', '.') | |
131 continue | |
132 | |
133 if clazz.endswith('Test'): | |
134 m = self._PROGUARD_METHOD_RE.match(line) | |
135 if m: | |
136 method = m.group(1) | |
137 clazzez[clazz]['methods'][method] = {'annotations': {}} | |
138 annotation = None | |
139 annotation_has_value = False | |
140 continue | |
141 | |
142 m = self._PROGUARD_ANNOTATION_RE.match(line) | |
143 if m: | |
144 # Ignore the annotation package. | |
145 annotation = m.group(1).split('/')[-1] | |
146 if method: | |
147 clazzez[clazz]['methods'][method]['annotations'][annotation] = None | |
148 else: | |
149 clazzez[clazz]['annotations'][annotation] = None | |
150 continue | |
151 | |
152 if annotation: | |
153 if not annotation_has_value: | |
154 m = self._PROGUARD_ANNOTATION_CONST_RE.match(line) | |
155 annotation_has_value = bool(m) | |
156 else: | |
157 m = self._PROGUARD_ANNOTATION_VALUE_RE.match(line) | |
158 if m: | |
159 if method: | |
160 clazzez[clazz]['methods'][method]['annotations'][annotation] = ( | |
161 m.group(1)) | |
162 else: | |
163 clazzez[clazz]['annotations'][annotation] = m.group(1) | |
164 annotation_has_value = None | |
165 | |
166 test_clazzez = ((n, i) for n, i in clazzez.items() if n.endswith('Test')) | |
167 for clazz_name, clazz_info in test_clazzez: | |
168 logging.info('Processing %s' % clazz_name) | |
169 c = clazz_name | |
170 min_sdk_level = None | |
171 | |
172 while c in clazzez: | |
173 c_info = clazzez[c] | |
174 if not min_sdk_level: | |
175 min_sdk_level = c_info['annotations'].get('MinAndroidSdkLevel', None) | |
176 c = c_info.get('superclass', None) | |
177 | |
178 for method_name, method_info in clazz_info['methods'].items(): | |
179 if method_name.startswith('test'): | |
180 qualified_method = '%s#%s' % (clazz_name, method_name) | |
181 method_annotations = [] | |
182 for annotation_name, annotation_value in ( | |
183 method_info['annotations'].items()): | |
184 method_annotations.append(annotation_name) | |
185 if annotation_value: | |
186 method_annotations.append( | |
187 annotation_name + ':' + annotation_value) | |
188 self._test_methods[qualified_method] = { | |
189 'annotations': method_annotations | |
190 } | |
191 if min_sdk_level is not None: | |
192 self._test_methods[qualified_method]['min_sdk_level'] = ( | |
193 int(min_sdk_level)) | |
194 | 113 |
195 logging.info('Storing proguard output to %s', self._pickled_proguard_name) | 114 logging.info('Storing proguard output to %s', self._pickled_proguard_name) |
196 d = {'VERSION': PICKLE_FORMAT_VERSION, | 115 d = {'VERSION': PICKLE_FORMAT_VERSION, |
197 'TEST_METHODS': self._test_methods, | 116 'TEST_METHODS': self._test_methods, |
198 'JAR_MD5SUM': self._CalculateMd5(self._jar_path)} | 117 'JAR_MD5SUM': self._CalculateMd5(self._jar_path)} |
199 with open(self._pickled_proguard_name, 'w') as f: | 118 with open(self._pickled_proguard_name, 'w') as f: |
200 f.write(pickle.dumps(d)) | 119 f.write(pickle.dumps(d)) |
201 | 120 |
202 | |
203 @staticmethod | 121 @staticmethod |
204 def _IsTestMethod(test): | 122 def _IsTestMethod(test): |
205 class_name, method = test.split('#') | 123 class_name, method = test.split('#') |
206 return class_name.endswith('Test') and method.startswith('test') | 124 return class_name.endswith('Test') and method.startswith('test') |
207 | 125 |
208 def GetTestAnnotations(self, test): | 126 def GetTestAnnotations(self, test): |
209 """Returns a list of all annotations for the given |test|. May be empty.""" | 127 """Returns a list of all annotations for the given |test|. May be empty.""" |
210 if not self._IsTestMethod(test) or not test in self._test_methods: | 128 if not self._IsTestMethod(test) or not test in self._test_methods: |
211 return [] | 129 return [] |
212 return self._test_methods[test]['annotations'] | 130 return self._test_methods[test]['annotations'] |
213 | 131 |
214 @staticmethod | 132 @staticmethod |
215 def _AnnotationsMatchFilters(annotation_filter_list, annotations): | 133 def _AnnotationsMatchFilters(annotation_filter_list, annotations): |
216 """Checks if annotations match any of the filters.""" | 134 """Checks if annotations match any of the filters.""" |
217 if not annotation_filter_list: | 135 if not annotation_filter_list: |
218 return True | 136 return True |
219 for annotation_filter in annotation_filter_list: | 137 for annotation_filter in annotation_filter_list: |
220 filters = annotation_filter.split('=') | 138 filters = annotation_filter.split('=') |
221 if len(filters) == 2: | 139 if len(filters) == 2: |
222 key = filters[0] | 140 key = filters[0] |
223 value_list = filters[1].split(',') | 141 value_list = filters[1].split(',') |
224 for value in value_list: | 142 for value in value_list: |
225 if key + ':' + value in annotations: | 143 if key in annotations and value == annotations['key']: |
226 return True | 144 return True |
227 elif annotation_filter in annotations: | 145 elif annotation_filter in annotations: |
228 return True | 146 return True |
229 return False | 147 return False |
230 | 148 |
231 def GetAnnotatedTests(self, annotation_filter_list): | 149 def GetAnnotatedTests(self, annotation_filter_list): |
232 """Returns a list of all tests that match the given annotation filters.""" | 150 """Returns a list of all tests that match the given annotation filters.""" |
233 return [test for test, attr in self.GetTestMethods().iteritems() | 151 return [test for test in self.GetTestMethods() |
234 if self._IsTestMethod(test) and self._AnnotationsMatchFilters( | 152 if self._IsTestMethod(test) and self._AnnotationsMatchFilters( |
235 annotation_filter_list, attr['annotations'])] | 153 annotation_filter_list, self.GetTestAnnotations(test))] |
236 | 154 |
237 def GetTestMethods(self): | 155 def GetTestMethods(self): |
238 """Returns a dict of all test methods and relevant attributes. | 156 """Returns a dict of all test methods and relevant attributes. |
239 | 157 |
240 Test methods are retrieved as Class#testMethod. | 158 Test methods are retrieved as Class#testMethod. |
241 """ | 159 """ |
242 return self._test_methods | 160 return self._test_methods |
243 | 161 |
244 def _GetTestsMissingAnnotation(self): | 162 def _GetTestsMissingAnnotation(self): |
245 """Get a list of test methods with no known annotations.""" | 163 """Get a list of test methods with no known annotations.""" |
246 tests_missing_annotations = [] | 164 tests_missing_annotations = [] |
247 for test_method in self.GetTestMethods().iterkeys(): | 165 for test_method in self.GetTestMethods().iterkeys(): |
248 annotations_ = frozenset(self.GetTestAnnotations(test_method)) | 166 annotations_ = frozenset(self.GetTestAnnotations(test_method).iterkeys()) |
249 if (annotations_.isdisjoint(self._ANNOTATIONS) and | 167 if (annotations_.isdisjoint(self._ANNOTATIONS) and |
250 not self.IsHostDrivenTest(test_method)): | 168 not self.IsHostDrivenTest(test_method)): |
251 tests_missing_annotations.append(test_method) | 169 tests_missing_annotations.append(test_method) |
252 return sorted(tests_missing_annotations) | 170 return sorted(tests_missing_annotations) |
253 | 171 |
254 def _IsTestValidForSdkRange(self, test_name, attached_min_sdk_level): | 172 def _IsTestValidForSdkRange(self, test_name, attached_min_sdk_level): |
255 required_min_sdk_level = self.GetTestMethods()[test_name].get( | 173 required_min_sdk_level = int( |
256 'min_sdk_level', None) | 174 self.GetTestAnnotations(test_name).get('MinAndroidSdkLevel', 0)) |
257 return (required_min_sdk_level is None or | 175 return (required_min_sdk_level is None or |
258 attached_min_sdk_level >= required_min_sdk_level) | 176 attached_min_sdk_level >= required_min_sdk_level) |
259 | 177 |
260 def GetAllMatchingTests(self, annotation_filter_list, | 178 def GetAllMatchingTests(self, annotation_filter_list, |
261 exclude_annotation_list, test_filter): | 179 exclude_annotation_list, test_filter): |
262 """Get a list of tests matching any of the annotations and the filter. | 180 """Get a list of tests matching any of the annotations and the filter. |
263 | 181 |
264 Args: | 182 Args: |
265 annotation_filter_list: List of test annotations. A test must have at | 183 annotation_filter_list: List of test annotations. A test must have at |
266 least one of these annotations. A test without any annotations is | 184 least one of these annotations. A test without any annotations is |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 'ro.build.version.sdk').pGet(None)] | 230 'ro.build.version.sdk').pGet(None)] |
313 tests = filter( | 231 tests = filter( |
314 lambda t: self._IsTestValidForSdkRange(t, min(sdk_versions)), | 232 lambda t: self._IsTestValidForSdkRange(t, min(sdk_versions)), |
315 tests) | 233 tests) |
316 | 234 |
317 return tests | 235 return tests |
318 | 236 |
319 @staticmethod | 237 @staticmethod |
320 def IsHostDrivenTest(test): | 238 def IsHostDrivenTest(test): |
321 return 'pythonDrivenTests' in test | 239 return 'pythonDrivenTests' in test |
OLD | NEW |