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 collections | |
9 import logging | 8 import logging |
10 import os | 9 import os |
11 import pickle | 10 import pickle |
12 import re | 11 import re |
13 import sys | 12 import sys |
13 import tempfile | |
14 | 14 |
15 from pylib import cmd_helper | 15 from pylib import cmd_helper |
16 from pylib import constants | 16 from pylib import constants |
17 from pylib.device import device_utils | |
17 | 18 |
18 sys.path.insert(0, | 19 sys.path.insert(0, |
19 os.path.join(constants.DIR_SOURCE_ROOT, | 20 os.path.join(constants.DIR_SOURCE_ROOT, |
20 'build', 'util', 'lib', 'common')) | 21 'build', 'util', 'lib', 'common')) |
21 | 22 |
22 import unittest_util # pylint: disable=F0401 | 23 import unittest_util # pylint: disable=F0401 |
23 | 24 |
24 # If you change the cached output of proguard, increment this number | 25 # If you change the cached output of proguard, increment this number |
25 PICKLE_FORMAT_VERSION = 1 | 26 PICKLE_FORMAT_VERSION = 2 |
26 | 27 |
27 | 28 |
28 class TestJar(object): | 29 class TestJar(object): |
29 _ANNOTATIONS = frozenset( | 30 _ANNOTATIONS = frozenset( |
30 ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest', 'EnormousTest', | 31 ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest', 'EnormousTest', |
31 'FlakyTest', 'DisabledTest', 'Manual', 'PerfTest', 'HostDrivenTest']) | 32 'FlakyTest', 'DisabledTest', 'Manual', 'PerfTest', 'HostDrivenTest']) |
32 _DEFAULT_ANNOTATION = 'SmallTest' | 33 _DEFAULT_ANNOTATION = 'SmallTest' |
33 _PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$') | 34 _PROGUARD_CLASS_RE = re.compile(r'\s*?- Program class:\s*([\S]+)$') |
35 _PROGUARD_SUPERCLASS_RE = re.compile(r'\s*? Superclass:\s*([\S]+)$') | |
34 _PROGUARD_METHOD_RE = re.compile(r'\s*?- Method:\s*(\S*)[(].*$') | 36 _PROGUARD_METHOD_RE = re.compile(r'\s*?- Method:\s*(\S*)[(].*$') |
35 _PROGUARD_ANNOTATION_RE = re.compile(r'\s*?- Annotation \[L(\S*);\]:$') | 37 _PROGUARD_ANNOTATION_RE = re.compile(r'\s*?- Annotation \[L(\S*);\]:$') |
36 _PROGUARD_ANNOTATION_CONST_RE = ( | 38 _PROGUARD_ANNOTATION_CONST_RE = ( |
37 re.compile(r'\s*?- Constant element value.*$')) | 39 re.compile(r'\s*?- Constant element value.*$')) |
38 _PROGUARD_ANNOTATION_VALUE_RE = re.compile(r'\s*?- \S+? \[(.*)\]$') | 40 _PROGUARD_ANNOTATION_VALUE_RE = re.compile(r'\s*?- \S+? \[(.*)\]$') |
39 | 41 |
40 def __init__(self, jar_path): | 42 def __init__(self, jar_path): |
41 if not os.path.exists(jar_path): | 43 if not os.path.exists(jar_path): |
42 raise Exception('%s not found, please build it' % jar_path) | 44 raise Exception('%s not found, please build it' % jar_path) |
43 | 45 |
44 self._PROGUARD_PATH = os.path.join(constants.ANDROID_SDK_ROOT, | 46 self._PROGUARD_PATH = os.path.join(constants.ANDROID_SDK_ROOT, |
45 'tools/proguard/lib/proguard.jar') | 47 'tools/proguard/lib/proguard.jar') |
46 if not os.path.exists(self._PROGUARD_PATH): | 48 if not os.path.exists(self._PROGUARD_PATH): |
47 self._PROGUARD_PATH = os.path.join(os.environ['ANDROID_BUILD_TOP'], | 49 self._PROGUARD_PATH = os.path.join(os.environ['ANDROID_BUILD_TOP'], |
48 'external/proguard/lib/proguard.jar') | 50 'external/proguard/lib/proguard.jar') |
49 self._jar_path = jar_path | 51 self._jar_path = jar_path |
50 self._annotation_map = collections.defaultdict(list) | |
51 self._pickled_proguard_name = self._jar_path + '-proguard.pickle' | 52 self._pickled_proguard_name = self._jar_path + '-proguard.pickle' |
52 self._test_methods = [] | 53 self._test_methods = {} |
53 if not self._GetCachedProguardData(): | 54 if not self._GetCachedProguardData(): |
54 self._GetProguardData() | 55 self._GetProguardData() |
55 | 56 |
56 def _GetCachedProguardData(self): | 57 def _GetCachedProguardData(self): |
57 if (os.path.exists(self._pickled_proguard_name) and | 58 if (os.path.exists(self._pickled_proguard_name) and |
58 (os.path.getmtime(self._pickled_proguard_name) > | 59 (os.path.getmtime(self._pickled_proguard_name) > |
59 os.path.getmtime(self._jar_path))): | 60 os.path.getmtime(self._jar_path))): |
60 logging.info('Loading cached proguard output from %s', | 61 logging.info('Loading cached proguard output from %s', |
61 self._pickled_proguard_name) | 62 self._pickled_proguard_name) |
62 try: | 63 try: |
63 with open(self._pickled_proguard_name, 'r') as r: | 64 with open(self._pickled_proguard_name, 'r') as r: |
64 d = pickle.loads(r.read()) | 65 d = pickle.loads(r.read()) |
65 if d['VERSION'] == PICKLE_FORMAT_VERSION: | 66 if d['VERSION'] == PICKLE_FORMAT_VERSION: |
66 self._annotation_map = d['ANNOTATION_MAP'] | |
67 self._test_methods = d['TEST_METHODS'] | 67 self._test_methods = d['TEST_METHODS'] |
68 return True | 68 return True |
69 except: | 69 except: |
70 logging.warning('PICKLE_FORMAT_VERSION has changed, ignoring cache') | 70 logging.warning('PICKLE_FORMAT_VERSION has changed, ignoring cache') |
71 return False | 71 return False |
72 | 72 |
73 def _GetProguardData(self): | 73 def _GetProguardData(self): |
74 proguard_output = cmd_helper.GetCmdOutput(['java', '-jar', | 74 logging.info('Retrieving test methods via proguard.') |
75 self._PROGUARD_PATH, | |
76 '-injars', self._jar_path, | |
77 '-dontshrink', | |
78 '-dontoptimize', | |
79 '-dontobfuscate', | |
80 '-dontpreverify', | |
81 '-dump', | |
82 ]).split('\n') | |
83 clazz = None | |
84 method = None | |
85 annotation = None | |
86 has_value = False | |
87 qualified_method = None | |
88 for line in proguard_output: | |
89 m = self._PROGUARD_CLASS_RE.match(line) | |
90 if m: | |
91 clazz = m.group(1).replace('/', '.') # Change package delim. | |
92 annotation = None | |
93 continue | |
94 | 75 |
95 m = self._PROGUARD_METHOD_RE.match(line) | 76 with tempfile.NamedTemporaryFile() as proguard_output: |
96 if m: | 77 cmd_helper.RunCmd(['java', '-jar', |
97 method = m.group(1) | 78 self._PROGUARD_PATH, |
98 annotation = None | 79 '-injars', self._jar_path, |
99 qualified_method = clazz + '#' + method | 80 '-dontshrink', |
100 if method.startswith('test') and clazz.endswith('Test'): | 81 '-dontoptimize', |
101 self._test_methods += [qualified_method] | 82 '-dontobfuscate', |
102 continue | 83 '-dontpreverify', |
84 '-dump', proguard_output.name]) | |
103 | 85 |
104 if not qualified_method: | 86 clazzez = {} |
105 # Ignore non-method annotations. | |
106 continue | |
107 | 87 |
108 m = self._PROGUARD_ANNOTATION_RE.match(line) | 88 annotation = None |
109 if m: | 89 annotation_has_value = False |
110 annotation = m.group(1).split('/')[-1] # Ignore the annotation package. | 90 clazz = None |
111 self._annotation_map[qualified_method].append(annotation) | 91 method = None |
112 has_value = False | 92 |
113 continue | 93 for line in proguard_output: |
114 if annotation: | 94 if len(line) == 0: |
115 if not has_value: | 95 annotation = None |
116 m = self._PROGUARD_ANNOTATION_CONST_RE.match(line) | 96 annotation_has_value = False |
97 method = None | |
98 continue | |
99 | |
100 m = self._PROGUARD_CLASS_RE.match(line) | |
101 if m: | |
102 clazz = m.group(1).replace('/', '.') | |
103 clazzez[clazz] = { | |
104 'methods': {}, | |
105 'annotations': {} | |
106 } | |
107 annotation = None | |
108 annotation_has_value = False | |
109 method = None | |
110 continue | |
111 | |
112 if not clazz: | |
113 continue | |
114 | |
115 m = self._PROGUARD_SUPERCLASS_RE.match(line) | |
116 if m: | |
117 clazzez[clazz]['superclass'] = m.group(1).replace('/', '.') | |
118 continue | |
119 | |
120 if clazz.endswith('Test'): | |
121 m = self._PROGUARD_METHOD_RE.match(line) | |
117 if m: | 122 if m: |
118 has_value = True | 123 method = m.group(1) |
119 else: | 124 clazzez[clazz]['methods'][method] = {'annotations': {}} |
120 m = self._PROGUARD_ANNOTATION_VALUE_RE.match(line) | 125 annotation = None |
121 if m: | 126 annotation_has_value = False |
122 value = m.group(1) | 127 continue |
123 self._annotation_map[qualified_method].append( | 128 |
124 annotation + ':' + value) | 129 m = self._PROGUARD_ANNOTATION_RE.match(line) |
125 has_value = False | 130 if m: |
131 # Ignore the annotation package. | |
132 annotation = m.group(1).split('/')[-1] | |
133 if method: | |
134 clazzez[clazz]['methods'][method]['annotations'][annotation] = None | |
135 else: | |
136 clazzez[clazz]['annotations'][annotation] = None | |
137 continue | |
138 | |
139 if annotation: | |
140 if not annotation_has_value: | |
141 m = self._PROGUARD_ANNOTATION_CONST_RE.match(line) | |
142 annotation_has_value = bool(m) | |
143 else: | |
144 m = self._PROGUARD_ANNOTATION_VALUE_RE.match(line) | |
145 if m: | |
146 if method: | |
147 clazzez[clazz]['methods'][method]['annotations'][annotation] = ( | |
148 m.group(1)) | |
149 else: | |
150 clazzez[clazz]['annotations'][annotation] = m.group(1) | |
151 annotation_has_value = None | |
152 | |
153 test_clazzez = ((n, i) for n, i in clazzez.items() if n.endswith('Test')) | |
Maria
2014/07/25 23:14:33
I thought list comprehensions were usually in [],
jbudorick
2014/07/25 23:25:39
They are. () is a generator expression. Similar id
| |
154 for clazz_name, clazz_info in test_clazzez: | |
155 logging.info('Processing %s' % clazz_name) | |
156 c = clazz_name | |
157 min_sdk_level = None | |
158 max_sdk_level = None | |
159 | |
160 while c in clazzez: | |
161 c_info = clazzez[c] | |
162 if not min_sdk_level: | |
163 min_sdk_level = c_info['annotations'].get('MinAndroidSdkLevel', None) | |
164 if not max_sdk_level: | |
165 max_sdk_level = c_info['annotations'].get('MaxAndroidSdkLevel', None) | |
166 c = c_info.get('superclass', None) | |
167 | |
168 for method_name, method_info in clazz_info['methods'].items(): | |
169 if method_name.startswith('test'): | |
170 qualified_method = '%s#%s' % (clazz_name, method_name) | |
171 method_annotations = [] | |
172 for annotation_name, annotation_value in ( | |
173 method_info['annotations'].items()): | |
174 method_annotations.append(annotation_name) | |
175 if annotation_value: | |
176 method_annotations.append( | |
177 annotation_name + ':' + annotation_value) | |
178 self._test_methods[qualified_method] = { | |
179 'annotations': method_annotations | |
180 } | |
181 if min_sdk_level is not None: | |
182 self._test_methods[qualified_method]['min_sdk_level'] = ( | |
183 int(min_sdk_level)) | |
184 if max_sdk_level: | |
185 self._test_methods[qualified_method]['max_sdk_level'] = ( | |
186 int(max_sdk_level)) | |
126 | 187 |
127 logging.info('Storing proguard output to %s', self._pickled_proguard_name) | 188 logging.info('Storing proguard output to %s', self._pickled_proguard_name) |
128 d = {'VERSION': PICKLE_FORMAT_VERSION, | 189 d = {'VERSION': PICKLE_FORMAT_VERSION, |
129 'ANNOTATION_MAP': self._annotation_map, | |
130 'TEST_METHODS': self._test_methods} | 190 'TEST_METHODS': self._test_methods} |
131 with open(self._pickled_proguard_name, 'w') as f: | 191 with open(self._pickled_proguard_name, 'w') as f: |
132 f.write(pickle.dumps(d)) | 192 f.write(pickle.dumps(d)) |
133 | 193 |
134 def _GetAnnotationMap(self): | |
135 return self._annotation_map | |
136 | 194 |
137 @staticmethod | 195 @staticmethod |
138 def _IsTestMethod(test): | 196 def _IsTestMethod(test): |
139 class_name, method = test.split('#') | 197 class_name, method = test.split('#') |
140 return class_name.endswith('Test') and method.startswith('test') | 198 return class_name.endswith('Test') and method.startswith('test') |
141 | 199 |
142 def GetTestAnnotations(self, test): | 200 def GetTestAnnotations(self, test): |
143 """Returns a list of all annotations for the given |test|. May be empty.""" | 201 """Returns a list of all annotations for the given |test|. May be empty.""" |
144 if not self._IsTestMethod(test): | 202 if not self._IsTestMethod(test) or not test in self._test_methods: |
145 return [] | 203 return [] |
146 return self._GetAnnotationMap()[test] | 204 return self._test_methods[test]['annotations'] |
147 | 205 |
148 @staticmethod | 206 @staticmethod |
149 def _AnnotationsMatchFilters(annotation_filter_list, annotations): | 207 def _AnnotationsMatchFilters(annotation_filter_list, annotations): |
150 """Checks if annotations match any of the filters.""" | 208 """Checks if annotations match any of the filters.""" |
151 if not annotation_filter_list: | 209 if not annotation_filter_list: |
152 return True | 210 return True |
153 for annotation_filter in annotation_filter_list: | 211 for annotation_filter in annotation_filter_list: |
154 filters = annotation_filter.split('=') | 212 filters = annotation_filter.split('=') |
155 if len(filters) == 2: | 213 if len(filters) == 2: |
156 key = filters[0] | 214 key = filters[0] |
157 value_list = filters[1].split(',') | 215 value_list = filters[1].split(',') |
158 for value in value_list: | 216 for value in value_list: |
159 if key + ':' + value in annotations: | 217 if key + ':' + value in annotations: |
160 return True | 218 return True |
161 elif annotation_filter in annotations: | 219 elif annotation_filter in annotations: |
162 return True | 220 return True |
163 return False | 221 return False |
164 | 222 |
165 def GetAnnotatedTests(self, annotation_filter_list): | 223 def GetAnnotatedTests(self, annotation_filter_list): |
166 """Returns a list of all tests that match the given annotation filters.""" | 224 """Returns a list of all tests that match the given annotation filters.""" |
167 return [test for test, annotations in self._GetAnnotationMap().iteritems() | 225 return [test for test, attr in self.GetTestMethods().iteritems() |
168 if self._IsTestMethod(test) and self._AnnotationsMatchFilters( | 226 if self._IsTestMethod(test) and self._AnnotationsMatchFilters( |
169 annotation_filter_list, annotations)] | 227 annotation_filter_list, attr['annotations'])] |
170 | 228 |
171 def GetTestMethods(self): | 229 def GetTestMethods(self): |
172 """Returns a list of all test methods in this apk as Class#testMethod.""" | 230 """Returns a dict of all test methods and relevant attributes. |
231 | |
232 Test methods are retrieved as Class#testMethod. | |
233 """ | |
173 return self._test_methods | 234 return self._test_methods |
174 | 235 |
175 def _GetTestsMissingAnnotation(self): | 236 def _GetTestsMissingAnnotation(self): |
176 """Get a list of test methods with no known annotations.""" | 237 """Get a list of test methods with no known annotations.""" |
177 tests_missing_annotations = [] | 238 tests_missing_annotations = [] |
178 for test_method in self.GetTestMethods(): | 239 for test_method in self.GetTestMethods().iterkeys(): |
179 annotations_ = frozenset(self.GetTestAnnotations(test_method)) | 240 annotations_ = frozenset(self.GetTestAnnotations(test_method)) |
180 if (annotations_.isdisjoint(self._ANNOTATIONS) and | 241 if (annotations_.isdisjoint(self._ANNOTATIONS) and |
181 not self.IsHostDrivenTest(test_method)): | 242 not self.IsHostDrivenTest(test_method)): |
182 tests_missing_annotations.append(test_method) | 243 tests_missing_annotations.append(test_method) |
183 return sorted(tests_missing_annotations) | 244 return sorted(tests_missing_annotations) |
184 | 245 |
246 def _IsTestValidForSdkRange(self, test_name, attached_min_sdk_level, | |
247 attached_max_sdk_level): | |
248 required_min_sdk_level = self.GetTestMethods()[test_name].get( | |
249 'min_sdk_level', None) | |
250 required_max_sdk_level = self.GetTestMethods()[test_name].get( | |
251 'max_sdk_level', None) | |
252 | |
253 if (required_min_sdk_level is not None and | |
254 attached_min_sdk_level < required_min_sdk_level): | |
255 return False | |
256 | |
257 if (required_max_sdk_level is not None and | |
258 attached_max_sdk_level > required_max_sdk_level): | |
259 return False | |
260 | |
261 return True | |
262 | |
185 def GetAllMatchingTests(self, annotation_filter_list, | 263 def GetAllMatchingTests(self, annotation_filter_list, |
186 exclude_annotation_list, test_filter): | 264 exclude_annotation_list, test_filter): |
187 """Get a list of tests matching any of the annotations and the filter. | 265 """Get a list of tests matching any of the annotations and the filter. |
188 | 266 |
189 Args: | 267 Args: |
190 annotation_filter_list: List of test annotations. A test must have at | 268 annotation_filter_list: List of test annotations. A test must have at |
191 least one of these annotations. A test without any annotations is | 269 least one of these annotations. A test without any annotations is |
192 considered to be SmallTest. | 270 considered to be SmallTest. |
193 exclude_annotation_list: List of test annotations. A test must not have | 271 exclude_annotation_list: List of test annotations. A test must not have |
194 any of these annotations. | 272 any of these annotations. |
(...skipping 26 matching lines...) Expand all Loading... | |
221 sanitized_test_names = dict([ | 299 sanitized_test_names = dict([ |
222 (t.split('.')[-1].replace('#', '.'), t) for t in available_tests]) | 300 (t.split('.')[-1].replace('#', '.'), t) for t in available_tests]) |
223 # Filters 'class.test' names and populates |tests| with the corresponding | 301 # Filters 'class.test' names and populates |tests| with the corresponding |
224 # 'package.path.class#test' names. | 302 # 'package.path.class#test' names. |
225 tests = [ | 303 tests = [ |
226 sanitized_test_names[t] for t in unittest_util.FilterTestNames( | 304 sanitized_test_names[t] for t in unittest_util.FilterTestNames( |
227 sanitized_test_names.keys(), test_filter.replace('#', '.'))] | 305 sanitized_test_names.keys(), test_filter.replace('#', '.'))] |
228 else: | 306 else: |
229 tests = available_tests | 307 tests = available_tests |
230 | 308 |
309 # Filter out any tests with SDK level requirements that don't match the set | |
310 # of attached devices. | |
311 sdk_versions = [ | |
312 int(v) for v in | |
313 device_utils.DeviceUtils.parallel().GetProp( | |
314 'ro.build.version.sdk').pGet(None)] | |
315 tests = filter( | |
316 lambda t: self._IsTestValidForSdkRange(t, min(sdk_versions), | |
317 max(sdk_versions)), | |
318 tests) | |
319 | |
231 return tests | 320 return tests |
232 | 321 |
233 @staticmethod | 322 @staticmethod |
234 def IsHostDrivenTest(test): | 323 def IsHostDrivenTest(test): |
235 return 'pythonDrivenTests' in test | 324 return 'pythonDrivenTests' in test |
OLD | NEW |