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 _CreateConfigFile(files, targets): |
18 """Creates the analyzer conflig file, which is used as the input to analyzer. | |
19 See description of analyzer.py for description of the arguments.""" | |
17 f = open('test_file', 'w') | 20 f = open('test_file', 'w') |
18 to_write = {'files': files, 'targets': targets } | 21 to_write = {'files': files, 'targets': 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 _CreateBogusConfigFile(): |
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, build_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_build_targets = set(result['build_targets']) | |
89 if actual_build_targets != build_targets: | |
90 print 'actual build_targets:', actual_build_targets, \ | |
91 '\nexpected build_targets:', build_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): |
89 if test.stdout().find(expected_error_string) == -1: | 132 if test.stdout().find(expected_error_string) == -1: |
90 print 'actual stdout:', test.stdout(), '\nexpected stdout:', \ | 133 print 'actual stdout:', test.stdout(), '\nexpected stdout:', \ |
91 expected_error_string | 134 expected_error_string |
92 test.fail_test() | 135 test.fail_test() |
93 | 136 |
94 | 137 |
95 def EnsureWarning(expected_warning_string): | 138 def EnsureWarning(expected_warning_string): |
96 """Verifies output contains the warning string.""" | 139 """Verifies output contains the warning string.""" |
97 result = _ReadOutputFileContents() | 140 result = _ReadOutputFileContents() |
98 if result.get('warning', '').find(expected_warning_string) == -1: | 141 if result.get('warning', '').find(expected_warning_string) == -1: |
99 print 'actual warning:', result.get('warning', ''), \ | 142 print 'actual warning:', result.get('warning', ''), \ |
100 '\nexpected warning:', expected_warning_string | 143 '\nexpected warning:', expected_warning_string |
101 test.fail_test() | 144 test.fail_test() |
102 | 145 |
103 | |
104 # Verifies config_path must be specified. | 146 # Verifies config_path must be specified. |
105 test.run_gyp('test.gyp') | 147 test.run_gyp('test.gyp') |
106 EnsureStdoutContains('Must specify files to analyze via config_path') | 148 EnsureStdoutContains('Must specify files to analyze via config_path') |
107 | 149 |
108 # Verifies config_path must point to a valid file. | 150 # Verifies config_path must point to a valid file. |
109 test.run_gyp('test.gyp', '-Gconfig_path=bogus_file', | 151 test.run_gyp('test.gyp', '-Gconfig_path=bogus_file', |
110 '-Ganalyzer_output_path=analyzer_output') | 152 '-Ganalyzer_output_path=analyzer_output') |
111 EnsureError('Unable to open file bogus_file') | 153 EnsureError('Unable to open file bogus_file') |
112 | 154 |
113 # Verify get error when bad target is specified. | 155 # Verify get warning when bad target is specified. |
114 _CreateTestFile(['exe2.c'], ['bad_target']) | 156 _CreateConfigFile(['exe2.c'], ['bad_target']) |
115 run_analyzer() | 157 run_analyzer() |
116 EnsureWarning('Unable to find all targets') | 158 EnsureWarning('Unable to find all targets') |
117 | 159 |
118 # Verifies config_path must point to a valid json file. | 160 # Verifies config_path must point to a valid json file. |
119 _CreateBogusTestFile() | 161 _CreateBogusConfigFile() |
120 run_analyzer() | 162 run_analyzer() |
121 EnsureError('Unable to parse config file test_file') | 163 EnsureError('Unable to parse config file test_file') |
122 | 164 |
123 # Trivial test of a source. | 165 # Trivial test of a source. |
124 _CreateTestFile(['foo.c'], []) | 166 _CreateConfigFile(['foo.c'], []) |
125 run_analyzer() | 167 run_analyzer() |
126 EnsureContains(matched=True) | 168 EnsureContains(matched=True, build_targets={'exe'}) |
127 | 169 |
128 # Conditional source that is excluded. | 170 # Conditional source that is excluded. |
129 _CreateTestFile(['conditional_source.c'], []) | 171 _CreateConfigFile(['conditional_source.c'], []) |
130 run_analyzer() | 172 run_analyzer() |
131 EnsureContains(matched=False) | 173 EnsureContains(matched=False) |
132 | 174 |
133 # Conditional source that is included by way of argument. | 175 # Conditional source that is included by way of argument. |
134 _CreateTestFile(['conditional_source.c'], []) | 176 _CreateConfigFile(['conditional_source.c'], []) |
135 run_analyzer('-Dtest_variable=1') | 177 run_analyzer('-Dtest_variable=1') |
136 EnsureContains(matched=True) | 178 EnsureContains(matched=True, build_targets={'exe'}) |
137 | 179 |
138 # Two unknown files. | 180 # Two unknown files. |
139 _CreateTestFile(['unknown1.c', 'unoknow2.cc'], []) | 181 _CreateConfigFile(['unknown1.c', 'unoknow2.cc'], []) |
140 run_analyzer() | 182 run_analyzer() |
141 EnsureContains() | 183 EnsureContains() |
142 | 184 |
143 # Two unknown files. | 185 # Two unknown files. |
144 _CreateTestFile(['unknown1.c', 'subdir/subdir_sourcex.c'], []) | 186 _CreateConfigFile(['unknown1.c', 'subdir/subdir_sourcex.c'], []) |
145 run_analyzer() | 187 run_analyzer() |
146 EnsureContains() | 188 EnsureContains() |
147 | 189 |
148 # Included dependency | 190 # Included dependency |
149 _CreateTestFile(['unknown1.c', 'subdir/subdir_source.c'], []) | 191 _CreateConfigFile(['unknown1.c', 'subdir/subdir_source.c'], []) |
150 run_analyzer() | 192 run_analyzer() |
151 EnsureContains(matched=True) | 193 EnsureContains(matched=True, build_targets={'exe', 'exe3'}) |
152 | 194 |
153 # Included inputs to actions. | 195 # Included inputs to actions. |
154 _CreateTestFile(['action_input.c'], []) | 196 _CreateConfigFile(['action_input.c'], []) |
155 run_analyzer() | 197 run_analyzer() |
156 EnsureContains(matched=True) | 198 EnsureContains(matched=True, build_targets={'exe'}) |
157 | 199 |
158 # Don't consider outputs. | 200 # Don't consider outputs. |
159 _CreateTestFile(['action_output.c'], []) | 201 _CreateConfigFile(['action_output.c'], []) |
160 run_analyzer() | 202 run_analyzer() |
161 EnsureContains(matched=False) | 203 EnsureContains(matched=False) |
162 | 204 |
163 # Rule inputs. | 205 # Rule inputs. |
164 _CreateTestFile(['rule_input.c'], []) | 206 _CreateConfigFile(['rule_input.c'], []) |
165 run_analyzer() | 207 run_analyzer() |
166 EnsureContains(matched=True) | 208 EnsureContains(matched=True, build_targets={'exe'}) |
167 | 209 |
168 # Ignore path specified with PRODUCT_DIR. | 210 # Ignore path specified with PRODUCT_DIR. |
169 _CreateTestFile(['product_dir_input.c'], []) | 211 _CreateConfigFile(['product_dir_input.c'], []) |
170 run_analyzer() | 212 run_analyzer() |
171 EnsureContains(matched=False) | 213 EnsureContains(matched=False) |
172 | 214 |
173 # Path specified via a variable. | 215 # Path specified via a variable. |
174 _CreateTestFile(['subdir/subdir_source2.c'], []) | 216 _CreateConfigFile(['subdir/subdir_source2.c'], []) |
175 run_analyzer() | 217 run_analyzer() |
176 EnsureContains(matched=True) | 218 EnsureContains(matched=True, build_targets={'exe'}) |
177 | 219 |
178 # Verifies paths with // are fixed up correctly. | 220 # Verifies paths with // are fixed up correctly. |
179 _CreateTestFile(['parent_source.c'], []) | 221 _CreateConfigFile(['parent_source.c'], []) |
180 run_analyzer() | 222 run_analyzer() |
181 EnsureContains(matched=True) | 223 EnsureContains(matched=True, build_targets={'exe', 'exe3'}) |
182 | 224 |
183 # Verifies relative paths are resolved correctly. | 225 # Verifies relative paths are resolved correctly. |
184 _CreateTestFile(['subdir/subdir_source.h'], []) | 226 _CreateConfigFile(['subdir/subdir_source.h'], []) |
185 run_analyzer() | 227 run_analyzer() |
186 EnsureContains(matched=True) | 228 EnsureContains(matched=True, build_targets={'exe'}) |
187 | 229 |
188 # Various permutations when passing in targets. | 230 # Various permutations when passing in targets. |
189 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe', 'exe3']) | 231 _CreateConfigFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe', 'exe3']) |
190 run_analyzer() | 232 run_analyzer() |
191 EnsureContains(matched=True, targets={'exe3'}) | 233 EnsureContains(matched=True, targets={'exe3'}, build_targets={'exe2', 'exe3'}) |
192 | 234 |
193 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe']) | 235 _CreateConfigFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe']) |
194 run_analyzer() | 236 run_analyzer() |
195 EnsureContains(matched=True) | 237 EnsureContains(matched=True, build_targets={'exe2', 'exe3'}) |
196 | 238 |
197 # Verifies duplicates are ignored. | 239 # Verifies duplicates are ignored. |
198 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe', 'exe']) | 240 _CreateConfigFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe', 'exe']) |
199 run_analyzer() | 241 run_analyzer() |
200 EnsureContains(matched=True) | 242 EnsureContains(matched=True, build_targets={'exe2', 'exe3'}) |
201 | 243 |
202 _CreateTestFile(['exe2.c'], ['exe']) | 244 _CreateConfigFile(['exe2.c'], ['exe']) |
203 run_analyzer() | 245 run_analyzer() |
204 EnsureContains(matched=True) | 246 EnsureContains(matched=True, build_targets={'exe2'}) |
205 | 247 |
206 _CreateTestFile(['exe2.c'], []) | 248 _CreateConfigFile(['exe2.c'], []) |
207 run_analyzer() | 249 run_analyzer() |
208 EnsureContains(matched=True) | 250 EnsureContains(matched=True, build_targets={'exe2'}) |
209 | 251 |
210 _CreateTestFile(['subdir/subdir2b_source.c', 'exe2.c'], []) | 252 _CreateConfigFile(['subdir/subdir2b_source.c', 'exe2.c'], []) |
211 run_analyzer() | 253 run_analyzer() |
212 EnsureContains(matched=True) | 254 EnsureContains(matched=True, build_targets={'exe2', 'exe3'}) |
213 | 255 |
214 _CreateTestFile(['exe2.c'], []) | 256 _CreateConfigFile(['subdir/subdir2b_source.c'], ['exe3']) |
215 run_analyzer() | 257 run_analyzer() |
216 EnsureContains(matched=True) | 258 EnsureContains(matched=True, targets={'exe3'}, build_targets={'exe3'}) |
259 | |
260 _CreateConfigFile(['exe2.c'], []) | |
261 run_analyzer() | |
262 EnsureContains(matched=True, build_targets={'exe2'}) | |
263 | |
264 _CreateConfigFile(['foo.c'], []) | |
265 run_analyzer() | |
266 EnsureContains(matched=True, build_targets={'exe'}) | |
217 | 267 |
218 # Assertions when modifying build (gyp/gypi) files, especially when said files | 268 # Assertions when modifying build (gyp/gypi) files, especially when said files |
219 # are included. | 269 # are included. |
220 _CreateTestFile(['subdir2/d.cc'], ['exe', 'exe2', 'foo', 'exe3']) | 270 _CreateConfigFile(['subdir2/d.cc'], ['exe', 'exe2', 'foo', 'exe3']) |
221 run_analyzer2() | 271 run_analyzer2() |
222 EnsureContains(matched=True, targets={'exe', 'foo'}) | 272 EnsureContains(matched=True, targets={'exe', 'foo'}, build_targets={'exe'}) |
223 | 273 |
224 _CreateTestFile(['subdir2/subdir.includes.gypi'], | 274 _CreateConfigFile(['subdir2/subdir.includes.gypi'], |
225 ['exe', 'exe2', 'foo', 'exe3']) | 275 ['exe', 'exe2', 'foo', 'exe3']) |
226 run_analyzer2() | 276 run_analyzer2() |
227 EnsureContains(matched=True, targets={'exe', 'foo'}) | 277 EnsureContains(matched=True, targets={'exe', 'foo'}, build_targets={'exe'}) |
228 | 278 |
229 _CreateTestFile(['subdir2/subdir.gyp'], ['exe', 'exe2', 'foo', 'exe3']) | 279 _CreateConfigFile(['subdir2/subdir.gyp'], ['exe', 'exe2', 'foo', 'exe3']) |
230 run_analyzer2() | 280 run_analyzer2() |
231 EnsureContains(matched=True, targets={'exe', 'foo'}) | 281 EnsureContains(matched=True, targets={'exe', 'foo'}, build_targets={'exe'}) |
232 | 282 |
233 _CreateTestFile(['test2.includes.gypi'], ['exe', 'exe2', 'foo', 'exe3']) | 283 _CreateConfigFile(['test2.includes.gypi'], ['exe', 'exe2', 'foo', 'exe3']) |
234 run_analyzer2() | 284 run_analyzer2() |
235 EnsureContains(matched=True, targets={'exe', 'exe2', 'exe3'}) | 285 EnsureContains(matched=True, targets={'exe', 'exe2', 'exe3'}, |
286 build_targets={'exe', 'exe2', 'exe3'}) | |
236 | 287 |
237 # Verify modifying a file included makes all targets dirty. | 288 # Verify modifying a file included makes all targets dirty. |
238 _CreateTestFile(['common.gypi'], ['exe', 'exe2', 'foo', 'exe3']) | 289 _CreateConfigFile(['common.gypi'], ['exe', 'exe2', 'foo', 'exe3']) |
239 run_analyzer2('-Icommon.gypi') | 290 run_analyzer2('-Icommon.gypi') |
240 EnsureContains(matched=True, targets={'exe', 'foo', 'exe2', 'exe3'}) | 291 EnsureMatchedAll({'exe', 'exe2', 'foo', 'exe3'}) |
292 | |
293 # Assertions from test3.gyp. | |
scottmg
2014/08/18 19:48:16
I found this helpful to try to understand if these
sky
2014/08/18 20:06:33
Very cool! I had to draw pictures.
| |
294 _CreateConfigFile(['d.c', 'f.c'], ['a']) | |
295 run_analyzer3() | |
296 EnsureContains(matched=True, targets={'a'}, build_targets={'a', 'b'}) | |
297 | |
298 _CreateConfigFile(['f.c'], ['a']) | |
299 run_analyzer3() | |
300 EnsureContains(matched=True, targets={'a'}, build_targets={'a', 'b'}) | |
301 | |
302 _CreateConfigFile(['f.c'], []) | |
303 run_analyzer3() | |
304 EnsureContains(matched=True, build_targets={'a', 'b'}) | |
305 | |
306 _CreateConfigFile(['c.c', 'e.c'], []) | |
307 run_analyzer3() | |
308 EnsureContains(matched=True, build_targets={'a', 'b'}) | |
309 | |
310 _CreateConfigFile(['d.c'], ['a']) | |
311 run_analyzer3() | |
312 EnsureContains(matched=True, targets={'a'}, build_targets={'a', 'b'}) | |
313 | |
314 _CreateConfigFile(['a.c'], ['a', 'b']) | |
315 run_analyzer3() | |
316 EnsureContains(matched=True, targets={'a'}, build_targets={'a'}) | |
317 | |
318 _CreateConfigFile(['a.c'], ['a', 'b']) | |
319 run_analyzer3() | |
320 EnsureContains(matched=True, targets={'a'}, build_targets={'a'}) | |
321 | |
322 _CreateConfigFile(['d.c'], ['a', 'b']) | |
323 run_analyzer3() | |
324 EnsureContains(matched=True, targets={'a', 'b'}, build_targets={'a', 'b'}) | |
325 | |
326 _CreateConfigFile(['f.c'], ['a']) | |
327 run_analyzer3() | |
328 EnsureContains(matched=True, targets={'a'}, build_targets={'a', 'b'}) | |
329 | |
330 _CreateConfigFile(['a.c'], ['a']) | |
331 run_analyzer3() | |
332 EnsureContains(matched=True, targets={'a'}, build_targets={'a'}) | |
333 | |
334 _CreateConfigFile(['a.c'], []) | |
335 run_analyzer3() | |
336 EnsureContains(matched=True, build_targets={'a'}) | |
337 | |
338 _CreateConfigFile(['d.c'], []) | |
339 run_analyzer3() | |
340 EnsureContains(matched=True, build_targets={'a', 'b'}) | |
341 | |
342 # Assertions around test4.gyp. | |
343 _CreateConfigFile(['f.c'], []) | |
344 run_analyzer4() | |
345 EnsureContains(matched=True, build_targets={'e'}) | |
346 | |
347 _CreateConfigFile(['d.c'], []) | |
348 run_analyzer4() | |
349 EnsureContains(matched=True, build_targets={'a'}) | |
350 | |
351 _CreateConfigFile(['i.c'], []) | |
352 run_analyzer4() | |
353 EnsureContains(matched=True, build_targets={'h'}) | |
241 | 354 |
242 test.pass_test() | 355 test.pass_test() |
OLD | NEW |