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

Side by Side Diff: tests/patch_test.py

Issue 7789030: Centralize testing to make it simpler to test when rename are supported correctly (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Created 9 years, 3 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 | « no previous file | no next file » | 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 sys 10 import sys
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 134
135 135
136 DELETE = ( 136 DELETE = (
137 '--- tools/clang_check/README.chromium\n' 137 '--- tools/clang_check/README.chromium\n'
138 '+++ /dev/null\n' 138 '+++ /dev/null\n'
139 '@@ -1,1 +0,0 @@\n' 139 '@@ -1,1 +0,0 @@\n'
140 '-bar\n') 140 '-bar\n')
141 141
142 142
143 class PatchTest(unittest.TestCase): 143 class PatchTest(unittest.TestCase):
144 def _check_patch(self,
145 p,
146 filename,
147 diff,
148 is_binary=False,
149 is_delete=False,
150 is_git_diff=False,
151 is_new=False,
152 patchlevel=0,
153 svn_properties=None):
154 svn_properties = svn_properties or []
155 self.assertEquals(p.filename, filename)
156 self.assertEquals(p.is_binary, is_binary)
157 self.assertEquals(p.is_delete, is_delete)
158 if hasattr(p, 'is_git_diff'):
159 self.assertEquals(p.is_git_diff, is_git_diff)
160 self.assertEquals(p.is_new, is_new)
161 if hasattr(p, 'patchlevel'):
162 self.assertEquals(p.patchlevel, patchlevel)
163 if diff:
164 self.assertEquals(p.get(), diff)
165 if hasattr(p, 'svn_properties'):
166 self.assertEquals(p.svn_properties, svn_properties)
167
144 def testFilePatchDelete(self): 168 def testFilePatchDelete(self):
145 p = patch.FilePatchDelete('foo', False) 169 p = patch.FilePatchDelete('foo', False)
146 self.assertEquals(p.filename, 'foo') 170 self._check_patch(p, 'foo', None, is_delete=True)
147 self.assertEquals(p.is_binary, False)
148 self.assertEquals(p.is_delete, True)
149 self.assertEquals(p.is_new, False)
150 try: 171 try:
151 p.get() 172 p.get()
152 self.fail() 173 self.fail()
153 except NotImplementedError: 174 except NotImplementedError:
154 pass 175 pass
176
177 def testFilePatchDeleteBin(self):
155 p = patch.FilePatchDelete('foo', True) 178 p = patch.FilePatchDelete('foo', True)
156 self.assertEquals(p.filename, 'foo') 179 self._check_patch(p, 'foo', None, is_delete=True, is_binary=True)
157 self.assertEquals(p.is_binary, True)
158 self.assertEquals(p.is_delete, True)
159 self.assertEquals(p.is_new, False)
160 try: 180 try:
161 p.get() 181 p.get()
162 self.fail() 182 self.fail()
163 except NotImplementedError: 183 except NotImplementedError:
164 pass 184 pass
165 185
166 def testFilePatchBinary(self): 186 def testFilePatchBinary(self):
167 p = patch.FilePatchBinary('foo', 'data', [], is_new=False) 187 p = patch.FilePatchBinary('foo', 'data', [], is_new=False)
168 self.assertEquals(p.filename, 'foo') 188 self._check_patch(p, 'foo', 'data', is_binary=True)
169 self.assertEquals(p.is_binary, True)
170 self.assertEquals(p.is_delete, False)
171 self.assertEquals(p.is_new, False)
172 self.assertEquals(p.get(), 'data')
173 189
174 def testFilePatchBinaryNew(self): 190 def testFilePatchBinaryNew(self):
175 p = patch.FilePatchBinary('foo', 'data', [], is_new=True) 191 p = patch.FilePatchBinary('foo', 'data', [], is_new=True)
176 self.assertEquals(p.filename, 'foo') 192 self._check_patch(p, 'foo', 'data', is_binary=True, is_new=True)
177 self.assertEquals(p.is_binary, True)
178 self.assertEquals(p.is_delete, False)
179 self.assertEquals(p.is_new, True)
180 self.assertEquals(p.get(), 'data')
181 193
182 def testFilePatchDiff(self): 194 def testFilePatchDiff(self):
183 p = patch.FilePatchDiff('chrome/file.cc', SVN_PATCH, []) 195 p = patch.FilePatchDiff('chrome/file.cc', SVN_PATCH, [])
184 self.assertEquals(p.filename, 'chrome/file.cc') 196 self._check_patch(p, 'chrome/file.cc', SVN_PATCH)
185 self.assertEquals(p.is_binary, False)
186 self.assertEquals(p.is_delete, False)
187 self.assertEquals(p.is_git_diff, False)
188 self.assertEquals(p.is_new, False)
189 self.assertEquals(p.patchlevel, 0)
190 self.assertEquals(p.get(), SVN_PATCH)
191 197
192 def testFilePatchDiffHeaderMode(self): 198 def testFilePatchDiffHeaderMode(self):
193 diff = ( 199 diff = (
194 'diff --git a/git_cl/git-cl b/git_cl/git-cl\n' 200 'diff --git a/git_cl/git-cl b/git_cl/git-cl\n'
195 'old mode 100644\n' 201 'old mode 100644\n'
196 'new mode 100755\n') 202 'new mode 100755\n')
197 p = patch.FilePatchDiff('git_cl/git-cl', diff, []) 203 p = patch.FilePatchDiff('git_cl/git-cl', diff, [])
198 self.assertEquals(p.filename, 'git_cl/git-cl') 204 self._check_patch(
199 self.assertEquals(p.is_binary, False) 205 p, 'git_cl/git-cl', diff, is_git_diff=True, patchlevel=1,
200 self.assertEquals(p.is_delete, False) 206 svn_properties=[('svn:executable', '*')])
201 self.assertEquals(p.is_git_diff, True)
202 self.assertEquals(p.is_new, False)
203 self.assertEquals(p.patchlevel, 1)
204 self.assertEquals(p.get(), diff)
205 self.assertEquals([('svn:executable', '*')], p.svn_properties)
206 207
207 def testFilePatchDiffHeaderModeIndex(self): 208 def testFilePatchDiffHeaderModeIndex(self):
208 diff = ( 209 diff = (
209 'Index: Junk\n' 210 'Index: Junk\n'
210 'diff --git a/git_cl/git-cl b/git_cl/git-cl\n' 211 'diff --git a/git_cl/git-cl b/git_cl/git-cl\n'
211 'old mode 100644\n' 212 'old mode 100644\n'
212 'new mode 100755\n') 213 'new mode 100755\n')
213 p = patch.FilePatchDiff('git_cl/git-cl', diff, []) 214 p = patch.FilePatchDiff('git_cl/git-cl', diff, [])
214 self.assertEquals(p.filename, 'git_cl/git-cl') 215 self._check_patch(
215 self.assertEquals(p.is_binary, False) 216 p, 'git_cl/git-cl', diff, is_git_diff=True, patchlevel=1,
216 self.assertEquals(p.is_delete, False) 217 svn_properties=[('svn:executable', '*')])
217 self.assertEquals(p.is_git_diff, True)
218 self.assertEquals(p.is_new, False)
219 self.assertEquals(p.patchlevel, 1)
220 self.assertEquals(p.get(), diff)
221 218
222 def testFilePatchDiffSvnNew(self): 219 def testFilePatchDiffSvnNew(self):
223 # The code path is different for git and svn. 220 # The code path is different for git and svn.
224 p = patch.FilePatchDiff('foo', NEW, []) 221 p = patch.FilePatchDiff('foo', NEW, [])
225 self.assertEquals(p.filename, 'foo') 222 self._check_patch(p, 'foo', NEW, is_new=True)
226 self.assertEquals(p.is_binary, False)
227 self.assertEquals(p.is_delete, False)
228 self.assertEquals(p.is_git_diff, False)
229 self.assertEquals(p.is_new, True)
230 self.assertEquals(p.patchlevel, 0)
231 self.assertEquals(p.get(), NEW)
232 223
233 def testFilePatchDiffGitNew(self): 224 def testFilePatchDiffGitNew(self):
234 # The code path is different for git and svn. 225 # The code path is different for git and svn.
235 p = patch.FilePatchDiff('foo', GIT_NEW, []) 226 p = patch.FilePatchDiff('foo', GIT_NEW, [])
236 self.assertEquals(p.filename, 'foo') 227 self._check_patch(
237 self.assertEquals(p.is_binary, False) 228 p, 'foo', GIT_NEW, is_new=True, is_git_diff=True, patchlevel=1)
238 self.assertEquals(p.is_delete, False)
239 self.assertEquals(p.is_git_diff, True)
240 self.assertEquals(p.is_new, True)
241 self.assertEquals(p.patchlevel, 1)
242 self.assertEquals(p.get(), GIT_NEW)
243 229
244 def testFilePatchDiffBad(self): 230 def testFilePatchDiffBad(self):
245 try: 231 try:
246 patch.FilePatchDiff('foo', 'data', []) 232 patch.FilePatchDiff('foo', 'data', [])
247 self.fail() 233 self.fail()
248 except patch.UnsupportedPatchFormat: 234 except patch.UnsupportedPatchFormat:
249 pass 235 pass
250 236
251 def testFilePatchDiffEmpty(self): 237 def testFilePatchDiffEmpty(self):
252 try: 238 try:
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 patches = patch.PatchSet([ 433 patches = patch.PatchSet([
448 patch.FilePatchDiff('chrome\\file.cc', mangled_patch, []), 434 patch.FilePatchDiff('chrome\\file.cc', mangled_patch, []),
449 patch.FilePatchDelete('other\\place\\foo', True), 435 patch.FilePatchDelete('other\\place\\foo', True),
450 ]) 436 ])
451 expected = ['chrome/file.cc', 'other/place/foo'] 437 expected = ['chrome/file.cc', 'other/place/foo']
452 self.assertEquals(expected, patches.filenames) 438 self.assertEquals(expected, patches.filenames)
453 self.assertEquals(SVN_PATCH, patches.patches[0].get()) 439 self.assertEquals(SVN_PATCH, patches.patches[0].get())
454 440
455 def testDelete(self): 441 def testDelete(self):
456 p = patch.FilePatchDiff('tools/clang_check/README.chromium', DELETE, []) 442 p = patch.FilePatchDiff('tools/clang_check/README.chromium', DELETE, [])
457 self.assertEquals(p.filename, 'tools/clang_check/README.chromium') 443 self._check_patch(
458 self.assertEquals(p.is_binary, False) 444 p, 'tools/clang_check/README.chromium', DELETE, is_delete=True)
459 self.assertEquals(p.is_delete, True)
460 self.assertEquals(p.is_new, False)
461 self.assertEquals(p.get(), DELETE)
462 self.assertEquals([], p.svn_properties)
463 445
464 def testGitDelete(self): 446 def testGitDelete(self):
465 p = patch.FilePatchDiff('tools/clang_check/README.chromium', GIT_DELETE, []) 447 p = patch.FilePatchDiff('tools/clang_check/README.chromium', GIT_DELETE, [])
466 self.assertEquals(p.filename, 'tools/clang_check/README.chromium') 448 self._check_patch(
467 self.assertEquals(p.is_binary, False) 449 p, 'tools/clang_check/README.chromium', GIT_DELETE, is_delete=True,
468 self.assertEquals(p.is_delete, True) 450 is_git_diff=True, patchlevel=1)
469 self.assertEquals(p.is_new, False)
470 self.assertEquals(p.get(), GIT_DELETE)
471 self.assertEquals([], p.svn_properties)
472 451
473 def testGitRename(self): 452 def testGitRename(self):
474 p = patch.FilePatchDiff('tools/run_local_server.sh', GIT_RENAME, []) 453 p = patch.FilePatchDiff('tools/run_local_server.sh', GIT_RENAME, [])
475 self.assertEquals(p.filename, 'tools/run_local_server.sh') 454 self._check_patch(p, 'tools/run_local_server.sh', GIT_RENAME,
476 self.assertEquals(p.is_binary, False) 455 is_git_diff=True, patchlevel=1)
477 self.assertEquals(p.is_delete, False)
478 self.assertEquals(p.is_new, False)
479 self.assertEquals(p.get(), GIT_RENAME)
480 self.assertEquals([], p.svn_properties)
481 456
482 def testGitRenamePartial(self): 457 def testGitRenamePartial(self):
483 p = patch.FilePatchDiff( 458 p = patch.FilePatchDiff(
484 'chrome/browser/chromeos/views/webui_menu_widget.h', 459 'chrome/browser/chromeos/views/webui_menu_widget.h',
485 GIT_RENAME_PARTIAL, []) 460 GIT_RENAME_PARTIAL, [])
486 self.assertEquals( 461 self._check_patch(
487 p.filename, 'chrome/browser/chromeos/views/webui_menu_widget.h') 462 p, 'chrome/browser/chromeos/views/webui_menu_widget.h',
488 self.assertEquals(p.is_binary, False) 463 GIT_RENAME_PARTIAL, is_git_diff=True, patchlevel=1)
489 self.assertEquals(p.is_delete, False)
490 self.assertEquals(p.is_new, False)
491 self.assertEquals(p.get(), GIT_RENAME_PARTIAL)
492 self.assertEquals([], p.svn_properties)
493 464
494 def testGitCopy(self): 465 def testGitCopy(self):
495 p = patch.FilePatchDiff('pp', GIT_COPY, []) 466 p = patch.FilePatchDiff('pp', GIT_COPY, [])
496 self.assertEquals(p.filename, 'pp') 467 self._check_patch(p, 'pp', GIT_COPY, is_git_diff=True, patchlevel=1)
497 self.assertEquals(p.is_binary, False)
498 self.assertEquals(p.is_delete, False)
499 self.assertEquals(p.is_new, False)
500 self.assertEquals(p.get(), GIT_COPY)
501 self.assertEquals([], p.svn_properties)
502
503 def testGitNew(self):
504 p = patch.FilePatchDiff('foo', GIT_NEW, [])
505 self.assertEquals(p.filename, 'foo')
506 self.assertEquals(p.is_binary, False)
507 self.assertEquals(p.is_delete, False)
508 self.assertEquals(p.is_new, True)
509 self.assertEquals(p.get(), GIT_NEW)
510 self.assertEquals([], p.svn_properties)
511 468
512 def testOnlyHeader(self): 469 def testOnlyHeader(self):
513 p = patch.FilePatchDiff('file_a', '--- file_a\n+++ file_a\n', []) 470 diff = '--- file_a\n+++ file_a\n'
514 self.assertTrue(p) 471 p = patch.FilePatchDiff('file_a', diff, [])
472 self._check_patch(p, 'file_a', diff)
515 473
516 def testSmallest(self): 474 def testSmallest(self):
517 p = patch.FilePatchDiff( 475 diff = '--- file_a\n+++ file_a\n@@ -0,0 +1 @@\n+foo\n'
518 'file_a', '--- file_a\n+++ file_a\n@@ -0,0 +1 @@\n+foo\n', []) 476 p = patch.FilePatchDiff('file_a', diff, [])
519 self.assertTrue(p) 477 self._check_patch(p, 'file_a', diff)
520 478
521 def testInverted(self): 479 def testInverted(self):
522 try: 480 try:
523 patch.FilePatchDiff( 481 patch.FilePatchDiff(
524 'file_a', '+++ file_a\n--- file_a\n@@ -0,0 +1 @@\n+foo\n', []) 482 'file_a', '+++ file_a\n--- file_a\n@@ -0,0 +1 @@\n+foo\n', [])
525 self.fail() 483 self.fail()
526 except patch.UnsupportedPatchFormat: 484 except patch.UnsupportedPatchFormat:
527 pass 485 pass
528 486
529 def testInvertedOnlyHeader(self): 487 def testInvertedOnlyHeader(self):
530 try: 488 try:
531 patch.FilePatchDiff('file_a', '+++ file_a\n--- file_a\n', []) 489 patch.FilePatchDiff('file_a', '+++ file_a\n--- file_a\n', [])
532 self.fail() 490 self.fail()
533 except patch.UnsupportedPatchFormat: 491 except patch.UnsupportedPatchFormat:
534 pass 492 pass
535 493
536 def testRenameOnlyHeader(self): 494 def testRenameOnlyHeader(self):
537 p = patch.FilePatchDiff('file_b', '--- file_a\n+++ file_b\n', []) 495 diff = '--- file_a\n+++ file_b\n'
538 self.assertEquals(p.filename, 'file_b') 496 p = patch.FilePatchDiff('file_b', diff, [])
539 self.assertEquals(p.is_binary, False)
540 self.assertEquals(p.is_delete, False)
541 # Should it be marked as new? 497 # Should it be marked as new?
542 self.assertEquals(p.is_new, False) 498 self._check_patch(p, 'file_b', diff)
543 self.assertEquals(p.get(), '--- file_a\n+++ file_b\n')
544 self.assertEquals([], p.svn_properties)
545 499
546 def testGitCopyPartial(self): 500 def testGitCopyPartial(self):
547 diff = ( 501 diff = (
548 'diff --git a/wtf b/wtf2\n' 502 'diff --git a/wtf b/wtf2\n'
549 'similarity index 98%\n' 503 'similarity index 98%\n'
550 'copy from wtf\n' 504 'copy from wtf\n'
551 'copy to wtf2\n' 505 'copy to wtf2\n'
552 'index 79fbaf3..3560689 100755\n' 506 'index 79fbaf3..3560689 100755\n'
553 '--- a/wtf\n' 507 '--- a/wtf\n'
554 '+++ b/wtf2\n' 508 '+++ b/wtf2\n'
555 '@@ -1,4 +1,4 @@\n' 509 '@@ -1,4 +1,4 @@\n'
556 '-#!/usr/bin/env python\n' 510 '-#!/usr/bin/env python\n'
557 '+#!/usr/bin/env python1.3\n' 511 '+#!/usr/bin/env python1.3\n'
558 ' # Copyright (c) 2010 The Chromium Authors. All rights reserved.\n' 512 ' # Copyright (c) 2010 The Chromium Authors. All rights reserved.\n'
559 ' # blah blah blah as\n' 513 ' # blah blah blah as\n'
560 ' # found in the LICENSE file.\n') 514 ' # found in the LICENSE file.\n')
561 p = patch.FilePatchDiff('wtf2', diff, []) 515 p = patch.FilePatchDiff('wtf2', diff, [])
562 self.assertEquals(p.filename, 'wtf2')
563 self.assertEquals(p.is_binary, False)
564 self.assertEquals(p.is_delete, False)
565 # Should it be marked as new? 516 # Should it be marked as new?
566 self.assertEquals(p.is_new, False) 517 self._check_patch(p, 'wtf2', diff, is_git_diff=True, patchlevel=1)
567 self.assertEquals(p.get(), diff)
568 self.assertEquals([], p.svn_properties)
569 518
570 def testGitExe(self): 519 def testGitExe(self):
571 diff = ( 520 diff = (
572 'diff --git a/natsort_test.py b/natsort_test.py\n' 521 'diff --git a/natsort_test.py b/natsort_test.py\n'
573 'new file mode 100755\n' 522 'new file mode 100755\n'
574 '--- /dev/null\n' 523 '--- /dev/null\n'
575 '+++ b/natsort_test.py\n' 524 '+++ b/natsort_test.py\n'
576 '@@ -0,0 +1,1 @@\n' 525 '@@ -0,0 +1,1 @@\n'
577 '+#!/usr/bin/env python\n') 526 '+#!/usr/bin/env python\n')
578 p = patch.FilePatchDiff('natsort_test.py', diff, []) 527 p = patch.FilePatchDiff('natsort_test.py', diff, [])
579 self.assertEquals(p.filename, 'natsort_test.py') 528 self._check_patch(
580 self.assertEquals(p.is_binary, False) 529 p, 'natsort_test.py', diff, is_new=True, is_git_diff=True, patchlevel=1,
581 self.assertEquals(p.is_delete, False) 530 svn_properties=[('svn:executable', '*')])
582 self.assertEquals(p.is_new, True)
583 self.assertEquals(p.get(), diff)
584 self.assertEquals([('svn:executable', '*')], p.svn_properties)
585 531
586 def testGitExe2(self): 532 def testGitExe2(self):
587 diff = ( 533 diff = (
588 'diff --git a/natsort_test.py b/natsort_test.py\n' 534 'diff --git a/natsort_test.py b/natsort_test.py\n'
589 'new file mode 100644\n' 535 'new file mode 100644\n'
590 '--- /dev/null\n' 536 '--- /dev/null\n'
591 '+++ b/natsort_test.py\n' 537 '+++ b/natsort_test.py\n'
592 '@@ -0,0 +1,1 @@\n' 538 '@@ -0,0 +1,1 @@\n'
593 '+#!/usr/bin/env python\n') 539 '+#!/usr/bin/env python\n')
594 p = patch.FilePatchDiff('natsort_test.py', diff, []) 540 p = patch.FilePatchDiff('natsort_test.py', diff, [])
595 self.assertEquals(p.filename, 'natsort_test.py') 541 self._check_patch(
596 self.assertEquals(p.is_binary, False) 542 p, 'natsort_test.py', diff, is_new=True, is_git_diff=True, patchlevel=1)
597 self.assertEquals(p.is_delete, False)
598 self.assertEquals(p.is_new, True)
599 self.assertEquals(p.get(), diff)
600 self.assertEquals([], p.svn_properties)
601 543
602 544
603 if __name__ == '__main__': 545 if __name__ == '__main__':
604 logging.basicConfig(level= 546 logging.basicConfig(level=
605 [logging.WARNING, logging.INFO, logging.DEBUG][ 547 [logging.WARNING, logging.INFO, logging.DEBUG][
606 min(2, sys.argv.count('-v'))]) 548 min(2, sys.argv.count('-v'))])
607 unittest.main() 549 unittest.main()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698