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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 '-run during normal compile, but for now, you can user run_plugin.sh to ' | 67 '-run during normal compile, but for now, you can user run_plugin.sh to ' |
68 'hack the\n' | 68 'hack the\n' |
69 '-build system to do a syntax check.\n' | 69 '-build system to do a syntax check.\n' |
70 '-\n' | 70 '-\n' |
71 '-Also see http://code.google.com/p/chromium/wiki/WritingClangPlugins\n' | 71 '-Also see http://code.google.com/p/chromium/wiki/WritingClangPlugins\n' |
72 '-\n') | 72 '-\n') |
73 | 73 |
74 | 74 |
75 # http://codereview.chromium.org/download/issue6250123_3013_6010.diff | 75 # http://codereview.chromium.org/download/issue6250123_3013_6010.diff |
76 GIT_RENAME_PARTIAL = ( | 76 GIT_RENAME_PARTIAL = ( |
77 'Index: chrome/browser/chromeos/views/webui_menu_widget.h\n' | 77 'Index: chromeos/views/webui_menu_widget.h\n' |
78 'diff --git a/chrome/browser/chromeos/views/domui_menu_widget.h ' | 78 'diff --git a/chromeos/views/DOMui_menu_widget.h ' |
79 'b/chrome/browser/chromeos/views/webui_menu_widget.h\n' | 79 'b/chromeos/views/webui_menu_widget.h\n' |
80 'similarity index 79%\n' | 80 'similarity index 79%\n' |
81 'rename from chrome/browser/chromeos/views/domui_menu_widget.h\n' | 81 'rename from chromeos/views/DOMui_menu_widget.h\n' |
82 'rename to chrome/browser/chromeos/views/webui_menu_widget.h\n' | 82 'rename to chromeos/views/webui_menu_widget.h\n' |
83 'index 095d4c474fd9718f5aebfa41a1ccb2d951356d41..' | 83 'index 095d4c474fd9718f5aebfa41a1ccb2d951356d41..' |
84 '157925075434b590e8acaaf605a64f24978ba08b 100644\n' | 84 '157925075434b590e8acaaf605a64f24978ba08b 100644\n' |
85 '--- a/chrome/browser/chromeos/views/domui_menu_widget.h\n' | 85 '--- a/chromeos/views/DOMui_menu_widget.h\n' |
86 '+++ b/chrome/browser/chromeos/views/webui_menu_widget.h\n' | 86 '+++ b/chromeos/views/webui_menu_widget.h\n' |
87 '@@ -1,9 +1,9 @@\n' | 87 '@@ -1,9 +1,9 @@\n' |
88 '-// Copyright (c) 2010 The Chromium Authors. All rights reserved.\n' | 88 '-// Copyright (c) 2010 The Chromium Authors. All rights reserved.\n' |
89 '+// Copyright (c) 2011 The Chromium Authors. All rights reserved.\n' | 89 '+// Copyright (c) 2011 The Chromium Authors. All rights reserved.\n' |
90 ' // Use of this source code is governed by a BSD-style license that can be' | 90 ' // Use of this source code is governed by a BSD-style license that can be' |
91 '\n' | 91 '\n' |
92 ' // found in the LICENSE file.\n' | 92 ' // found in the LICENSE file.\n' |
93 ' \n' | 93 ' \n' |
94 '-#ifndef CHROME_BROWSER_CHROMEOS_VIEWS_DOMUI_MENU_WIDGET_H_\n' | 94 '-#ifndef CHROME_BROWSER_CHROMEOS_VIEWS_DOMUI_MENU_WIDGET_H_\n' |
95 '-#define CHROME_BROWSER_CHROMEOS_VIEWS_DOMUI_MENU_WIDGET_H_\n' | 95 '-#define CHROME_BROWSER_CHROMEOS_VIEWS_DOMUI_MENU_WIDGET_H_\n' |
96 '+#ifndef CHROME_BROWSER_CHROMEOS_VIEWS_WEBUI_MENU_WIDGET_H_\n' | 96 '+#ifndef CHROME_BROWSER_CHROMEOS_VIEWS_WEBUI_MENU_WIDGET_H_\n' |
97 '+#define CHROME_BROWSER_CHROMEOS_VIEWS_WEBUI_MENU_WIDGET_H_\n' | 97 '+#define CHROME_BROWSER_CHROMEOS_VIEWS_WEBUI_MENU_WIDGET_H_\n' |
98 ' #pragma once\n' | 98 ' #pragma once\n' |
99 ' \n' | 99 ' \n' |
100 ' #include <string>\n') | 100 ' #include <string>\n') |
101 | 101 |
102 | 102 |
103 # http://codereview.chromium.org/download/issue6287022_3001_4010.diff | 103 # http://codereview.chromium.org/download/issue6287022_3001_4010.diff |
104 GIT_RENAME = ( | 104 GIT_RENAME = ( |
105 'Index: tools/run_local_server.sh\n' | 105 'Index: tools/run_local_server.sh\n' |
106 'diff --git a/tools/run_local_server.py b/tools/run_local_server.sh\n' | 106 'diff --git a/tools/run_local_server.PY b/tools/run_local_server.sh\n' |
107 'similarity index 100%\n' | 107 'similarity index 100%\n' |
108 'rename from tools/run_local_server.py\n' | 108 'rename from tools/run_local_server.PY\n' |
109 'rename to tools/run_local_server.sh\n') | 109 'rename to tools/run_local_server.sh\n') |
110 | 110 |
111 | 111 |
112 GIT_COPY = ( | 112 GIT_COPY = ( |
113 'diff --git a/PRESUBMIT.py b/pp\n' | 113 'diff --git a/PRESUBMIT.py b/pp\n' |
114 'similarity index 100%\n' | 114 'similarity index 100%\n' |
115 'copy from PRESUBMIT.py\n' | 115 'copy from PRESUBMIT.py\n' |
116 'copy to pp\n') | 116 'copy to pp\n') |
117 | 117 |
118 | 118 |
(...skipping 19 matching lines...) Expand all Loading... |
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, | 144 def _check_patch(self, |
145 p, | 145 p, |
146 filename, | 146 filename, |
147 diff, | 147 diff, |
| 148 source_filename=None, |
148 is_binary=False, | 149 is_binary=False, |
149 is_delete=False, | 150 is_delete=False, |
150 is_git_diff=False, | 151 is_git_diff=False, |
151 is_new=False, | 152 is_new=False, |
152 patchlevel=0, | 153 patchlevel=0, |
153 svn_properties=None): | 154 svn_properties=None): |
154 svn_properties = svn_properties or [] | 155 svn_properties = svn_properties or [] |
155 self.assertEquals(p.filename, filename) | 156 self.assertEquals(p.filename, filename) |
| 157 self.assertEquals(p.source_filename, source_filename) |
156 self.assertEquals(p.is_binary, is_binary) | 158 self.assertEquals(p.is_binary, is_binary) |
157 self.assertEquals(p.is_delete, is_delete) | 159 self.assertEquals(p.is_delete, is_delete) |
158 if hasattr(p, 'is_git_diff'): | 160 if hasattr(p, 'is_git_diff'): |
159 self.assertEquals(p.is_git_diff, is_git_diff) | 161 self.assertEquals(p.is_git_diff, is_git_diff) |
160 self.assertEquals(p.is_new, is_new) | 162 self.assertEquals(p.is_new, is_new) |
161 if hasattr(p, 'patchlevel'): | 163 if hasattr(p, 'patchlevel'): |
162 self.assertEquals(p.patchlevel, patchlevel) | 164 self.assertEquals(p.patchlevel, patchlevel) |
163 if diff: | 165 if diff: |
164 self.assertEquals(p.get(), diff) | 166 self.assertEquals(p.get(), diff) |
165 if hasattr(p, 'svn_properties'): | 167 if hasattr(p, 'svn_properties'): |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 self.assertEquals('', p.diff_hunks) | 373 self.assertEquals('', p.diff_hunks) |
372 self.assertEquals(text, p.get()) | 374 self.assertEquals(text, p.get()) |
373 | 375 |
374 def testRelPath(self): | 376 def testRelPath(self): |
375 patches = patch.PatchSet([ | 377 patches = patch.PatchSet([ |
376 patch.FilePatchDiff('chrome/file.cc', SVN_PATCH, []), | 378 patch.FilePatchDiff('chrome/file.cc', SVN_PATCH, []), |
377 patch.FilePatchDiff( | 379 patch.FilePatchDiff( |
378 'tools\\clang_check/README.chromium', GIT_DELETE, []), | 380 'tools\\clang_check/README.chromium', GIT_DELETE, []), |
379 patch.FilePatchDiff('tools/run_local_server.sh', GIT_RENAME, []), | 381 patch.FilePatchDiff('tools/run_local_server.sh', GIT_RENAME, []), |
380 patch.FilePatchDiff( | 382 patch.FilePatchDiff( |
381 'chrome\\browser/chromeos/views/webui_menu_widget.h', | 383 'chromeos\\views/webui_menu_widget.h', GIT_RENAME_PARTIAL, []), |
382 GIT_RENAME_PARTIAL, []), | |
383 patch.FilePatchDiff('pp', GIT_COPY, []), | 384 patch.FilePatchDiff('pp', GIT_COPY, []), |
384 patch.FilePatchDiff('foo', GIT_NEW, []), | 385 patch.FilePatchDiff('foo', GIT_NEW, []), |
385 patch.FilePatchDelete('other/place/foo', True), | 386 patch.FilePatchDelete('other/place/foo', True), |
386 patch.FilePatchBinary('bar', 'data', [], is_new=False), | 387 patch.FilePatchBinary('bar', 'data', [], is_new=False), |
387 ]) | 388 ]) |
388 expected = [ | 389 expected = [ |
389 'chrome/file.cc', 'tools/clang_check/README.chromium', | 390 'chrome/file.cc', 'tools/clang_check/README.chromium', |
390 'tools/run_local_server.sh', | 391 'tools/run_local_server.sh', |
391 'chrome/browser/chromeos/views/webui_menu_widget.h', 'pp', 'foo', | 392 'chromeos/views/webui_menu_widget.h', 'pp', 'foo', |
392 'other/place/foo', 'bar'] | 393 'other/place/foo', 'bar'] |
393 self.assertEquals(expected, patches.filenames) | 394 self.assertEquals(expected, patches.filenames) |
394 orig_name = patches.patches[0].filename | 395 orig_name = patches.patches[0].filename |
| 396 orig_source_name = patches.patches[0].source_filename or orig_name |
395 patches.set_relpath(os.path.join('a', 'bb')) | 397 patches.set_relpath(os.path.join('a', 'bb')) |
396 expected = [os.path.join('a', 'bb', x) for x in expected] | 398 expected = [os.path.join('a', 'bb', x) for x in expected] |
397 self.assertEquals(expected, patches.filenames) | 399 self.assertEquals(expected, patches.filenames) |
398 # Make sure each header is updated accordingly. | 400 # Make sure each header is updated accordingly. |
399 header = [] | 401 header = [] |
400 new_name = os.path.join('a', 'bb', orig_name) | 402 new_name = os.path.join('a', 'bb', orig_name) |
| 403 new_source_name = os.path.join('a', 'bb', orig_source_name) |
401 for line in SVN_PATCH.splitlines(True): | 404 for line in SVN_PATCH.splitlines(True): |
402 if line.startswith('@@'): | 405 if line.startswith('@@'): |
403 break | 406 break |
404 if line[:3] in ('---', '+++', 'Ind'): | 407 if line[:3] == '---': |
| 408 line = line.replace(orig_source_name, new_source_name) |
| 409 if line[:3] == '+++': |
405 line = line.replace(orig_name, new_name) | 410 line = line.replace(orig_name, new_name) |
406 header.append(line) | 411 header.append(line) |
407 header = ''.join(header) | 412 header = ''.join(header) |
408 self.assertEquals(header, patches.patches[0].diff_header) | 413 self.assertEquals(header, patches.patches[0].diff_header) |
409 | 414 |
410 def testRelPathBad(self): | 415 def testRelPathBad(self): |
411 patches = patch.PatchSet([ | 416 patches = patch.PatchSet([ |
412 patch.FilePatchDiff('chrome\\file.cc', SVN_PATCH, []), | 417 patch.FilePatchDiff('chrome\\file.cc', SVN_PATCH, []), |
413 patch.FilePatchDelete('other\\place\\foo', True), | 418 patch.FilePatchDelete('other\\place\\foo', True), |
414 ]) | 419 ]) |
415 try: | 420 try: |
416 patches.set_relpath('..') | 421 patches.set_relpath('..') |
417 self.fail() | 422 self.fail() |
418 except patch.UnsupportedPatchFormat: | 423 except patch.UnsupportedPatchFormat: |
419 pass | 424 pass |
420 | 425 |
421 def testRelPathEmpty(self): | 426 def testRelPathEmpty(self): |
422 patches = patch.PatchSet([ | 427 patches = patch.PatchSet([ |
423 patch.FilePatchDiff('chrome\\file.cc', SVN_PATCH, []), | 428 patch.FilePatchDiff('chrome\\file.cc', SVN_PATCH, []), |
424 patch.FilePatchDelete('other\\place\\foo', True), | 429 patch.FilePatchDelete('other\\place\\foo', True), |
425 ]) | 430 ]) |
426 patches.set_relpath('') | 431 patches.set_relpath('') |
427 self.assertEquals( | 432 self.assertEquals( |
428 ['chrome/file.cc', 'other/place/foo'], | 433 ['chrome/file.cc', 'other/place/foo'], |
429 [f.filename for f in patches]) | 434 [f.filename for f in patches]) |
| 435 self.assertEquals([None, None], [f.source_filename for f in patches]) |
430 | 436 |
431 def testBackSlash(self): | 437 def testBackSlash(self): |
432 mangled_patch = SVN_PATCH.replace('chrome/', 'chrome\\') | 438 mangled_patch = SVN_PATCH.replace('chrome/', 'chrome\\') |
433 patches = patch.PatchSet([ | 439 patches = patch.PatchSet([ |
434 patch.FilePatchDiff('chrome\\file.cc', mangled_patch, []), | 440 patch.FilePatchDiff('chrome\\file.cc', mangled_patch, []), |
435 patch.FilePatchDelete('other\\place\\foo', True), | 441 patch.FilePatchDelete('other\\place\\foo', True), |
436 ]) | 442 ]) |
437 expected = ['chrome/file.cc', 'other/place/foo'] | 443 expected = ['chrome/file.cc', 'other/place/foo'] |
438 self.assertEquals(expected, patches.filenames) | 444 self.assertEquals(expected, patches.filenames) |
439 self.assertEquals(SVN_PATCH, patches.patches[0].get()) | 445 self.assertEquals(SVN_PATCH, patches.patches[0].get()) |
440 | 446 |
441 def testDelete(self): | 447 def testDelete(self): |
442 p = patch.FilePatchDiff('tools/clang_check/README.chromium', DELETE, []) | 448 p = patch.FilePatchDiff('tools/clang_check/README.chromium', DELETE, []) |
443 self._check_patch( | 449 self._check_patch( |
444 p, 'tools/clang_check/README.chromium', DELETE, is_delete=True) | 450 p, 'tools/clang_check/README.chromium', DELETE, is_delete=True) |
445 | 451 |
446 def testGitDelete(self): | 452 def testGitDelete(self): |
447 p = patch.FilePatchDiff('tools/clang_check/README.chromium', GIT_DELETE, []) | 453 p = patch.FilePatchDiff('tools/clang_check/README.chromium', GIT_DELETE, []) |
448 self._check_patch( | 454 self._check_patch( |
449 p, 'tools/clang_check/README.chromium', GIT_DELETE, is_delete=True, | 455 p, 'tools/clang_check/README.chromium', GIT_DELETE, is_delete=True, |
450 is_git_diff=True, patchlevel=1) | 456 is_git_diff=True, patchlevel=1) |
451 | 457 |
452 def testGitRename(self): | 458 def testGitRename(self): |
453 p = patch.FilePatchDiff('tools/run_local_server.sh', GIT_RENAME, []) | 459 p = patch.FilePatchDiff('tools/run_local_server.sh', GIT_RENAME, []) |
454 self._check_patch(p, 'tools/run_local_server.sh', GIT_RENAME, | 460 self._check_patch(p, 'tools/run_local_server.sh', GIT_RENAME, |
455 is_git_diff=True, patchlevel=1) | 461 is_git_diff=True, patchlevel=1, |
| 462 source_filename='tools/run_local_server.PY') |
456 | 463 |
457 def testGitRenamePartial(self): | 464 def testGitRenamePartial(self): |
458 p = patch.FilePatchDiff( | 465 p = patch.FilePatchDiff( |
459 'chrome/browser/chromeos/views/webui_menu_widget.h', | 466 'chromeos/views/webui_menu_widget.h', GIT_RENAME_PARTIAL, []) |
460 GIT_RENAME_PARTIAL, []) | |
461 self._check_patch( | 467 self._check_patch( |
462 p, 'chrome/browser/chromeos/views/webui_menu_widget.h', | 468 p, 'chromeos/views/webui_menu_widget.h', GIT_RENAME_PARTIAL, |
463 GIT_RENAME_PARTIAL, is_git_diff=True, patchlevel=1) | 469 source_filename='chromeos/views/DOMui_menu_widget.h', is_git_diff=True, |
| 470 patchlevel=1) |
464 | 471 |
465 def testGitCopy(self): | 472 def testGitCopy(self): |
466 p = patch.FilePatchDiff('pp', GIT_COPY, []) | 473 p = patch.FilePatchDiff('pp', GIT_COPY, []) |
467 self._check_patch(p, 'pp', GIT_COPY, is_git_diff=True, patchlevel=1) | 474 self._check_patch(p, 'pp', GIT_COPY, is_git_diff=True, patchlevel=1, |
| 475 source_filename='PRESUBMIT.py') |
468 | 476 |
469 def testOnlyHeader(self): | 477 def testOnlyHeader(self): |
470 diff = '--- file_a\n+++ file_a\n' | 478 diff = '--- file_a\n+++ file_a\n' |
471 p = patch.FilePatchDiff('file_a', diff, []) | 479 p = patch.FilePatchDiff('file_a', diff, []) |
472 self._check_patch(p, 'file_a', diff) | 480 self._check_patch(p, 'file_a', diff) |
473 | 481 |
474 def testSmallest(self): | 482 def testSmallest(self): |
475 diff = '--- file_a\n+++ file_a\n@@ -0,0 +1 @@\n+foo\n' | 483 diff = '--- file_a\n+++ file_a\n@@ -0,0 +1 @@\n+foo\n' |
476 p = patch.FilePatchDiff('file_a', diff, []) | 484 p = patch.FilePatchDiff('file_a', diff, []) |
477 self._check_patch(p, 'file_a', diff) | 485 self._check_patch(p, 'file_a', diff) |
(...skipping 10 matching lines...) Expand all Loading... |
488 try: | 496 try: |
489 patch.FilePatchDiff('file_a', '+++ file_a\n--- file_a\n', []) | 497 patch.FilePatchDiff('file_a', '+++ file_a\n--- file_a\n', []) |
490 self.fail() | 498 self.fail() |
491 except patch.UnsupportedPatchFormat: | 499 except patch.UnsupportedPatchFormat: |
492 pass | 500 pass |
493 | 501 |
494 def testRenameOnlyHeader(self): | 502 def testRenameOnlyHeader(self): |
495 diff = '--- file_a\n+++ file_b\n' | 503 diff = '--- file_a\n+++ file_b\n' |
496 p = patch.FilePatchDiff('file_b', diff, []) | 504 p = patch.FilePatchDiff('file_b', diff, []) |
497 # Should it be marked as new? | 505 # Should it be marked as new? |
498 self._check_patch(p, 'file_b', diff) | 506 self._check_patch(p, 'file_b', diff, source_filename='file_a') |
499 | 507 |
500 def testGitCopyPartial(self): | 508 def testGitCopyPartial(self): |
501 diff = ( | 509 diff = ( |
502 'diff --git a/wtf b/wtf2\n' | 510 'diff --git a/wtf b/wtf2\n' |
503 'similarity index 98%\n' | 511 'similarity index 98%\n' |
504 'copy from wtf\n' | 512 'copy from wtf\n' |
505 'copy to wtf2\n' | 513 'copy to wtf2\n' |
506 'index 79fbaf3..3560689 100755\n' | 514 'index 79fbaf3..3560689 100755\n' |
507 '--- a/wtf\n' | 515 '--- a/wtf\n' |
508 '+++ b/wtf2\n' | 516 '+++ b/wtf2\n' |
509 '@@ -1,4 +1,4 @@\n' | 517 '@@ -1,4 +1,4 @@\n' |
510 '-#!/usr/bin/env python\n' | 518 '-#!/usr/bin/env python\n' |
511 '+#!/usr/bin/env python1.3\n' | 519 '+#!/usr/bin/env python1.3\n' |
512 ' # Copyright (c) 2010 The Chromium Authors. All rights reserved.\n' | 520 ' # Copyright (c) 2010 The Chromium Authors. All rights reserved.\n' |
513 ' # blah blah blah as\n' | 521 ' # blah blah blah as\n' |
514 ' # found in the LICENSE file.\n') | 522 ' # found in the LICENSE file.\n') |
515 p = patch.FilePatchDiff('wtf2', diff, []) | 523 p = patch.FilePatchDiff('wtf2', diff, []) |
516 # Should it be marked as new? | 524 # Should it be marked as new? |
517 self._check_patch(p, 'wtf2', diff, is_git_diff=True, patchlevel=1) | 525 self._check_patch( |
| 526 p, 'wtf2', diff, source_filename='wtf', is_git_diff=True, patchlevel=1) |
518 | 527 |
519 def testGitExe(self): | 528 def testGitExe(self): |
520 diff = ( | 529 diff = ( |
521 'diff --git a/natsort_test.py b/natsort_test.py\n' | 530 'diff --git a/natsort_test.py b/natsort_test.py\n' |
522 'new file mode 100755\n' | 531 'new file mode 100755\n' |
523 '--- /dev/null\n' | 532 '--- /dev/null\n' |
524 '+++ b/natsort_test.py\n' | 533 '+++ b/natsort_test.py\n' |
525 '@@ -0,0 +1,1 @@\n' | 534 '@@ -0,0 +1,1 @@\n' |
526 '+#!/usr/bin/env python\n') | 535 '+#!/usr/bin/env python\n') |
527 p = patch.FilePatchDiff('natsort_test.py', diff, []) | 536 p = patch.FilePatchDiff('natsort_test.py', diff, []) |
(...skipping 12 matching lines...) Expand all Loading... |
540 p = patch.FilePatchDiff('natsort_test.py', diff, []) | 549 p = patch.FilePatchDiff('natsort_test.py', diff, []) |
541 self._check_patch( | 550 self._check_patch( |
542 p, 'natsort_test.py', diff, is_new=True, is_git_diff=True, patchlevel=1) | 551 p, 'natsort_test.py', diff, is_new=True, is_git_diff=True, patchlevel=1) |
543 | 552 |
544 | 553 |
545 if __name__ == '__main__': | 554 if __name__ == '__main__': |
546 logging.basicConfig(level= | 555 logging.basicConfig(level= |
547 [logging.WARNING, logging.INFO, logging.DEBUG][ | 556 [logging.WARNING, logging.INFO, logging.DEBUG][ |
548 min(2, sys.argv.count('-v'))]) | 557 min(2, sys.argv.count('-v'))]) |
549 unittest.main() | 558 unittest.main() |
OLD | NEW |