OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. 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 """Unit tests for patch.py.""" | 6 """Unit tests for patch.py.""" |
7 | 7 |
8 import logging | 8 import logging |
9 import os | 9 import os |
10 import sys | 10 import sys |
(...skipping 22 matching lines...) Expand all Loading... |
33 self.assertEquals(p.filename, filename) | 33 self.assertEquals(p.filename, filename) |
34 self.assertEquals(p.source_filename, source_filename) | 34 self.assertEquals(p.source_filename, source_filename) |
35 self.assertEquals(p.is_binary, is_binary) | 35 self.assertEquals(p.is_binary, is_binary) |
36 self.assertEquals(p.is_delete, is_delete) | 36 self.assertEquals(p.is_delete, is_delete) |
37 if hasattr(p, 'is_git_diff'): | 37 if hasattr(p, 'is_git_diff'): |
38 self.assertEquals(p.is_git_diff, is_git_diff) | 38 self.assertEquals(p.is_git_diff, is_git_diff) |
39 self.assertEquals(p.is_new, is_new) | 39 self.assertEquals(p.is_new, is_new) |
40 if hasattr(p, 'patchlevel'): | 40 if hasattr(p, 'patchlevel'): |
41 self.assertEquals(p.patchlevel, patchlevel) | 41 self.assertEquals(p.patchlevel, patchlevel) |
42 if diff: | 42 if diff: |
43 self.assertEquals(p.get(), diff) | 43 if is_binary: |
| 44 self.assertEquals(p.get(), diff) |
| 45 else: |
| 46 self.assertEquals(p.get(True), diff) |
44 if hasattr(p, 'svn_properties'): | 47 if hasattr(p, 'svn_properties'): |
45 self.assertEquals(p.svn_properties, svn_properties) | 48 self.assertEquals(p.svn_properties, svn_properties) |
46 | 49 |
47 def testFilePatchDelete(self): | 50 def testFilePatchDelete(self): |
48 p = patch.FilePatchDelete('foo', False) | 51 p = patch.FilePatchDelete('foo', False) |
49 self._check_patch(p, 'foo', None, is_delete=True) | 52 self._check_patch(p, 'foo', None, is_delete=True) |
50 | 53 |
51 def testFilePatchDeleteBin(self): | 54 def testFilePatchDeleteBin(self): |
52 p = patch.FilePatchDelete('foo', True) | 55 p = patch.FilePatchDelete('foo', True) |
53 self._check_patch(p, 'foo', None, is_delete=True, is_binary=True) | 56 self._check_patch(p, 'foo', None, is_delete=True, is_binary=True) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 def testValidSvn(self): | 93 def testValidSvn(self): |
91 # pylint: disable=R0201 | 94 # pylint: disable=R0201 |
92 # Method could be a function | 95 # Method could be a function |
93 # Should not throw. | 96 # Should not throw. |
94 p = patch.FilePatchDiff('chrome/file.cc', RAW.PATCH, []) | 97 p = patch.FilePatchDiff('chrome/file.cc', RAW.PATCH, []) |
95 lines = RAW.PATCH.splitlines(True) | 98 lines = RAW.PATCH.splitlines(True) |
96 header = ''.join(lines[:4]) | 99 header = ''.join(lines[:4]) |
97 hunks = ''.join(lines[4:]) | 100 hunks = ''.join(lines[4:]) |
98 self.assertEquals(header, p.diff_header) | 101 self.assertEquals(header, p.diff_header) |
99 self.assertEquals(hunks, p.diff_hunks) | 102 self.assertEquals(hunks, p.diff_hunks) |
100 self.assertEquals(RAW.PATCH, p.get()) | 103 self.assertEquals(RAW.PATCH, p.get(True)) |
| 104 self.assertEquals(RAW.PATCH, p.get(False)) |
101 | 105 |
102 def testValidSvnNew(self): | 106 def testValidSvnNew(self): |
103 p = patch.FilePatchDiff('chrome/file.cc', RAW.MINIMAL_NEW, []) | 107 p = patch.FilePatchDiff('chrome/file.cc', RAW.MINIMAL_NEW, []) |
104 self.assertEquals(RAW.MINIMAL_NEW, p.diff_header) | 108 self.assertEquals(RAW.MINIMAL_NEW, p.diff_header) |
105 self.assertEquals('', p.diff_hunks) | 109 self.assertEquals('', p.diff_hunks) |
106 self.assertEquals(RAW.MINIMAL_NEW, p.get()) | 110 self.assertEquals(RAW.MINIMAL_NEW, p.get(True)) |
| 111 self.assertEquals(RAW.MINIMAL_NEW, p.get(False)) |
107 | 112 |
108 def testValidSvnDelete(self): | 113 def testValidSvnDelete(self): |
109 p = patch.FilePatchDiff('chrome/file.cc', RAW.MINIMAL_DELETE, []) | 114 p = patch.FilePatchDiff('chrome/file.cc', RAW.MINIMAL_DELETE, []) |
110 self.assertEquals(RAW.MINIMAL_DELETE, p.diff_header) | 115 self.assertEquals(RAW.MINIMAL_DELETE, p.diff_header) |
111 self.assertEquals('', p.diff_hunks) | 116 self.assertEquals('', p.diff_hunks) |
112 self.assertEquals(RAW.MINIMAL_DELETE, p.get()) | 117 self.assertEquals(RAW.MINIMAL_DELETE, p.get(True)) |
| 118 self.assertEquals(RAW.MINIMAL_DELETE, p.get(False)) |
| 119 |
| 120 def testValidSvnRename(self): |
| 121 p = patch.FilePatchDiff('file_b', RAW.MINIMAL_RENAME, []) |
| 122 self.assertEquals(RAW.MINIMAL_RENAME, p.diff_header) |
| 123 self.assertEquals('', p.diff_hunks) |
| 124 self.assertEquals(RAW.MINIMAL_RENAME, p.get(True)) |
| 125 self.assertEquals('--- file_b\n+++ file_b\n', p.get(False)) |
113 | 126 |
114 def testRelPath(self): | 127 def testRelPath(self): |
115 patches = patch.PatchSet([ | 128 patches = patch.PatchSet([ |
116 patch.FilePatchDiff('pp', GIT.COPY, []), | 129 patch.FilePatchDiff('pp', GIT.COPY, []), |
117 patch.FilePatchDiff( | 130 patch.FilePatchDiff( |
118 'chromeos\\views/webui_menu_widget.h', GIT.RENAME_PARTIAL, []), | 131 'chromeos\\views/webui_menu_widget.h', GIT.RENAME_PARTIAL, []), |
119 patch.FilePatchDiff('tools/run_local_server.sh', GIT.RENAME, []), | 132 patch.FilePatchDiff('tools/run_local_server.sh', GIT.RENAME, []), |
120 patch.FilePatchBinary('bar', 'data', [], is_new=False), | 133 patch.FilePatchBinary('bar', 'data', [], is_new=False), |
121 patch.FilePatchDiff('chrome/file.cc', RAW.PATCH, []), | 134 patch.FilePatchDiff('chrome/file.cc', RAW.PATCH, []), |
122 patch.FilePatchDiff('foo', GIT.NEW, []), | 135 patch.FilePatchDiff('foo', GIT.NEW, []), |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 self.assertEquals([None, None], [f.source_filename for f in patches]) | 182 self.assertEquals([None, None], [f.source_filename for f in patches]) |
170 | 183 |
171 def testBackSlash(self): | 184 def testBackSlash(self): |
172 mangled_patch = RAW.PATCH.replace('chrome/', 'chrome\\') | 185 mangled_patch = RAW.PATCH.replace('chrome/', 'chrome\\') |
173 patches = patch.PatchSet([ | 186 patches = patch.PatchSet([ |
174 patch.FilePatchDiff('chrome\\file.cc', mangled_patch, []), | 187 patch.FilePatchDiff('chrome\\file.cc', mangled_patch, []), |
175 patch.FilePatchDelete('other\\place\\foo', True), | 188 patch.FilePatchDelete('other\\place\\foo', True), |
176 ]) | 189 ]) |
177 expected = ['chrome/file.cc', 'other/place/foo'] | 190 expected = ['chrome/file.cc', 'other/place/foo'] |
178 self.assertEquals(expected, patches.filenames) | 191 self.assertEquals(expected, patches.filenames) |
179 self.assertEquals(RAW.PATCH, patches.patches[0].get()) | 192 self.assertEquals(RAW.PATCH, patches.patches[0].get(True)) |
| 193 self.assertEquals(RAW.PATCH, patches.patches[0].get(False)) |
180 | 194 |
181 def testDelete(self): | 195 def testDelete(self): |
182 p = patch.FilePatchDiff('tools/clang_check/README.chromium', RAW.DELETE, []) | 196 p = patch.FilePatchDiff('tools/clang_check/README.chromium', RAW.DELETE, []) |
183 self._check_patch( | 197 self._check_patch( |
184 p, 'tools/clang_check/README.chromium', RAW.DELETE, is_delete=True) | 198 p, 'tools/clang_check/README.chromium', RAW.DELETE, is_delete=True) |
185 | 199 |
186 def testGitDelete(self): | 200 def testGitDelete(self): |
187 p = patch.FilePatchDiff('tools/clang_check/README.chromium', GIT.DELETE, []) | 201 p = patch.FilePatchDiff('tools/clang_check/README.chromium', GIT.DELETE, []) |
188 self._check_patch( | 202 self._check_patch( |
189 p, 'tools/clang_check/README.chromium', GIT.DELETE, is_delete=True, | 203 p, 'tools/clang_check/README.chromium', GIT.DELETE, is_delete=True, |
(...skipping 30 matching lines...) Expand all Loading... |
220 p = patch.FilePatchDiff('file_b', RAW.MINIMAL_RENAME, []) | 234 p = patch.FilePatchDiff('file_b', RAW.MINIMAL_RENAME, []) |
221 self._check_patch( | 235 self._check_patch( |
222 p, 'file_b', RAW.MINIMAL_RENAME, source_filename='file_a', is_new=True) | 236 p, 'file_b', RAW.MINIMAL_RENAME, source_filename='file_a', is_new=True) |
223 | 237 |
224 def testGitCopyPartial(self): | 238 def testGitCopyPartial(self): |
225 p = patch.FilePatchDiff('wtf2', GIT.COPY_PARTIAL, []) | 239 p = patch.FilePatchDiff('wtf2', GIT.COPY_PARTIAL, []) |
226 self._check_patch( | 240 self._check_patch( |
227 p, 'wtf2', GIT.COPY_PARTIAL, source_filename='wtf', is_git_diff=True, | 241 p, 'wtf2', GIT.COPY_PARTIAL, source_filename='wtf', is_git_diff=True, |
228 patchlevel=1, is_new=True) | 242 patchlevel=1, is_new=True) |
229 | 243 |
| 244 def testGitCopyPartialAsSvn(self): |
| 245 p = patch.FilePatchDiff('wtf2', GIT.COPY_PARTIAL, []) |
| 246 # TODO(maruel): Improve processing. |
| 247 diff = ( |
| 248 'diff --git a/wtf2 b/wtf22\n' |
| 249 'similarity index 98%\n' |
| 250 'copy from wtf2\n' |
| 251 'copy to wtf22\n' |
| 252 'index 79fbaf3..3560689 100755\n' |
| 253 '--- a/wtf2\n' |
| 254 '+++ b/wtf22\n' |
| 255 '@@ -1,4 +1,4 @@\n' |
| 256 '-#!/usr/bin/env python\n' |
| 257 '+#!/usr/bin/env python1.3\n' |
| 258 ' # Copyright (c) 2010 The Chromium Authors. All rights reserved.\n' |
| 259 ' # blah blah blah as\n' |
| 260 ' # found in the LICENSE file.\n') |
| 261 self.assertEquals(diff, p.get(False)) |
| 262 |
230 def testGitNewExe(self): | 263 def testGitNewExe(self): |
231 p = patch.FilePatchDiff('natsort_test.py', GIT.NEW_EXE, []) | 264 p = patch.FilePatchDiff('natsort_test.py', GIT.NEW_EXE, []) |
232 self._check_patch( | 265 self._check_patch( |
233 p, 'natsort_test.py', GIT.NEW_EXE, is_new=True, is_git_diff=True, | 266 p, 'natsort_test.py', GIT.NEW_EXE, is_new=True, is_git_diff=True, |
234 patchlevel=1, svn_properties=[('svn:executable', '*')]) | 267 patchlevel=1, svn_properties=[('svn:executable', '*')]) |
235 | 268 |
236 def testGitNewMode(self): | 269 def testGitNewMode(self): |
237 p = patch.FilePatchDiff('natsort_test.py', GIT.NEW_MODE, []) | 270 p = patch.FilePatchDiff('natsort_test.py', GIT.NEW_MODE, []) |
238 self._check_patch( | 271 self._check_patch( |
239 p, 'natsort_test.py', GIT.NEW_MODE, is_new=True, is_git_diff=True, | 272 p, 'natsort_test.py', GIT.NEW_MODE, is_new=True, is_git_diff=True, |
240 patchlevel=1) | 273 patchlevel=1) |
241 | 274 |
| 275 def testPatchsetOrder(self): |
| 276 # Deletes must be last. |
| 277 # File renames/move/copy must be first. |
| 278 patches = [ |
| 279 patch.FilePatchDiff('chrome/file.cc', RAW.PATCH, []), |
| 280 patch.FilePatchDiff( |
| 281 'tools\\clang_check/README.chromium', GIT.DELETE, []), |
| 282 patch.FilePatchDiff('tools/run_local_server.sh', GIT.RENAME, []), |
| 283 patch.FilePatchDiff( |
| 284 'chromeos\\views/webui_menu_widget.h', GIT.RENAME_PARTIAL, []), |
| 285 patch.FilePatchDiff('pp', GIT.COPY, []), |
| 286 patch.FilePatchDiff('foo', GIT.NEW, []), |
| 287 patch.FilePatchDelete('other/place/foo', True), |
| 288 patch.FilePatchBinary('bar', 'data', [], is_new=False), |
| 289 ] |
| 290 expected = [ |
| 291 'pp', |
| 292 'chromeos/views/webui_menu_widget.h', |
| 293 'tools/run_local_server.sh', |
| 294 'bar', |
| 295 'chrome/file.cc', |
| 296 'foo', |
| 297 'other/place/foo', |
| 298 'tools/clang_check/README.chromium', |
| 299 ] |
| 300 patchset = patch.PatchSet(patches) |
| 301 self.assertEquals(expected, patchset.filenames) |
| 302 |
242 | 303 |
243 class PatchTestFail(unittest.TestCase): | 304 class PatchTestFail(unittest.TestCase): |
244 # All patches that should throw. | 305 # All patches that should throw. |
245 def testFilePatchDelete(self): | 306 def testFilePatchDelete(self): |
246 p = patch.FilePatchDelete('foo', False) | 307 self.assertFalse(hasattr(patch.FilePatchDelete('foo', False), 'get')) |
247 try: | |
248 p.get() | |
249 self.fail() | |
250 except NotImplementedError: | |
251 pass | |
252 | 308 |
253 def testFilePatchDeleteBin(self): | 309 def testFilePatchDeleteBin(self): |
254 p = patch.FilePatchDelete('foo', True) | 310 self.assertFalse(hasattr(patch.FilePatchDelete('foo', True), 'get')) |
255 try: | |
256 p.get() | |
257 self.fail() | |
258 except NotImplementedError: | |
259 pass | |
260 | 311 |
261 def testFilePatchDiffBad(self): | 312 def testFilePatchDiffBad(self): |
262 try: | 313 try: |
263 patch.FilePatchDiff('foo', 'data', []) | 314 patch.FilePatchDiff('foo', 'data', []) |
264 self.fail() | 315 self.fail() |
265 except patch.UnsupportedPatchFormat: | 316 except patch.UnsupportedPatchFormat: |
266 pass | 317 pass |
267 | 318 |
268 def testFilePatchDiffEmpty(self): | 319 def testFilePatchDiffEmpty(self): |
269 try: | 320 try: |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 self.fail() | 452 self.fail() |
402 except patch.UnsupportedPatchFormat: | 453 except patch.UnsupportedPatchFormat: |
403 pass | 454 pass |
404 | 455 |
405 | 456 |
406 if __name__ == '__main__': | 457 if __name__ == '__main__': |
407 logging.basicConfig(level= | 458 logging.basicConfig(level= |
408 [logging.WARNING, logging.INFO, logging.DEBUG][ | 459 [logging.WARNING, logging.INFO, logging.DEBUG][ |
409 min(2, sys.argv.count('-v'))]) | 460 min(2, sys.argv.count('-v'))]) |
410 unittest.main() | 461 unittest.main() |
OLD | NEW |