Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: tests/patch_test.py

Issue 9167015: Fix svn delete handling (again). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « testing_support/patches_data.py ('k') | tests/rietveld_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 posixpath 10 import posixpath
(...skipping 11 matching lines...) Expand all
22 def _check_patch(self, 22 def _check_patch(self,
23 p, 23 p,
24 filename, 24 filename,
25 diff, 25 diff,
26 source_filename=None, 26 source_filename=None,
27 is_binary=False, 27 is_binary=False,
28 is_delete=False, 28 is_delete=False,
29 is_git_diff=False, 29 is_git_diff=False,
30 is_new=False, 30 is_new=False,
31 patchlevel=0, 31 patchlevel=0,
32 svn_properties=None): 32 svn_properties=None,
33 nb_hunks=None):
33 svn_properties = svn_properties or [] 34 svn_properties = svn_properties or []
34 self.assertEquals(p.filename, filename) 35 self.assertEquals(p.filename, filename)
35 self.assertEquals(p.source_filename, source_filename) 36 self.assertEquals(p.source_filename, source_filename)
36 self.assertEquals(p.is_binary, is_binary) 37 self.assertEquals(p.is_binary, is_binary)
37 self.assertEquals(p.is_delete, is_delete) 38 self.assertEquals(p.is_delete, is_delete)
38 if hasattr(p, 'is_git_diff'): 39 if hasattr(p, 'is_git_diff'):
39 self.assertEquals(p.is_git_diff, is_git_diff) 40 self.assertEquals(p.is_git_diff, is_git_diff)
40 self.assertEquals(p.is_new, is_new) 41 self.assertEquals(p.is_new, is_new)
41 if hasattr(p, 'patchlevel'): 42 if hasattr(p, 'patchlevel'):
42 self.assertEquals(p.patchlevel, patchlevel) 43 self.assertEquals(p.patchlevel, patchlevel)
43 if diff: 44 if diff:
44 if is_binary: 45 if is_binary:
45 self.assertEquals(p.get(), diff) 46 self.assertEquals(p.get(), diff)
46 else: 47 else:
47 self.assertEquals(p.get(True), diff) 48 self.assertEquals(p.get(True), diff)
48 if hasattr(p, 'svn_properties'): 49 if hasattr(p, 'hunks'):
49 self.assertEquals(p.svn_properties, svn_properties) 50 self.assertEquals(len(p.hunks), nb_hunks)
51 else:
52 self.assertEquals(None, nb_hunks)
50 53
51 def testFilePatchDelete(self): 54 def testFilePatchDelete(self):
52 p = patch.FilePatchDelete('foo', False) 55 p = patch.FilePatchDelete('foo', False)
53 self._check_patch(p, 'foo', None, is_delete=True) 56 self._check_patch(p, 'foo', None, is_delete=True)
54 57
55 def testFilePatchDeleteBin(self): 58 def testFilePatchDeleteBin(self):
56 p = patch.FilePatchDelete('foo', True) 59 p = patch.FilePatchDelete('foo', True)
57 self._check_patch(p, 'foo', None, is_delete=True, is_binary=True) 60 self._check_patch(p, 'foo', None, is_delete=True, is_binary=True)
58 61
59 def testFilePatchBinary(self): 62 def testFilePatchBinary(self):
60 p = patch.FilePatchBinary('foo', 'data', [], is_new=False) 63 p = patch.FilePatchBinary('foo', 'data', [], is_new=False)
61 self._check_patch(p, 'foo', 'data', is_binary=True) 64 self._check_patch(p, 'foo', 'data', is_binary=True)
62 65
63 def testFilePatchBinaryNew(self): 66 def testFilePatchBinaryNew(self):
64 p = patch.FilePatchBinary('foo', 'data', [], is_new=True) 67 p = patch.FilePatchBinary('foo', 'data', [], is_new=True)
65 self._check_patch(p, 'foo', 'data', is_binary=True, is_new=True) 68 self._check_patch(p, 'foo', 'data', is_binary=True, is_new=True)
66 69
67 def testFilePatchDiff(self): 70 def testFilePatchDiff(self):
68 p = patch.FilePatchDiff('chrome/file.cc', RAW.PATCH, []) 71 p = patch.FilePatchDiff('chrome/file.cc', RAW.PATCH, [])
69 self._check_patch(p, 'chrome/file.cc', RAW.PATCH) 72 self._check_patch(p, 'chrome/file.cc', RAW.PATCH, nb_hunks=1)
70 73
71 def testFilePatchDiffHeaderMode(self): 74 def testFilePatchDiffHeaderMode(self):
72 p = patch.FilePatchDiff('git_cl/git-cl', GIT.MODE_EXE, []) 75 p = patch.FilePatchDiff('git_cl/git-cl', GIT.MODE_EXE, [])
73 self._check_patch( 76 self._check_patch(
74 p, 'git_cl/git-cl', GIT.MODE_EXE, is_git_diff=True, patchlevel=1, 77 p, 'git_cl/git-cl', GIT.MODE_EXE, is_git_diff=True, patchlevel=1,
75 svn_properties=[('svn:executable', '*')]) 78 svn_properties=[('svn:executable', '*')], nb_hunks=0)
76 79
77 def testFilePatchDiffHeaderModeIndex(self): 80 def testFilePatchDiffHeaderModeIndex(self):
78 p = patch.FilePatchDiff('git_cl/git-cl', GIT.MODE_EXE_JUNK, []) 81 p = patch.FilePatchDiff('git_cl/git-cl', GIT.MODE_EXE_JUNK, [])
79 self._check_patch( 82 self._check_patch(
80 p, 'git_cl/git-cl', GIT.MODE_EXE_JUNK, is_git_diff=True, patchlevel=1, 83 p, 'git_cl/git-cl', GIT.MODE_EXE_JUNK, is_git_diff=True, patchlevel=1,
81 svn_properties=[('svn:executable', '*')]) 84 svn_properties=[('svn:executable', '*')], nb_hunks=0)
82 85
83 def testFilePatchDiffSvnNew(self): 86 def testFilePatchDiffSvnNew(self):
84 # The code path is different for git and svn. 87 # The code path is different for git and svn.
85 p = patch.FilePatchDiff('foo', RAW.NEW, []) 88 p = patch.FilePatchDiff('foo', RAW.NEW, [])
86 self._check_patch(p, 'foo', RAW.NEW, is_new=True) 89 self._check_patch(p, 'foo', RAW.NEW, is_new=True, nb_hunks=1)
87 90
88 def testFilePatchDiffGitNew(self): 91 def testFilePatchDiffGitNew(self):
89 # The code path is different for git and svn. 92 # The code path is different for git and svn.
90 p = patch.FilePatchDiff('foo', GIT.NEW, []) 93 p = patch.FilePatchDiff('foo', GIT.NEW, [])
91 self._check_patch( 94 self._check_patch(
92 p, 'foo', GIT.NEW, is_new=True, is_git_diff=True, patchlevel=1) 95 p, 'foo', GIT.NEW, is_new=True, is_git_diff=True, patchlevel=1,
96 nb_hunks=1)
93 97
94 def testValidSvn(self): 98 def testSvn(self):
95 # Should not throw. 99 # Should not throw.
96 p = patch.FilePatchDiff('chrome/file.cc', RAW.PATCH, []) 100 p = patch.FilePatchDiff('chrome/file.cc', RAW.PATCH, [])
97 lines = RAW.PATCH.splitlines(True) 101 lines = RAW.PATCH.splitlines(True)
98 header = ''.join(lines[:4]) 102 header = ''.join(lines[:4])
99 hunks = ''.join(lines[4:]) 103 hunks = ''.join(lines[4:])
100 self.assertEquals(header, p.diff_header) 104 self.assertEquals(header, p.diff_header)
101 self.assertEquals(hunks, p.diff_hunks) 105 self.assertEquals(hunks, p.diff_hunks)
102 self.assertEquals(RAW.PATCH, p.get(True)) 106 self.assertEquals(RAW.PATCH, p.get(True))
103 self.assertEquals(RAW.PATCH, p.get(False)) 107 self.assertEquals(RAW.PATCH, p.get(False))
104 108
105 def testValidSvnNew(self): 109 def testSvnNew(self):
106 p = patch.FilePatchDiff('chrome/file.cc', RAW.MINIMAL_NEW, []) 110 p = patch.FilePatchDiff('chrome/file.cc', RAW.MINIMAL_NEW, [])
107 self.assertEquals(RAW.MINIMAL_NEW, p.diff_header) 111 self.assertEquals(RAW.MINIMAL_NEW, p.diff_header)
108 self.assertEquals('', p.diff_hunks) 112 self.assertEquals('', p.diff_hunks)
109 self.assertEquals(RAW.MINIMAL_NEW, p.get(True)) 113 self.assertEquals(RAW.MINIMAL_NEW, p.get(True))
110 self.assertEquals(RAW.MINIMAL_NEW, p.get(False)) 114 self.assertEquals(RAW.MINIMAL_NEW, p.get(False))
111 115
112 def testValidSvnDelete(self): 116 def testSvnDelete(self):
113 p = patch.FilePatchDiff('chrome/file.cc', RAW.MINIMAL_DELETE, []) 117 p = patch.FilePatchDiff('chrome/file.cc', RAW.MINIMAL_DELETE, [])
114 self.assertEquals(RAW.MINIMAL_DELETE, p.diff_header) 118 self.assertEquals(RAW.MINIMAL_DELETE, p.diff_header)
115 self.assertEquals('', p.diff_hunks) 119 self.assertEquals('', p.diff_hunks)
116 self.assertEquals(RAW.MINIMAL_DELETE, p.get(True)) 120 self.assertEquals(RAW.MINIMAL_DELETE, p.get(True))
117 self.assertEquals(RAW.MINIMAL_DELETE, p.get(False)) 121 self.assertEquals(RAW.MINIMAL_DELETE, p.get(False))
118 122
119 def testValidSvnRename(self): 123 def testSvnRename(self):
120 p = patch.FilePatchDiff('file_b', RAW.MINIMAL_RENAME, []) 124 p = patch.FilePatchDiff('file_b', RAW.MINIMAL_RENAME, [])
121 self.assertEquals(RAW.MINIMAL_RENAME, p.diff_header) 125 self.assertEquals(RAW.MINIMAL_RENAME, p.diff_header)
122 self.assertEquals('', p.diff_hunks) 126 self.assertEquals('', p.diff_hunks)
123 self.assertEquals(RAW.MINIMAL_RENAME, p.get(True)) 127 self.assertEquals(RAW.MINIMAL_RENAME, p.get(True))
124 self.assertEquals('--- file_b\n+++ file_b\n', p.get(False)) 128 self.assertEquals('--- file_b\n+++ file_b\n', p.get(False))
125 129
126 def testRelPath(self): 130 def testRelPath(self):
127 patches = patch.PatchSet([ 131 patches = patch.PatchSet([
128 patch.FilePatchDiff('pp', GIT.COPY, []), 132 patch.FilePatchDiff('pp', GIT.COPY, []),
129 patch.FilePatchDiff( 133 patch.FilePatchDiff(
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 mangled_patch = RAW.PATCH.replace('chrome/', 'chrome\\') 189 mangled_patch = RAW.PATCH.replace('chrome/', 'chrome\\')
186 patches = patch.PatchSet([ 190 patches = patch.PatchSet([
187 patch.FilePatchDiff('chrome\\file.cc', mangled_patch, []), 191 patch.FilePatchDiff('chrome\\file.cc', mangled_patch, []),
188 patch.FilePatchDelete('other\\place\\foo', True), 192 patch.FilePatchDelete('other\\place\\foo', True),
189 ]) 193 ])
190 expected = ['chrome/file.cc', 'other/place/foo'] 194 expected = ['chrome/file.cc', 'other/place/foo']
191 self.assertEquals(expected, patches.filenames) 195 self.assertEquals(expected, patches.filenames)
192 self.assertEquals(RAW.PATCH, patches.patches[0].get(True)) 196 self.assertEquals(RAW.PATCH, patches.patches[0].get(True))
193 self.assertEquals(RAW.PATCH, patches.patches[0].get(False)) 197 self.assertEquals(RAW.PATCH, patches.patches[0].get(False))
194 198
199 def testTwoHunks(self):
200 name = 'chrome/app/generated_resources.grd'
201 p = patch.FilePatchDiff(name, RAW.TWO_HUNKS, [])
202 self._check_patch(p, name, RAW.TWO_HUNKS, nb_hunks=2)
203
204 def testGitThreeHunks(self):
205 p = patch.FilePatchDiff('presubmit_support.py', GIT.FOUR_HUNKS, [])
206 self._check_patch(
207 p, 'presubmit_support.py', GIT.FOUR_HUNKS, is_git_diff=True,
208 patchlevel=1,
209 nb_hunks=4)
210
195 def testDelete(self): 211 def testDelete(self):
196 p = patch.FilePatchDiff('tools/clang_check/README.chromium', RAW.DELETE, []) 212 p = patch.FilePatchDiff('tools/clang_check/README.chromium', RAW.DELETE, [])
197 self._check_patch( 213 self._check_patch(
198 p, 'tools/clang_check/README.chromium', RAW.DELETE, is_delete=True) 214 p, 'tools/clang_check/README.chromium', RAW.DELETE, is_delete=True,
215 nb_hunks=1)
216
217 def testDelete2(self):
218 name = 'browser/extensions/extension_sidebar_api.cc'
219 p = patch.FilePatchDiff(name, RAW.DELETE2, [])
220 self._check_patch(p, name, RAW.DELETE2, is_delete=True, nb_hunks=1)
199 221
200 def testGitDelete(self): 222 def testGitDelete(self):
201 p = patch.FilePatchDiff('tools/clang_check/README.chromium', GIT.DELETE, []) 223 p = patch.FilePatchDiff('tools/clang_check/README.chromium', GIT.DELETE, [])
202 self._check_patch( 224 self._check_patch(
203 p, 'tools/clang_check/README.chromium', GIT.DELETE, is_delete=True, 225 p, 'tools/clang_check/README.chromium', GIT.DELETE, is_delete=True,
204 is_git_diff=True, patchlevel=1) 226 is_git_diff=True, patchlevel=1, nb_hunks=1)
205 227
206 def testGitRename(self): 228 def testGitRename(self):
207 p = patch.FilePatchDiff('tools/run_local_server.sh', GIT.RENAME, []) 229 p = patch.FilePatchDiff('tools/run_local_server.sh', GIT.RENAME, [])
208 self._check_patch(p, 'tools/run_local_server.sh', GIT.RENAME, 230 self._check_patch(
209 is_git_diff=True, patchlevel=1, 231 p,
210 source_filename='tools/run_local_server.PY', is_new=True) 232 'tools/run_local_server.sh',
233 GIT.RENAME,
234 is_git_diff=True,
235 patchlevel=1,
236 source_filename='tools/run_local_server.PY',
237 is_new=True,
238 nb_hunks=0)
211 239
212 def testGitRenamePartial(self): 240 def testGitRenamePartial(self):
213 p = patch.FilePatchDiff( 241 p = patch.FilePatchDiff(
214 'chromeos/views/webui_menu_widget.h', GIT.RENAME_PARTIAL, []) 242 'chromeos/views/webui_menu_widget.h', GIT.RENAME_PARTIAL, [])
215 self._check_patch( 243 self._check_patch(
216 p, 'chromeos/views/webui_menu_widget.h', GIT.RENAME_PARTIAL, 244 p,
217 source_filename='chromeos/views/DOMui_menu_widget.h', is_git_diff=True, 245 'chromeos/views/webui_menu_widget.h',
218 patchlevel=1, is_new=True) 246 GIT.RENAME_PARTIAL,
247 source_filename='chromeos/views/DOMui_menu_widget.h',
248 is_git_diff=True,
249 patchlevel=1,
250 is_new=True,
251 nb_hunks=1)
219 252
220 def testGitCopy(self): 253 def testGitCopy(self):
221 p = patch.FilePatchDiff('pp', GIT.COPY, []) 254 p = patch.FilePatchDiff('pp', GIT.COPY, [])
222 self._check_patch(p, 'pp', GIT.COPY, is_git_diff=True, patchlevel=1, 255 self._check_patch(
223 source_filename='PRESUBMIT.py', is_new=True) 256 p, 'pp', GIT.COPY, is_git_diff=True, patchlevel=1,
257 source_filename='PRESUBMIT.py', is_new=True, nb_hunks=0)
224 258
225 def testOnlyHeader(self): 259 def testOnlyHeader(self):
226 p = patch.FilePatchDiff('file_a', RAW.MINIMAL, []) 260 p = patch.FilePatchDiff('file_a', RAW.MINIMAL, [])
227 self._check_patch(p, 'file_a', RAW.MINIMAL) 261 self._check_patch(p, 'file_a', RAW.MINIMAL, nb_hunks=0)
228 262
229 def testSmallest(self): 263 def testSmallest(self):
230 p = patch.FilePatchDiff('file_a', RAW.NEW_NOT_NULL, []) 264 p = patch.FilePatchDiff('file_a', RAW.NEW_NOT_NULL, [])
231 self._check_patch(p, 'file_a', RAW.NEW_NOT_NULL) 265 self._check_patch(p, 'file_a', RAW.NEW_NOT_NULL, is_new=True, nb_hunks=1)
232 266
233 def testRenameOnlyHeader(self): 267 def testRenameOnlyHeader(self):
234 p = patch.FilePatchDiff('file_b', RAW.MINIMAL_RENAME, []) 268 p = patch.FilePatchDiff('file_b', RAW.MINIMAL_RENAME, [])
235 self._check_patch( 269 self._check_patch(
236 p, 'file_b', RAW.MINIMAL_RENAME, source_filename='file_a', is_new=True) 270 p, 'file_b', RAW.MINIMAL_RENAME, source_filename='file_a', is_new=True,
271 nb_hunks=0)
237 272
238 def testGitCopyPartial(self): 273 def testGitCopyPartial(self):
239 p = patch.FilePatchDiff('wtf2', GIT.COPY_PARTIAL, []) 274 p = patch.FilePatchDiff('wtf2', GIT.COPY_PARTIAL, [])
240 self._check_patch( 275 self._check_patch(
241 p, 'wtf2', GIT.COPY_PARTIAL, source_filename='wtf', is_git_diff=True, 276 p, 'wtf2', GIT.COPY_PARTIAL, source_filename='wtf', is_git_diff=True,
242 patchlevel=1, is_new=True) 277 patchlevel=1, is_new=True, nb_hunks=1)
243 278
244 def testGitCopyPartialAsSvn(self): 279 def testGitCopyPartialAsSvn(self):
245 p = patch.FilePatchDiff('wtf2', GIT.COPY_PARTIAL, []) 280 p = patch.FilePatchDiff('wtf2', GIT.COPY_PARTIAL, [])
246 # TODO(maruel): Improve processing. 281 # TODO(maruel): Improve processing.
247 diff = ( 282 diff = (
248 'diff --git a/wtf2 b/wtf22\n' 283 'diff --git a/wtf2 b/wtf22\n'
249 'similarity index 98%\n' 284 'similarity index 98%\n'
250 'copy from wtf2\n' 285 'copy from wtf2\n'
251 'copy to wtf22\n' 286 'copy to wtf22\n'
252 'index 79fbaf3..3560689 100755\n' 287 'index 79fbaf3..3560689 100755\n'
253 '--- a/wtf2\n' 288 '--- a/wtf2\n'
254 '+++ b/wtf22\n' 289 '+++ b/wtf22\n'
255 '@@ -1,4 +1,4 @@\n' 290 '@@ -1,4 +1,4 @@\n'
256 '-#!/usr/bin/env python\n' 291 '-#!/usr/bin/env python\n'
257 '+#!/usr/bin/env python1.3\n' 292 '+#!/usr/bin/env python1.3\n'
258 ' # Copyright (c) 2010 The Chromium Authors. All rights reserved.\n' 293 ' # Copyright (c) 2010 The Chromium Authors. All rights reserved.\n'
259 ' # blah blah blah as\n' 294 ' # blah blah blah as\n'
260 ' # found in the LICENSE file.\n') 295 ' # found in the LICENSE file.\n')
261 self.assertEquals(diff, p.get(False)) 296 self.assertEquals(diff, p.get(False))
262 297
263 def testGitNewExe(self): 298 def testGitNewExe(self):
264 p = patch.FilePatchDiff('natsort_test.py', GIT.NEW_EXE, []) 299 p = patch.FilePatchDiff('natsort_test.py', GIT.NEW_EXE, [])
265 self._check_patch( 300 self._check_patch(
266 p, 'natsort_test.py', GIT.NEW_EXE, is_new=True, is_git_diff=True, 301 p,
267 patchlevel=1, svn_properties=[('svn:executable', '*')]) 302 'natsort_test.py',
303 GIT.NEW_EXE,
304 is_new=True,
305 is_git_diff=True,
306 patchlevel=1,
307 svn_properties=[('svn:executable', '*')],
308 nb_hunks=1)
268 309
269 def testGitNewMode(self): 310 def testGitNewMode(self):
270 p = patch.FilePatchDiff('natsort_test.py', GIT.NEW_MODE, []) 311 p = patch.FilePatchDiff('natsort_test.py', GIT.NEW_MODE, [])
271 self._check_patch( 312 self._check_patch(
272 p, 'natsort_test.py', GIT.NEW_MODE, is_new=True, is_git_diff=True, 313 p, 'natsort_test.py', GIT.NEW_MODE, is_new=True, is_git_diff=True,
273 patchlevel=1) 314 patchlevel=1, nb_hunks=1)
274 315
275 def testPatchsetOrder(self): 316 def testPatchsetOrder(self):
276 # Deletes must be last. 317 # Deletes must be last.
277 # File renames/move/copy must be first. 318 # File renames/move/copy must be first.
278 patches = [ 319 patches = [
279 patch.FilePatchDiff('chrome/file.cc', RAW.PATCH, []), 320 patch.FilePatchDiff('chrome/file.cc', RAW.PATCH, []),
280 patch.FilePatchDiff( 321 patch.FilePatchDiff(
281 'tools\\clang_check/README.chromium', GIT.DELETE, []), 322 'tools\\clang_check/README.chromium', GIT.DELETE, []),
282 patch.FilePatchDiff('tools/run_local_server.sh', GIT.RENAME, []), 323 patch.FilePatchDiff('tools/run_local_server.sh', GIT.RENAME, []),
283 patch.FilePatchDiff( 324 patch.FilePatchDiff(
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 self.fail() 493 self.fail()
453 except patch.UnsupportedPatchFormat: 494 except patch.UnsupportedPatchFormat:
454 pass 495 pass
455 496
456 497
457 if __name__ == '__main__': 498 if __name__ == '__main__':
458 logging.basicConfig(level= 499 logging.basicConfig(level=
459 [logging.WARNING, logging.INFO, logging.DEBUG][ 500 [logging.WARNING, logging.INFO, logging.DEBUG][
460 min(2, sys.argv.count('-v'))]) 501 min(2, sys.argv.count('-v'))])
461 unittest.main() 502 unittest.main()
OLDNEW
« no previous file with comments | « testing_support/patches_data.py ('k') | tests/rietveld_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698