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