OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2014 Google Inc. All rights reserved. | 2 # Copyright (c) 2014 Google Inc. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Tests for analyzer | 6 """Tests for analyzer |
7 """ | 7 """ |
8 | 8 |
9 import json | 9 import json |
10 import TestGyp | 10 import TestGyp |
11 | 11 |
12 found = 'Found dependency' | 12 found = 'Found dependency' |
13 found_all = 'Found dependency (all)' | |
13 not_found = 'No dependencies' | 14 not_found = 'No dependencies' |
14 | 15 |
15 | 16 |
16 def _CreateTestFile(files, targets): | 17 def _CreateTestFile(files, targets, ignore_targets=[]): |
17 f = open('test_file', 'w') | 18 f = open('test_file', 'w') |
scottmg
2014/08/15 21:30:24
could you add a doc comment here? I find it confus
sky
2014/08/15 22:39:37
Done.
| |
18 to_write = {'files': files, 'targets': targets } | 19 to_write = {'files': files, |
20 'targets': targets, | |
21 'ignore_targets': ignore_targets } | |
19 json.dump(to_write, f) | 22 json.dump(to_write, f) |
20 f.close() | 23 f.close() |
21 | 24 |
22 | 25 |
23 def _CreateBogusTestFile(): | 26 def _CreateBogusTestFile(): |
24 f = open('test_file','w') | 27 f = open('test_file','w') |
25 f.write('bogus') | 28 f.write('bogus') |
26 f.close() | 29 f.close() |
27 | 30 |
28 | 31 |
29 def _ReadOutputFileContents(): | 32 def _ReadOutputFileContents(): |
30 f = open('analyzer_output', 'r') | 33 f = open('analyzer_output', 'r') |
31 result = json.load(f) | 34 result = json.load(f) |
32 f.close() | 35 f.close() |
33 return result | 36 return result |
34 | 37 |
35 | 38 |
36 # NOTE: this would be clearer if it subclassed TestGypCustom, but that trips | 39 # NOTE: this would be clearer if it subclassed TestGypCustom, but that trips |
37 # over a bug in pylint (E1002). | 40 # over a bug in pylint (E1002). |
38 test = TestGyp.TestGypCustom(format='analyzer') | 41 test = TestGyp.TestGypCustom(format='analyzer') |
39 | 42 |
43 def CommonArgs(): | |
44 return ('-Gconfig_path=test_file', | |
45 '-Ganalyzer_output_path=analyzer_output') | |
46 | |
40 | 47 |
41 def run_analyzer(*args, **kw): | 48 def run_analyzer(*args, **kw): |
42 """Runs the test specifying a particular config and output path.""" | 49 """Runs the test specifying a particular config and output path.""" |
43 args += ('-Gconfig_path=test_file', | 50 args += CommonArgs() |
44 '-Ganalyzer_output_path=analyzer_output') | |
45 test.run_gyp('test.gyp', *args, **kw) | 51 test.run_gyp('test.gyp', *args, **kw) |
46 | 52 |
47 | 53 |
48 def run_analyzer2(*args, **kw): | 54 def run_analyzer2(*args, **kw): |
49 """Runs the test specifying a particular config and output path.""" | 55 """Same as run_analyzer(), but passes in test2.gyp instead of test.gyp.""" |
50 args += ('-Gconfig_path=test_file', | 56 args += CommonArgs() |
51 '-Ganalyzer_output_path=analyzer_output') | |
52 test.run_gyp('test2.gyp', *args, **kw) | 57 test.run_gyp('test2.gyp', *args, **kw) |
53 | 58 |
54 | 59 |
55 def EnsureContains(targets=set(), matched=False): | 60 def run_analyzer3(*args, **kw): |
61 """Same as run_analyzer(), but passes in test3.gyp instead of test.gyp.""" | |
62 args += CommonArgs() | |
63 test.run_gyp('test3.gyp', *args, **kw) | |
64 | |
65 | |
66 def run_analyzer4(*args, **kw): | |
67 """Same as run_analyzer(), but passes in test3.gyp instead of test.gyp.""" | |
68 args += CommonArgs() | |
69 test.run_gyp('test4.gyp', *args, **kw) | |
70 | |
71 | |
72 def EnsureContains(targets=set(), matched=False, affected_targets=set()): | |
56 """Verifies output contains |targets|.""" | 73 """Verifies output contains |targets|.""" |
57 result = _ReadOutputFileContents() | 74 result = _ReadOutputFileContents() |
58 if result.get('error', None): | 75 if result.get('error', None): |
59 print 'unexpected error', result.get('error') | 76 print 'unexpected error', result.get('error') |
60 test.fail_test() | 77 test.fail_test() |
61 | 78 |
62 if result.get('warning', None): | 79 if result.get('warning', None): |
63 print 'unexpected warning', result.get('warning') | 80 print 'unexpected warning', result.get('warning') |
64 test.fail_test() | 81 test.fail_test() |
65 | 82 |
66 actual_targets = set(result['targets']) | 83 actual_targets = set(result['targets']) |
67 if actual_targets != targets: | 84 if actual_targets != targets: |
68 print 'actual targets:', actual_targets, '\nexpected targets:', targets | 85 print 'actual targets:', actual_targets, '\nexpected targets:', targets |
69 test.fail_test() | 86 test.fail_test() |
70 | 87 |
88 actual_affected_targets = set(result['affected_targets']) | |
89 if actual_affected_targets != affected_targets: | |
90 print 'actual affected_targets:', actual_affected_targets, \ | |
91 '\nexpected affected_targets:', affected_targets | |
92 test.fail_test() | |
93 | |
71 if matched and result['status'] != found: | 94 if matched and result['status'] != found: |
72 print 'expected', found, 'got', result['status'] | 95 print 'expected', found, 'got', result['status'] |
73 test.fail_test() | 96 test.fail_test() |
74 elif not matched and result['status'] != not_found: | 97 elif not matched and result['status'] != not_found: |
75 print 'expected', not_found, 'got', result['status'] | 98 print 'expected', not_found, 'got', result['status'] |
76 test.fail_test() | 99 test.fail_test() |
77 | 100 |
78 | 101 |
102 def EnsureMatchedAll(targets): | |
103 result = _ReadOutputFileContents() | |
104 if result.get('error', None): | |
105 print 'unexpected error', result.get('error') | |
106 test.fail_test() | |
107 | |
108 if result.get('warning', None): | |
109 print 'unexpected warning', result.get('warning') | |
110 test.fail_test() | |
111 | |
112 if result['status'] != found_all: | |
113 print 'expected', found_all, 'got', result['status'] | |
114 test.fail_test() | |
115 | |
116 actual_targets = set(result['targets']) | |
117 if actual_targets != targets: | |
118 print 'actual targets:', actual_targets, '\nexpected targets:', targets | |
119 test.fail_test() | |
120 | |
121 | |
79 def EnsureError(expected_error_string): | 122 def EnsureError(expected_error_string): |
80 """Verifies output contains the error string.""" | 123 """Verifies output contains the error string.""" |
81 result = _ReadOutputFileContents() | 124 result = _ReadOutputFileContents() |
82 if result.get('error', '').find(expected_error_string) == -1: | 125 if result.get('error', '').find(expected_error_string) == -1: |
83 print 'actual error:', result.get('error', ''), '\nexpected error:', \ | 126 print 'actual error:', result.get('error', ''), '\nexpected error:', \ |
84 expected_error_string | 127 expected_error_string |
85 test.fail_test() | 128 test.fail_test() |
86 | 129 |
87 | 130 |
88 def EnsureStdoutContains(expected_error_string): | 131 def EnsureStdoutContains(expected_error_string): |
(...skipping 14 matching lines...) Expand all Loading... | |
103 | 146 |
104 # Verifies config_path must be specified. | 147 # Verifies config_path must be specified. |
105 test.run_gyp('test.gyp') | 148 test.run_gyp('test.gyp') |
106 EnsureStdoutContains('Must specify files to analyze via config_path') | 149 EnsureStdoutContains('Must specify files to analyze via config_path') |
107 | 150 |
108 # Verifies config_path must point to a valid file. | 151 # Verifies config_path must point to a valid file. |
109 test.run_gyp('test.gyp', '-Gconfig_path=bogus_file', | 152 test.run_gyp('test.gyp', '-Gconfig_path=bogus_file', |
110 '-Ganalyzer_output_path=analyzer_output') | 153 '-Ganalyzer_output_path=analyzer_output') |
111 EnsureError('Unable to open file bogus_file') | 154 EnsureError('Unable to open file bogus_file') |
112 | 155 |
113 # Verify get error when bad target is specified. | 156 # Verify get warning when bad target is specified. |
114 _CreateTestFile(['exe2.c'], ['bad_target']) | 157 _CreateTestFile(['exe2.c'], ['bad_target']) |
115 run_analyzer() | 158 run_analyzer() |
116 EnsureWarning('Unable to find all targets') | 159 EnsureWarning('Unable to find all targets') |
117 | 160 |
118 # Verifies config_path must point to a valid json file. | 161 # Verifies config_path must point to a valid json file. |
119 _CreateBogusTestFile() | 162 _CreateBogusTestFile() |
120 run_analyzer() | 163 run_analyzer() |
121 EnsureError('Unable to parse config file test_file') | 164 EnsureError('Unable to parse config file test_file') |
122 | 165 |
123 # Trivial test of a source. | 166 # Trivial test of a source. |
124 _CreateTestFile(['foo.c'], []) | 167 _CreateTestFile(['foo.c'], []) |
125 run_analyzer() | 168 run_analyzer() |
126 EnsureContains(matched=True) | 169 EnsureContains(matched=True, affected_targets={'all'}) |
127 | 170 |
128 # Conditional source that is excluded. | 171 # Conditional source that is excluded. |
129 _CreateTestFile(['conditional_source.c'], []) | 172 _CreateTestFile(['conditional_source.c'], []) |
130 run_analyzer() | 173 run_analyzer() |
131 EnsureContains(matched=False) | 174 EnsureContains(matched=False) |
132 | 175 |
133 # Conditional source that is included by way of argument. | 176 # Conditional source that is included by way of argument. |
134 _CreateTestFile(['conditional_source.c'], []) | 177 _CreateTestFile(['conditional_source.c'], []) |
135 run_analyzer('-Dtest_variable=1') | 178 run_analyzer('-Dtest_variable=1') |
136 EnsureContains(matched=True) | 179 EnsureContains(matched=True, affected_targets={'all'}) |
137 | 180 |
138 # Two unknown files. | 181 # Two unknown files. |
139 _CreateTestFile(['unknown1.c', 'unoknow2.cc'], []) | 182 _CreateTestFile(['unknown1.c', 'unoknow2.cc'], []) |
140 run_analyzer() | 183 run_analyzer() |
141 EnsureContains() | 184 EnsureContains() |
142 | 185 |
143 # Two unknown files. | 186 # Two unknown files. |
144 _CreateTestFile(['unknown1.c', 'subdir/subdir_sourcex.c'], []) | 187 _CreateTestFile(['unknown1.c', 'subdir/subdir_sourcex.c'], []) |
145 run_analyzer() | 188 run_analyzer() |
146 EnsureContains() | 189 EnsureContains() |
147 | 190 |
148 # Included dependency | 191 # Included dependency |
149 _CreateTestFile(['unknown1.c', 'subdir/subdir_source.c'], []) | 192 _CreateTestFile(['unknown1.c', 'subdir/subdir_source.c'], []) |
150 run_analyzer() | 193 run_analyzer() |
151 EnsureContains(matched=True) | 194 EnsureContains(matched=True, affected_targets={'exe', 'exe3'}) |
152 | 195 |
153 # Included inputs to actions. | 196 # Included inputs to actions. |
154 _CreateTestFile(['action_input.c'], []) | 197 _CreateTestFile(['action_input.c'], []) |
155 run_analyzer() | 198 run_analyzer() |
156 EnsureContains(matched=True) | 199 EnsureContains(matched=True, affected_targets={'all'}) |
157 | 200 |
158 # Don't consider outputs. | 201 # Don't consider outputs. |
159 _CreateTestFile(['action_output.c'], []) | 202 _CreateTestFile(['action_output.c'], []) |
160 run_analyzer() | 203 run_analyzer() |
161 EnsureContains(matched=False) | 204 EnsureContains(matched=False) |
162 | 205 |
163 # Rule inputs. | 206 # Rule inputs. |
164 _CreateTestFile(['rule_input.c'], []) | 207 _CreateTestFile(['rule_input.c'], []) |
165 run_analyzer() | 208 run_analyzer() |
166 EnsureContains(matched=True) | 209 EnsureContains(matched=True, affected_targets={'all'}) |
167 | 210 |
168 # Ignore path specified with PRODUCT_DIR. | 211 # Ignore path specified with PRODUCT_DIR. |
169 _CreateTestFile(['product_dir_input.c'], []) | 212 _CreateTestFile(['product_dir_input.c'], []) |
170 run_analyzer() | 213 run_analyzer() |
171 EnsureContains(matched=False) | 214 EnsureContains(matched=False) |
172 | 215 |
173 # Path specified via a variable. | 216 # Path specified via a variable. |
174 _CreateTestFile(['subdir/subdir_source2.c'], []) | 217 _CreateTestFile(['subdir/subdir_source2.c'], []) |
175 run_analyzer() | 218 run_analyzer() |
176 EnsureContains(matched=True) | 219 EnsureContains(matched=True, affected_targets={'all'}) |
177 | 220 |
178 # Verifies paths with // are fixed up correctly. | 221 # Verifies paths with // are fixed up correctly. |
179 _CreateTestFile(['parent_source.c'], []) | 222 _CreateTestFile(['parent_source.c'], []) |
180 run_analyzer() | 223 run_analyzer() |
181 EnsureContains(matched=True) | 224 EnsureContains(matched=True, affected_targets={'exe', 'exe3'}) |
182 | 225 |
183 # Verifies relative paths are resolved correctly. | 226 # Verifies relative paths are resolved correctly. |
184 _CreateTestFile(['subdir/subdir_source.h'], []) | 227 _CreateTestFile(['subdir/subdir_source.h'], []) |
185 run_analyzer() | 228 run_analyzer() |
186 EnsureContains(matched=True) | 229 EnsureContains(matched=True, affected_targets={'exe'}) |
187 | 230 |
188 # Various permutations when passing in targets. | 231 # Various permutations when passing in targets. |
189 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe', 'exe3']) | 232 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe', 'exe3']) |
190 run_analyzer() | 233 run_analyzer() |
191 EnsureContains(matched=True, targets={'exe3'}) | 234 EnsureContains(matched=True, targets={'exe3'}, |
235 affected_targets={'exe2', 'exe3'}) | |
192 | 236 |
193 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe']) | 237 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe']) |
194 run_analyzer() | 238 run_analyzer() |
195 EnsureContains(matched=True) | 239 EnsureContains(matched=True, affected_targets={'exe2', 'subdir2a'}) |
196 | 240 |
197 # Verifies duplicates are ignored. | 241 # Verifies duplicates are ignored. |
198 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe', 'exe']) | 242 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe', 'exe']) |
199 run_analyzer() | 243 run_analyzer() |
200 EnsureContains(matched=True) | 244 EnsureContains(matched=True, affected_targets={'exe2', 'subdir2a'}) |
201 | 245 |
202 _CreateTestFile(['exe2.c'], ['exe']) | 246 _CreateTestFile(['exe2.c'], ['exe']) |
203 run_analyzer() | 247 run_analyzer() |
204 EnsureContains(matched=True) | 248 EnsureContains(matched=True, affected_targets={'exe2'}) |
205 | 249 |
206 _CreateTestFile(['exe2.c'], []) | 250 _CreateTestFile(['exe2.c'], []) |
207 run_analyzer() | 251 run_analyzer() |
208 EnsureContains(matched=True) | 252 EnsureContains(matched=True, affected_targets={'exe2'}) |
209 | 253 |
210 _CreateTestFile(['subdir/subdir2b_source.c', 'exe2.c'], []) | 254 _CreateTestFile(['subdir/subdir2b_source.c', 'exe2.c'], []) |
211 run_analyzer() | 255 run_analyzer() |
212 EnsureContains(matched=True) | 256 EnsureContains(matched=True, affected_targets={'exe2', 'subdir2a'}) |
257 | |
258 _CreateTestFile(['subdir/subdir2b_source.c'], ['exe3']) | |
259 run_analyzer() | |
260 EnsureContains(matched=True, targets={'exe3'}, affected_targets={'exe3'}) | |
213 | 261 |
214 _CreateTestFile(['exe2.c'], []) | 262 _CreateTestFile(['exe2.c'], []) |
215 run_analyzer() | 263 run_analyzer() |
216 EnsureContains(matched=True) | 264 EnsureContains(matched=True, affected_targets={'exe2'}) |
265 | |
266 _CreateTestFile(['foo.c'], [], ignore_targets=['all']) | |
267 run_analyzer() | |
268 EnsureContains(matched=True, affected_targets={'exe'}) | |
217 | 269 |
218 # Assertions when modifying build (gyp/gypi) files, especially when said files | 270 # Assertions when modifying build (gyp/gypi) files, especially when said files |
219 # are included. | 271 # are included. |
220 _CreateTestFile(['subdir2/d.cc'], ['exe', 'exe2', 'foo', 'exe3']) | 272 _CreateTestFile(['subdir2/d.cc'], ['exe', 'exe2', 'foo', 'exe3']) |
221 run_analyzer2() | 273 run_analyzer2() |
222 EnsureContains(matched=True, targets={'exe', 'foo'}) | 274 EnsureContains(matched=True, targets={'exe', 'foo'}, |
275 affected_targets={'exe'}) | |
223 | 276 |
224 _CreateTestFile(['subdir2/subdir.includes.gypi'], | 277 _CreateTestFile(['subdir2/subdir.includes.gypi'], |
225 ['exe', 'exe2', 'foo', 'exe3']) | 278 ['exe', 'exe2', 'foo', 'exe3']) |
226 run_analyzer2() | 279 run_analyzer2() |
227 EnsureContains(matched=True, targets={'exe', 'foo'}) | 280 EnsureContains(matched=True, targets={'exe', 'foo'}, |
281 affected_targets={'exe'}) | |
228 | 282 |
229 _CreateTestFile(['subdir2/subdir.gyp'], ['exe', 'exe2', 'foo', 'exe3']) | 283 _CreateTestFile(['subdir2/subdir.gyp'], ['exe', 'exe2', 'foo', 'exe3']) |
230 run_analyzer2() | 284 run_analyzer2() |
231 EnsureContains(matched=True, targets={'exe', 'foo'}) | 285 EnsureContains(matched=True, targets={'exe', 'foo'}, |
286 affected_targets={'exe'}) | |
232 | 287 |
233 _CreateTestFile(['test2.includes.gypi'], ['exe', 'exe2', 'foo', 'exe3']) | 288 _CreateTestFile(['test2.includes.gypi'], ['exe', 'exe2', 'foo', 'exe3']) |
234 run_analyzer2() | 289 run_analyzer2() |
235 EnsureContains(matched=True, targets={'exe', 'exe2', 'exe3'}) | 290 EnsureContains(matched=True, targets={'exe', 'exe2', 'exe3'}, |
291 affected_targets={'exe', 'exe2', 'exe3'}) | |
236 | 292 |
237 # Verify modifying a file included makes all targets dirty. | 293 # Verify modifying a file included makes all targets dirty. |
238 _CreateTestFile(['common.gypi'], ['exe', 'exe2', 'foo', 'exe3']) | 294 _CreateTestFile(['common.gypi'], ['exe', 'exe2', 'foo', 'exe3']) |
239 run_analyzer2('-Icommon.gypi') | 295 run_analyzer2('-Icommon.gypi') |
240 EnsureContains(matched=True, targets={'exe', 'foo', 'exe2', 'exe3'}) | 296 EnsureMatchedAll({'exe', 'exe2', 'foo', 'exe3'}) |
297 | |
298 # Assertions from test3.gyp. | |
299 _CreateTestFile(['d.c', 'f.c'], ['a']) | |
300 run_analyzer3() | |
301 EnsureContains(matched=True, targets={'a'}, affected_targets={'a', 'b'}) | |
scottmg
2014/08/15 21:30:24
Huh, I hadn't seen set notation before https://doc
| |
302 | |
303 _CreateTestFile(['f.c'], ['a']) | |
304 run_analyzer3() | |
305 EnsureContains(matched=True, targets={'a'}, affected_targets={'a'}) | |
306 | |
307 _CreateTestFile(['f.c'], []) | |
308 run_analyzer3() | |
309 EnsureContains(matched=True, affected_targets={'d'}) | |
scottmg
2014/08/15 21:30:24
could you explain this one? f.c changed, so 'f' is
sky
2014/08/15 22:39:37
My assumption, which could certainly be bogus, is
| |
310 | |
311 _CreateTestFile(['c.c', 'e.c'], []) | |
312 run_analyzer3() | |
313 EnsureContains(matched=True, affected_targets={'a', 'b'}) | |
314 | |
315 _CreateTestFile(['d.c'], ['a']) | |
316 run_analyzer3() | |
317 EnsureContains(matched=True, targets={'a'}, affected_targets={'a', 'b'}) | |
318 | |
319 _CreateTestFile(['a.c'], ['a', 'b'], ignore_targets=['all']) | |
320 run_analyzer3() | |
321 EnsureContains(matched=True, targets={'a'}, affected_targets={'a'}) | |
322 | |
323 _CreateTestFile(['a.c'], ['a', 'b']) | |
324 run_analyzer3() | |
325 EnsureContains(matched=True, targets={'a'}, affected_targets={'all'}) | |
326 | |
327 _CreateTestFile(['d.c'], ['a', 'b']) | |
328 run_analyzer3() | |
329 EnsureContains(matched=True, targets={'a', 'b'}, affected_targets={'a', 'b'}) | |
330 | |
331 _CreateTestFile(['f.c'], ['a']) | |
332 run_analyzer3() | |
333 EnsureContains(matched=True, targets={'a'}, affected_targets={'a'}) | |
334 | |
335 _CreateTestFile(['a.c'], ['a'], ignore_targets=['all']) | |
336 run_analyzer3() | |
337 EnsureContains(matched=True, targets={'a'}, affected_targets={'a'}) | |
338 | |
339 _CreateTestFile(['a.c'], [], ignore_targets=['all']) | |
340 run_analyzer3() | |
341 EnsureContains(matched=True, affected_targets={'a'}) | |
342 | |
343 _CreateTestFile(['d.c'], []) | |
344 run_analyzer3() | |
345 EnsureContains(matched=True, affected_targets={'a', 'b'}) | |
346 | |
347 _CreateTestFile(['f.c'], []) | |
scottmg
2014/08/15 21:30:24
comment here that we switched to test4.gyp
sky
2014/08/15 22:39:37
Done.
| |
348 run_analyzer4() | |
349 EnsureContains(matched=True, affected_targets={'e'}) | |
350 | |
351 _CreateTestFile(['d.c'], []) | |
352 run_analyzer4() | |
353 EnsureContains(matched=True, affected_targets={'c'}) | |
241 | 354 |
242 test.pass_test() | 355 test.pass_test() |
OLD | NEW |