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 EnsureContains(targets=set(), matched=False): |
| 46 """Verifies output contains |targets| and |direct_targets|.""" |
| 47 result = _ReadOutputFileContents() |
| 48 if result.get('error', None): |
| 49 print 'unexpected error', result.get('error') |
| 50 test.fail_test() |
| 51 |
| 52 if result.get('warning', None): |
| 53 print 'unexpected warning', result.get('warning') |
| 54 test.fail_test() |
| 55 |
| 56 actual_targets = set(result['targets']) |
| 57 if actual_targets != targets: |
| 58 print 'actual targets:', actual_targets, '\nexpected targets:', targets |
| 59 test.fail_test() |
| 60 |
| 61 if matched and result['status'] != found: |
| 62 print 'expected', found, 'got', result['status'] |
| 63 test.fail_test() |
| 64 elif not matched and result['status'] != not_found: |
| 65 print 'expected', not_found, 'got', result['status'] |
| 66 test.fail_test() |
| 67 |
| 68 def EnsureError(expected_error_string): |
| 69 """Verifies output contains the error string.""" |
| 70 result = _ReadOutputFileContents() |
| 71 if result.get('error', '').find(expected_error_string) == -1: |
| 72 print 'actual error:', result.get('error', ''), '\nexpected error:', \ |
| 73 expected_error_string |
| 74 test.fail_test() |
| 75 |
| 76 def EnsureWarning(expected_warning_string): |
| 77 """Verifies output contains the warning string.""" |
| 78 result = _ReadOutputFileContents() |
| 79 if result.get('warning', '').find(expected_warning_string) == -1: |
| 80 print 'actual warning:', result.get('warning', ''), \ |
| 81 '\nexpected warning:', expected_warning_string |
| 82 test.fail_test() |
| 83 |
| 84 # Verifies file_path must be specified. |
| 85 test.run_gyp('test.gyp', |
| 86 stdout='Must specify files to analyze via file_path generator ' |
| 87 'flag\n') |
| 88 |
| 89 # Verifies config_path must point to a valid file. |
| 90 test.run_gyp('test.gyp', '-Gconfig_path=bogus_file', |
| 91 '-Ganalyzer_output_path=analyzer_output') |
| 92 EnsureError('Unable to open file bogus_file') |
| 93 |
| 94 # Verify get error when bad target is specified. |
| 95 _CreateTestFile(['exe2.c'], ['bad_target']) |
| 96 run_analyzer() |
| 97 EnsureWarning('Unable to find all targets') |
| 98 |
| 99 # Verifies config_path must point to a valid json file. |
| 100 _CreateBogusTestFile() |
| 101 run_analyzer() |
| 102 EnsureError('Unable to parse config file test_file') |
| 103 |
| 104 # Trivial test of a source. |
| 105 _CreateTestFile(['foo.c'], []) |
| 106 run_analyzer() |
| 107 EnsureContains(matched=True) |
| 108 |
| 109 # Conditional source that is excluded. |
| 110 _CreateTestFile(['conditional_source.c'], []) |
| 111 run_analyzer() |
| 112 EnsureContains(matched=False) |
| 113 |
| 114 # Conditional source that is included by way of argument. |
| 115 _CreateTestFile(['conditional_source.c'], []) |
| 116 run_analyzer('-Dtest_variable=1') |
| 117 EnsureContains(matched=True) |
| 118 |
| 119 # Two unknown files. |
| 120 _CreateTestFile(['unknown1.c', 'unoknow2.cc'], []) |
| 121 run_analyzer() |
| 122 EnsureContains() |
| 123 |
| 124 # Two unknown files. |
| 125 _CreateTestFile(['unknown1.c', 'subdir/subdir_sourcex.c'], []) |
| 126 run_analyzer() |
| 127 EnsureContains() |
| 128 |
| 129 # Included dependency |
| 130 _CreateTestFile(['unknown1.c', 'subdir/subdir_source.c'], []) |
| 131 run_analyzer() |
| 132 EnsureContains(matched=True) |
| 133 |
| 134 # Included inputs to actions. |
| 135 _CreateTestFile(['action_input.c'], []) |
| 136 run_analyzer() |
| 137 EnsureContains(matched=True) |
| 138 |
| 139 # Don't consider outputs. |
| 140 _CreateTestFile(['action_output.c'], []) |
| 141 run_analyzer() |
| 142 EnsureContains(matched=False) |
| 143 |
| 144 # Rule inputs. |
| 145 _CreateTestFile(['rule_input.c'], []) |
| 146 run_analyzer() |
| 147 EnsureContains(matched=True) |
| 148 |
| 149 # Ignore path specified with PRODUCT_DIR. |
| 150 _CreateTestFile(['product_dir_input.c'], []) |
| 151 run_analyzer() |
| 152 EnsureContains(matched=False) |
| 153 |
| 154 # Path specified via a variable. |
| 155 _CreateTestFile(['subdir/subdir_source2.c'], []) |
| 156 run_analyzer() |
| 157 EnsureContains(matched=True) |
| 158 |
| 159 # Verifies paths with // are fixed up correctly. |
| 160 _CreateTestFile(['parent_source.c'], []) |
| 161 run_analyzer() |
| 162 EnsureContains(matched=True) |
| 163 |
| 164 # Verifies relative paths are resolved correctly. |
| 165 _CreateTestFile(['subdir/subdir_source.h'], []) |
| 166 run_analyzer() |
| 167 EnsureContains(matched=True) |
| 168 |
| 169 # Various permutations when passing in targets. |
| 170 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe', 'exe3']) |
| 171 run_analyzer() |
| 172 EnsureContains(matched=True, targets={'exe3'}) |
| 173 |
| 174 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe']) |
| 175 run_analyzer() |
| 176 EnsureContains(matched=True) |
| 177 |
| 178 # Verifies duplicates are ignored. |
| 179 _CreateTestFile(['exe2.c', 'subdir/subdir2b_source.c'], ['exe', 'exe']) |
| 180 run_analyzer() |
| 181 EnsureContains(matched=True) |
| 182 |
| 183 _CreateTestFile(['exe2.c'], ['exe']) |
| 184 run_analyzer() |
| 185 EnsureContains(matched=True) |
| 186 |
| 187 _CreateTestFile(['exe2.c'], []) |
| 188 run_analyzer() |
| 189 EnsureContains(matched=True) |
| 190 |
| 191 _CreateTestFile(['subdir/subdir2b_source.c', 'exe2.c'], []) |
| 192 run_analyzer() |
| 193 EnsureContains(matched=True) |
| 194 |
| 195 _CreateTestFile(['exe2.c'], []) |
| 196 run_analyzer() |
| 197 EnsureContains(matched=True) |
| 198 |
| 199 test.pass_test() |
OLD | NEW |