OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2014 The Chromium Authors. All rights reserved. |
rpaquay
2014/02/18 21:39:26
nit: not "(c)"
| |
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 import difflib | 6 import difflib |
7 import os | 7 import os |
8 import re | 8 import re |
9 import unittest | 9 import unittest |
10 | 10 |
11 import PRESUBMIT | 11 from strict_enum_value_checker import StrictEnumValueChecker |
12 | 12 |
13 class MockLogging(object): | 13 class MockLogging(object): |
14 def __init__(self): | 14 def __init__(self): |
15 self.lines = [] | 15 self.lines = [] |
16 | 16 |
17 def info(self, message): | 17 def info(self, message): |
18 self.lines.append(message) | 18 self.lines.append(message) |
19 | 19 |
20 def debug(self, message): | 20 def debug(self, message): |
21 self.lines.append(message) | 21 self.lines.append(message) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 def NewContents(self): | 68 def NewContents(self): |
69 return self._new_contents | 69 return self._new_contents |
70 | 70 |
71 def LocalPath(self): | 71 def LocalPath(self): |
72 return self._local_path | 72 return self._local_path |
73 | 73 |
74 def IsDirectory(self): | 74 def IsDirectory(self): |
75 return False | 75 return False |
76 | 76 |
77 def GenerateScmDiff(self): | 77 def GenerateScmDiff(self): |
78 result = "" | 78 result = '' |
rpaquay
2014/02/18 21:39:26
nit: why changing usage to single quote?
| |
79 for line in difflib.unified_diff(self._old_contents, self._new_contents, | 79 for line in difflib.unified_diff(self._old_contents, self._new_contents, |
80 self._local_path, self._local_path): | 80 self._local_path, self._local_path): |
81 result += line | 81 result += line |
82 return result | 82 return result |
83 | 83 |
84 # NOTE: This method is a copy of ChangeContents method of AffectedFile in | 84 # NOTE: This method is a copy of ChangeContents method of AffectedFile in |
85 # presubmit_support.py | 85 # presubmit_support.py |
86 def ChangedContents(self): | 86 def ChangedContents(self): |
87 """Returns a list of tuples (line number, line text) of all new lines. | 87 '''Returns a list of tuples (line number, line text) of all new lines. |
88 | 88 |
89 This relies on the scm diff output describing each changed code section | 89 This relies on the scm diff output describing each changed code section |
90 with a line of the form | 90 with a line of the form |
91 | 91 |
92 ^@@ <old line num>,<old size> <new line num>,<new size> @@$ | 92 ^@@ <old line num>,<old size> <new line num>,<new size> @@$ |
93 """ | 93 ''' |
94 if self._cached_changed_contents is not None: | 94 if self._cached_changed_contents is not None: |
95 return self._cached_changed_contents[:] | 95 return self._cached_changed_contents[:] |
96 self._cached_changed_contents = [] | 96 self._cached_changed_contents = [] |
97 line_num = 0 | 97 line_num = 0 |
98 | 98 |
99 if self.IsDirectory(): | 99 if self.IsDirectory(): |
100 return [] | 100 return [] |
101 | 101 |
102 for line in self.GenerateScmDiff().splitlines(): | 102 for line in self.GenerateScmDiff().splitlines(): |
103 m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line) | 103 m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line) |
104 if m: | 104 if m: |
105 line_num = int(m.groups(1)[0]) | 105 line_num = int(m.groups(1)[0]) |
106 continue | 106 continue |
107 if line.startswith('+') and not line.startswith('++'): | 107 if line.startswith('+') and not line.startswith('++'): |
108 self._cached_changed_contents.append((line_num, line[1:])) | 108 self._cached_changed_contents.append((line_num, line[1:])) |
109 if not line.startswith('-'): | 109 if not line.startswith('-'): |
110 line_num += 1 | 110 line_num += 1 |
111 return self._cached_changed_contents[:] | 111 return self._cached_changed_contents[:] |
112 | 112 |
113 | 113 |
114 class MockChange(object): | 114 class MockChange(object): |
115 def __init__(self, changed_files): | 115 def __init__(self, changed_files): |
116 self._changed_files = changed_files | 116 self._changed_files = changed_files |
117 | 117 |
118 def LocalPaths(self): | 118 def LocalPaths(self): |
119 return self._changed_files | 119 return self._changed_files |
120 | 120 |
121 | 121 |
122 class HistogramValueCheckerTest(unittest.TestCase): | 122 class StrictEnumValueCheckerTest(unittest.TestCase): |
123 TEST_FILE_PATTERN = "PRESUBMIT_test_new_file_%s.txt" | 123 TEST_FILE_PATTERN = 'changed_file_%s.h' |
124 MOCK_FILE_LOCAL_PATH = 'mock_enum.h' | |
125 START_MARKER = 'enum MockEnum {' | |
126 END_MARKER = ' mBoundary' | |
124 | 127 |
125 def _ReadTextFileContents(self, path): | 128 def _ReadTextFileContents(self, path): |
126 """Given a path, returns a list of strings corresponding to the text lines | 129 '''Given a path, returns a list of strings corresponding to the text lines |
127 in the file. Reads files in text format. | 130 in the file. Reads files in text format. |
128 | 131 |
129 """ | 132 ''' |
130 fo = open(path, 'r') | 133 fo = open(path, 'r') |
131 try: | 134 try: |
132 contents = fo.readlines() | 135 contents = fo.readlines() |
133 finally: | 136 finally: |
134 fo.close() | 137 fo.close() |
135 return contents | 138 return contents |
136 | 139 |
137 def _ReadInputFile(self): | 140 def _ReadInputFile(self): |
138 return self._ReadTextFileContents("PRESUBMIT_test_old_file.txt") | 141 return self._ReadTextFileContents('mock_enum.h') |
139 | 142 |
140 def _PrepareTest(self, new_file_path): | 143 def _PrepareTest(self, new_file_path): |
141 old_contents = self._ReadInputFile() | 144 old_contents = self._ReadInputFile() |
142 if not new_file_path: | 145 if not new_file_path: |
143 new_contents = [] | 146 new_contents = [] |
144 else: | 147 else: |
145 new_contents = self._ReadTextFileContents(new_file_path) | 148 new_contents = self._ReadTextFileContents(new_file_path) |
146 input_api = MockInputApi() | 149 input_api = MockInputApi() |
147 mock_file = MockFile(PRESUBMIT.HistogramValueChecker.LOCAL_PATH, | 150 mock_file = MockFile(self.MOCK_FILE_LOCAL_PATH, |
148 old_contents, | 151 old_contents, |
149 new_contents) | 152 new_contents) |
150 input_api.files.append(mock_file) | 153 input_api.files.append(mock_file) |
151 output_api = MockOutputApi() | 154 output_api = MockOutputApi() |
152 return input_api, output_api | 155 return input_api, output_api |
153 | 156 |
154 def _RunTest(self, new_file_path): | 157 def _RunTest(self, new_file_path): |
155 input_api, output_api = self._PrepareTest(new_file_path) | 158 input_api, output_api = self._PrepareTest(new_file_path) |
156 checker = PRESUBMIT.HistogramValueChecker(input_api, output_api) | 159 checker = StrictEnumValueChecker(input_api, output_api, self.START_MARKER, |
160 self.END_MARKER, self.MOCK_FILE_LOCAL_PATH) | |
157 results = checker.Run() | 161 results = checker.Run() |
158 return results | 162 return results |
159 | 163 |
160 def testDeleteFile(self): | 164 def testDeleteFile(self): |
161 results = self._RunTest(new_file_path=None) | 165 results = self._RunTest(new_file_path=None) |
162 # TODO(rpaquay) How to check it's the expected warning?' | 166 # TODO(rpaquay) How to check it's the expected warning?' |
163 self.assertEquals(1, len(results), | 167 self.assertEquals(1, len(results), |
164 "We hould get a single warning about file deletion.") | 168 'We should get a single warning about file deletion.') |
165 | 169 |
166 def testSimpleValidEdit(self): | 170 def testSimpleValidEdit(self): |
167 results = self._RunTest(self.TEST_FILE_PATTERN % "1") | 171 results = self._RunTest(self.TEST_FILE_PATTERN % '1') |
168 # TODO(rpaquay) How to check it's the expected warning?' | 172 # TODO(rpaquay) How to check it's the expected warning?' |
169 self.assertEquals(0, len(results), | 173 self.assertEquals(0, len(results), |
170 "We should get no warning for simple edits.") | 174 'We should get no warning for simple edits.') |
171 | 175 |
172 def testSingleDeletionOfEntry(self): | 176 def testSingleDeletionOfEntry(self): |
173 results = self._RunTest(self.TEST_FILE_PATTERN % "2") | 177 results = self._RunTest(self.TEST_FILE_PATTERN % '2') |
174 # TODO(rpaquay) How to check it's the expected warning?' | 178 # TODO(rpaquay) How to check it's the expected warning?' |
175 self.assertEquals(1, len(results), | 179 self.assertEquals(1, len(results), |
176 "We should get a warning for an entry deletion.") | 180 'We should get a warning for an entry deletion.') |
177 | 181 |
178 def testSingleRenameOfEntry(self): | 182 def testSingleRenameOfEntry(self): |
179 results = self._RunTest(self.TEST_FILE_PATTERN % "3") | 183 results = self._RunTest(self.TEST_FILE_PATTERN % '3') |
180 # TODO(rpaquay) How to check it's the expected warning?' | 184 # TODO(rpaquay) How to check it's the expected warning?' |
181 self.assertEquals(1, len(results), | 185 self.assertEquals(1, len(results), |
182 "We should get a warning for an entry rename, even " | 186 'We should get a warning for an entry rename, even ' |
183 "though it is not optimal.") | 187 'though it is not optimal.') |
184 | 188 |
185 def testMissingEnumStartOfEntry(self): | 189 def testMissingEnumStartOfEntry(self): |
186 results = self._RunTest(self.TEST_FILE_PATTERN % "4") | 190 results = self._RunTest(self.TEST_FILE_PATTERN % '4') |
187 # TODO(rpaquay) How to check it's the expected warning?' | 191 # TODO(rpaquay) How to check it's the expected warning?' |
188 self.assertEquals(1, len(results), | 192 self.assertEquals(1, len(results), |
189 "We should get a warning for a missing enum marker.") | 193 'We should get a warning for a missing enum marker.') |
190 | 194 |
191 def testMissingEnumEndOfEntry(self): | 195 def testMissingEnumEndOfEntry(self): |
192 results = self._RunTest(self.TEST_FILE_PATTERN % "5") | 196 results = self._RunTest(self.TEST_FILE_PATTERN % '5') |
193 # TODO(rpaquay) How to check it's the expected warning?' | 197 # TODO(rpaquay) How to check it's the expected warning?' |
194 self.assertEquals(1, len(results), | 198 self.assertEquals(1, len(results), |
195 "We should get a warning for a missing enum marker.") | 199 'We should get a warning for a missing enum marker.') |
196 | 200 |
197 def testInvertedEnumMarkersOfEntry(self): | 201 def testInvertedEnumMarkersOfEntry(self): |
198 results = self._RunTest(self.TEST_FILE_PATTERN % "6") | 202 results = self._RunTest(self.TEST_FILE_PATTERN % '6') |
199 # TODO(rpaquay) How to check it's the expected warning?' | 203 # TODO(rpaquay) How to check it's the expected warning?' |
200 self.assertEquals(1, len(results), | 204 self.assertEquals(1, len(results), |
201 "We should get a warning for inverted enum markers.") | 205 'We should get a warning for inverted enum markers.') |
202 | 206 |
203 def testMultipleInvalidEdits(self): | 207 def testMultipleInvalidEdits(self): |
204 results = self._RunTest(self.TEST_FILE_PATTERN % "7") | 208 results = self._RunTest(self.TEST_FILE_PATTERN % '7') |
205 # TODO(rpaquay) How to check it's the expected warning?' | 209 # TODO(rpaquay) How to check it's the expected warning?' |
206 self.assertEquals(3, len(results), | 210 self.assertEquals(3, len(results), |
207 "We should get 3 warnings (one per edit).") | 211 'We should get 3 warnings (one per edit).') |
208 | 212 |
209 def testSingleInvalidInserts(self): | 213 def testSingleInvalidInserts(self): |
210 results = self._RunTest(self.TEST_FILE_PATTERN % "8") | 214 results = self._RunTest(self.TEST_FILE_PATTERN % '8') |
211 # TODO(rpaquay) How to check it's the expected warning?' | 215 # TODO(rpaquay) How to check it's the expected warning?' |
212 self.assertEquals(1, len(results), | 216 self.assertEquals(1, len(results), |
213 "We should get a warning for a single invalid " | 217 'We should get a warning for a single invalid ' |
214 "insertion inside the enum.") | 218 'insertion inside the enum.') |
215 | 219 |
216 def testMulitpleValidInserts(self): | 220 def testMulitpleValidInserts(self): |
217 results = self._RunTest(self.TEST_FILE_PATTERN % "9") | 221 results = self._RunTest(self.TEST_FILE_PATTERN % '9') |
218 # TODO(rpaquay) How to check it's the expected warning?' | 222 # TODO(rpaquay) How to check it's the expected warning?' |
219 self.assertEquals(0, len(results), | 223 self.assertEquals(0, len(results), |
220 "We should not get a warning mulitple valid edits") | 224 'We should not get a warning mulitple valid edits') |
221 | 225 |
222 def testSingleValidDeleteOutsideOfEnum(self): | 226 def testSingleValidDeleteOutsideOfEnum(self): |
223 results = self._RunTest(self.TEST_FILE_PATTERN % "10") | 227 results = self._RunTest(self.TEST_FILE_PATTERN % '10') |
224 # TODO(rpaquay) How to check it's the expected warning?' | 228 # TODO(rpaquay) How to check it's the expected warning?' |
225 self.assertEquals(0, len(results), | 229 self.assertEquals(0, len(results), |
226 "We should not get a warning for a deletion outside of " | 230 'We should not get a warning for a deletion outside of ' |
227 "the enum") | 231 'the enum') |
228 | 232 |
229 | 233 |
230 if __name__ == '__main__': | 234 if __name__ == '__main__': |
231 unittest.main() | 235 unittest.main() |
OLD | NEW |