| OLD | NEW |
| (Empty) |
| 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 # TODO(sky): when done migrating recipes rename to gyptest-analyzer and nuke | |
| 13 # existing gyptest-analyzer. | |
| 14 | |
| 15 found = 'Found dependency' | |
| 16 not_found = 'No dependencies' | |
| 17 | |
| 18 def _CreateTestFile(files, targets): | |
| 19 f = open('test_file', 'w') | |
| 20 to_write = {'files': files, 'targets': targets } | |
| 21 json.dump(to_write, f) | |
| 22 f.close() | |
| 23 | |
| 24 def _CreateBogusTestFile(): | |
| 25 f = open('test_file','w') | |
| 26 f.write('bogus') | |
| 27 f.close() | |
| 28 | |
| 29 def _ReadOutputFileContents(): | |
| 30 f = open('analyzer_output', 'r') | |
| 31 result = json.load(f) | |
| 32 f.close() | |
| 33 return result | |
| 34 | |
| 35 # NOTE: this would be clearer if it subclassed TestGypCustom, but that trips | |
| 36 # over a bug in pylint (E1002). | |
| 37 test = TestGyp.TestGypCustom(format='analyzer') | |
| 38 | |
| 39 def run_analyzer(*args, **kw): | |
| 40 """Runs the test specifying a particular config and output path.""" | |
| 41 args += ('-Gconfig_path=test_file', | |
| 42 '-Ganalyzer_output_path=analyzer_output') | |
| 43 test.run_gyp('test.gyp', *args, **kw) | |
| 44 | |
| 45 def run_analyzer2(*args, **kw): | |
| 46 """Runs the test specifying a particular config and output path.""" | |
| 47 args += ('-Gconfig_path=test_file', | |
| 48 '-Ganalyzer_output_path=analyzer_output') | |
| 49 test.run_gyp('test2.gyp', *args, **kw) | |
| 50 | |
| 51 def EnsureContains(targets=set(), matched=False): | |
| 52 """Verifies output contains |targets|.""" | |
| 53 result = _ReadOutputFileContents() | |
| 54 if result.get('error', None): | |
| 55 print 'unexpected error', result.get('error') | |
| 56 test.fail_test() | |
| 57 | |
| 58 if result.get('warning', None): | |
| 59 print 'unexpected warning', result.get('warning') | |
| 60 test.fail_test() | |
| 61 | |
| 62 actual_targets = set(result['targets']) | |
| 63 if actual_targets != targets: | |
| 64 print 'actual targets:', actual_targets, '\nexpected targets:', targets | |
| 65 test.fail_test() | |
| 66 | |
| 67 if matched and result['status'] != found: | |
| 68 print 'expected', found, 'got', result['status'] | |
| 69 test.fail_test() | |
| 70 elif not matched and result['status'] != not_found: | |
| 71 print 'expected', not_found, 'got', result['status'] | |
| 72 test.fail_test() | |
| 73 | |
| 74 def EnsureError(expected_error_string): | |
| 75 """Verifies output contains the error string.""" | |
| 76 result = _ReadOutputFileContents() | |
| 77 if result.get('error', '').find(expected_error_string) == -1: | |
| 78 print 'actual error:', result.get('error', ''), '\nexpected error:', \ | |
| 79 expected_error_string | |
| 80 test.fail_test() | |
| 81 | |
| 82 def EnsureWarning(expected_warning_string): | |
| 83 """Verifies output contains the warning string.""" | |
| 84 result = _ReadOutputFileContents() | |
| 85 if result.get('warning', '').find(expected_warning_string) == -1: | |
| 86 print 'actual warning:', result.get('warning', ''), \ | |
| 87 '\nexpected warning:', expected_warning_string | |
| 88 test.fail_test() | |
| 89 | |
| 90 # Verifies file_path must be specified. | |
| 91 test.run_gyp('test.gyp', | |
| 92 stdout='Must specify files to analyze via file_path generator ' | |
| 93 'flag\n') | |
| 94 | |
| 95 # Verifies config_path must point to a valid file. | |
| 96 test.run_gyp('test.gyp', '-Gconfig_path=bogus_file', | |
| 97 '-Ganalyzer_output_path=analyzer_output') | |
| 98 EnsureError('Unable to open file bogus_file') | |
| 99 | |
| 100 # Verify get error when bad target is specified. | |
| 101 _CreateTestFile(['exe2.c'], ['bad_target']) | |
| 102 run_analyzer() | |
| 103 EnsureWarning('Unable to find all targets') | |
| 104 | |
| 105 # Verifies config_path must point to a valid json file. | |
| 106 _CreateBogusTestFile() | |
| 107 run_analyzer() | |
| 108 EnsureError('Unable to parse config file test_file') | |
| 109 | |
| 110 # Trivial test of a source. | |
| 111 _CreateTestFile(['foo.c'], []) | |
| 112 run_analyzer() | |
| 113 EnsureContains(matched=True) | |
| 114 | |
| 115 # Conditional source that is excluded. | |
| 116 _CreateTestFile(['conditional_source.c'], []) | |
| 117 run_analyzer() | |
| 118 EnsureContains(matched=False) | |
| 119 | |
| 120 # Conditional source that is included by way of argument. | |
| 121 _CreateTestFile(['conditional_source.c'], []) | |
| 122 run_analyzer('-Dtest_variable=1') | |
| 123 EnsureContains(matched=True) | |
| 124 | |
| 125 # Two unknown files. | |
| 126 _CreateTestFile(['unknown1.c', 'unoknow2.cc'], []) | |
| 127 run_analyzer() | |
| 128 EnsureContains() | |
| 129 | |
| 130 # Two unknown files. | |
| 131 _CreateTestFile(['unknown1.c', 'subdir/subdir_sourcex.c'], []) | |
| 132 run_analyzer() | |
| 133 EnsureContains() | |
| 134 | |
| 135 # Included dependency | |
| 136 _CreateTestFile(['unknown1.c', 'subdir/subdir_source.c'], []) | |
| 137 run_analyzer() | |
| 138 EnsureContains(matched=True) | |
| 139 | |
| 140 # Included inputs to actions. | |
| 141 _CreateTestFile(['action_input.c'], []) | |
| 142 run_analyzer() | |
| 143 EnsureContains(matched=True) | |
| 144 | |
| 145 # Don't consider outputs. | |
| 146 _CreateTestFile(['action_output.c'], []) | |
| 147 run_analyzer() | |
| 148 EnsureContains(matched=False) | |
| 149 | |
| 150 # Rule inputs. | |
| 151 _CreateTestFile(['rule_input.c'], []) | |
| 152 run_analyzer() | |
| 153 EnsureContains(matched=True) | |
| 154 | |
| 155 # Ignore path specified with PRODUCT_DIR. | |
| 156 _CreateTestFile(['product_dir_input.c'], []) | |
| 157 run_analyzer() | |
| 158 EnsureContains(matched=False) | |
| 159 | |
| 160 # Path specified via a variable. | |
| 161 _CreateTestFile(['subdir/subdir_source2.c'], []) | |
| 162 run_analyzer() | |
| 163 EnsureContains(matched=True) | |
| 164 | |
| 165 # Verifies paths with // are fixed up correctly. | |
| 166 _CreateTestFile(['parent_source.c'], []) | |
| 167 run_analyzer() | |
| 168 EnsureContains(matched=True) | |
| 169 | |
| 170 # Verifies relative paths are resolved correctly. | |
| 171 _CreateTestFile(['subdir/subdir_source.h'], []) | |
| 172 run_analyzer() | |
| 173 EnsureContains(matched=True) | |
| 174 | |
| 175 # Various permutations when passing in targets. | |
| 176 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe', 'exe3']) | |
| 177 run_analyzer() | |
| 178 EnsureContains(matched=True, targets={'exe3'}) | |
| 179 | |
| 180 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe']) | |
| 181 run_analyzer() | |
| 182 EnsureContains(matched=True) | |
| 183 | |
| 184 # Verifies duplicates are ignored. | |
| 185 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe', 'exe']) | |
| 186 run_analyzer() | |
| 187 EnsureContains(matched=True) | |
| 188 | |
| 189 _CreateTestFile(['exe2.c'], ['exe']) | |
| 190 run_analyzer() | |
| 191 EnsureContains(matched=True) | |
| 192 | |
| 193 _CreateTestFile(['exe2.c'], []) | |
| 194 run_analyzer() | |
| 195 EnsureContains(matched=True) | |
| 196 | |
| 197 _CreateTestFile(['subdir/subdir2b_source.c', 'exe2.c'], []) | |
| 198 run_analyzer() | |
| 199 EnsureContains(matched=True) | |
| 200 | |
| 201 _CreateTestFile(['exe2.c'], []) | |
| 202 run_analyzer() | |
| 203 EnsureContains(matched=True) | |
| 204 | |
| 205 # Assertions when modifying build (gyp/gypi) files, especially when said files | |
| 206 # are included. | |
| 207 _CreateTestFile(['subdir2/d.cc'], ['exe', 'exe2', 'foo', 'exe3']) | |
| 208 run_analyzer2() | |
| 209 EnsureContains(matched=True, targets={'exe', 'foo'}) | |
| 210 | |
| 211 _CreateTestFile(['subdir2/subdir.includes.gypi'], | |
| 212 ['exe', 'exe2', 'foo', 'exe3']) | |
| 213 run_analyzer2() | |
| 214 EnsureContains(matched=True, targets={'exe', 'foo'}) | |
| 215 | |
| 216 _CreateTestFile(['subdir2/subdir.gyp'], ['exe', 'exe2', 'foo', 'exe3']) | |
| 217 run_analyzer2() | |
| 218 EnsureContains(matched=True, targets={'exe', 'foo'}) | |
| 219 | |
| 220 _CreateTestFile(['test2.includes.gypi'], ['exe', 'exe2', 'foo', 'exe3']) | |
| 221 run_analyzer2() | |
| 222 EnsureContains(matched=True, targets={'exe', 'exe2', 'exe3'}) | |
| 223 | |
| 224 # Verify modifying a file included makes all targets dirty. | |
| 225 _CreateTestFile(['common.gypi'], ['exe', 'exe2', 'foo', 'exe3']) | |
| 226 run_analyzer2('-Icommon.gypi') | |
| 227 EnsureContains(matched=True, targets={'exe', 'foo', 'exe2', 'exe3'}) | |
| 228 | |
| 229 test.pass_test() | |
| OLD | NEW |