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