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