| 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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 if hasattr(p, 'patchlevel'): | 163 if hasattr(p, 'patchlevel'): |
| 164 self.assertEquals(p.patchlevel, patchlevel) | 164 self.assertEquals(p.patchlevel, patchlevel) |
| 165 if diff: | 165 if diff: |
| 166 self.assertEquals(p.get(), diff) | 166 self.assertEquals(p.get(), diff) |
| 167 if hasattr(p, 'svn_properties'): | 167 if hasattr(p, 'svn_properties'): |
| 168 self.assertEquals(p.svn_properties, svn_properties) | 168 self.assertEquals(p.svn_properties, svn_properties) |
| 169 | 169 |
| 170 def testFilePatchDelete(self): | 170 def testFilePatchDelete(self): |
| 171 p = patch.FilePatchDelete('foo', False) | 171 p = patch.FilePatchDelete('foo', False) |
| 172 self._check_patch(p, 'foo', None, is_delete=True) | 172 self._check_patch(p, 'foo', None, is_delete=True) |
| 173 try: | |
| 174 p.get() | |
| 175 self.fail() | |
| 176 except NotImplementedError: | |
| 177 pass | |
| 178 | 173 |
| 179 def testFilePatchDeleteBin(self): | 174 def testFilePatchDeleteBin(self): |
| 180 p = patch.FilePatchDelete('foo', True) | 175 p = patch.FilePatchDelete('foo', True) |
| 181 self._check_patch(p, 'foo', None, is_delete=True, is_binary=True) | 176 self._check_patch(p, 'foo', None, is_delete=True, is_binary=True) |
| 182 try: | |
| 183 p.get() | |
| 184 self.fail() | |
| 185 except NotImplementedError: | |
| 186 pass | |
| 187 | 177 |
| 188 def testFilePatchBinary(self): | 178 def testFilePatchBinary(self): |
| 189 p = patch.FilePatchBinary('foo', 'data', [], is_new=False) | 179 p = patch.FilePatchBinary('foo', 'data', [], is_new=False) |
| 190 self._check_patch(p, 'foo', 'data', is_binary=True) | 180 self._check_patch(p, 'foo', 'data', is_binary=True) |
| 191 | 181 |
| 192 def testFilePatchBinaryNew(self): | 182 def testFilePatchBinaryNew(self): |
| 193 p = patch.FilePatchBinary('foo', 'data', [], is_new=True) | 183 p = patch.FilePatchBinary('foo', 'data', [], is_new=True) |
| 194 self._check_patch(p, 'foo', 'data', is_binary=True, is_new=True) | 184 self._check_patch(p, 'foo', 'data', is_binary=True, is_new=True) |
| 195 | 185 |
| 196 def testFilePatchDiff(self): | 186 def testFilePatchDiff(self): |
| (...skipping 25 matching lines...) Expand all Loading... |
| 222 # The code path is different for git and svn. | 212 # The code path is different for git and svn. |
| 223 p = patch.FilePatchDiff('foo', NEW, []) | 213 p = patch.FilePatchDiff('foo', NEW, []) |
| 224 self._check_patch(p, 'foo', NEW, is_new=True) | 214 self._check_patch(p, 'foo', NEW, is_new=True) |
| 225 | 215 |
| 226 def testFilePatchDiffGitNew(self): | 216 def testFilePatchDiffGitNew(self): |
| 227 # The code path is different for git and svn. | 217 # The code path is different for git and svn. |
| 228 p = patch.FilePatchDiff('foo', GIT_NEW, []) | 218 p = patch.FilePatchDiff('foo', GIT_NEW, []) |
| 229 self._check_patch( | 219 self._check_patch( |
| 230 p, 'foo', GIT_NEW, is_new=True, is_git_diff=True, patchlevel=1) | 220 p, 'foo', GIT_NEW, is_new=True, is_git_diff=True, patchlevel=1) |
| 231 | 221 |
| 232 def testFilePatchDiffBad(self): | |
| 233 try: | |
| 234 patch.FilePatchDiff('foo', 'data', []) | |
| 235 self.fail() | |
| 236 except patch.UnsupportedPatchFormat: | |
| 237 pass | |
| 238 | |
| 239 def testFilePatchDiffEmpty(self): | |
| 240 try: | |
| 241 patch.FilePatchDiff('foo', '', []) | |
| 242 self.fail() | |
| 243 except patch.UnsupportedPatchFormat: | |
| 244 pass | |
| 245 | |
| 246 def testFilePatchDiffNone(self): | |
| 247 try: | |
| 248 patch.FilePatchDiff('foo', None, []) | |
| 249 self.fail() | |
| 250 except patch.UnsupportedPatchFormat: | |
| 251 pass | |
| 252 | |
| 253 def testFilePatchBadDiffName(self): | |
| 254 try: | |
| 255 patch.FilePatchDiff('foo', SVN_PATCH, []) | |
| 256 self.fail() | |
| 257 except patch.UnsupportedPatchFormat, e: | |
| 258 self.assertEquals( | |
| 259 "Can't process patch for file foo.\nUnexpected diff: chrome/file.cc.", | |
| 260 str(e)) | |
| 261 | |
| 262 def testFilePatchDiffBadHeader(self): | |
| 263 try: | |
| 264 diff = ( | |
| 265 '+++ b/foo\n' | |
| 266 '@@ -0,0 +1 @@\n' | |
| 267 '+bar\n') | |
| 268 patch.FilePatchDiff('foo', diff, []) | |
| 269 self.fail() | |
| 270 except patch.UnsupportedPatchFormat: | |
| 271 pass | |
| 272 | |
| 273 def testFilePatchDiffBadGitHeader(self): | |
| 274 try: | |
| 275 diff = ( | |
| 276 'diff --git a/foo b/foo\n' | |
| 277 '+++ b/foo\n' | |
| 278 '@@ -0,0 +1 @@\n' | |
| 279 '+bar\n') | |
| 280 patch.FilePatchDiff('foo', diff, []) | |
| 281 self.fail() | |
| 282 except patch.UnsupportedPatchFormat: | |
| 283 pass | |
| 284 | |
| 285 def testFilePatchDiffBadHeaderReversed(self): | |
| 286 try: | |
| 287 diff = ( | |
| 288 '+++ b/foo\n' | |
| 289 '--- b/foo\n' | |
| 290 '@@ -0,0 +1 @@\n' | |
| 291 '+bar\n') | |
| 292 patch.FilePatchDiff('foo', diff, []) | |
| 293 self.fail() | |
| 294 except patch.UnsupportedPatchFormat: | |
| 295 pass | |
| 296 | |
| 297 def testFilePatchDiffGitBadHeaderReversed(self): | |
| 298 try: | |
| 299 diff = ( | |
| 300 'diff --git a/foo b/foo\n' | |
| 301 '+++ b/foo\n' | |
| 302 '--- b/foo\n' | |
| 303 '@@ -0,0 +1 @@\n' | |
| 304 '+bar\n') | |
| 305 patch.FilePatchDiff('foo', diff, []) | |
| 306 self.fail() | |
| 307 except patch.UnsupportedPatchFormat: | |
| 308 pass | |
| 309 | |
| 310 def testFilePatchDiffInvalidGit(self): | |
| 311 try: | |
| 312 patch.FilePatchDiff('svn_utils_test.txt', ( | |
| 313 'diff --git a/tests/svn_utils_test_data/svn_utils_test.txt ' | |
| 314 'b/tests/svn_utils_test_data/svn_utils_test.txt\n' | |
| 315 'index 0e4de76..8320059 100644\n' | |
| 316 '--- a/svn_utils_test.txt\n' | |
| 317 '+++ b/svn_utils_test.txt\n' | |
| 318 '@@ -3,6 +3,7 @@ bb\n' | |
| 319 'ccc\n' | |
| 320 'dd\n' | |
| 321 'e\n' | |
| 322 '+FOO!\n' | |
| 323 'ff\n' | |
| 324 'ggg\n' | |
| 325 'hh\n'), | |
| 326 []) | |
| 327 self.fail() | |
| 328 except patch.UnsupportedPatchFormat: | |
| 329 pass | |
| 330 try: | |
| 331 patch.FilePatchDiff('svn_utils_test2.txt', ( | |
| 332 'diff --git a/svn_utils_test_data/svn_utils_test.txt ' | |
| 333 'b/svn_utils_test.txt\n' | |
| 334 'index 0e4de76..8320059 100644\n' | |
| 335 '--- a/svn_utils_test.txt\n' | |
| 336 '+++ b/svn_utils_test.txt\n' | |
| 337 '@@ -3,6 +3,7 @@ bb\n' | |
| 338 'ccc\n' | |
| 339 'dd\n' | |
| 340 'e\n' | |
| 341 '+FOO!\n' | |
| 342 'ff\n' | |
| 343 'ggg\n' | |
| 344 'hh\n'), | |
| 345 []) | |
| 346 self.fail() | |
| 347 except patch.UnsupportedPatchFormat: | |
| 348 pass | |
| 349 | |
| 350 def testValidSvn(self): | 222 def testValidSvn(self): |
| 351 # pylint: disable=R0201 | 223 # pylint: disable=R0201 |
| 352 # Method could be a function | 224 # Method could be a function |
| 353 # Should not throw. | 225 # Should not throw. |
| 354 p = patch.FilePatchDiff('chrome/file.cc', SVN_PATCH, []) | 226 p = patch.FilePatchDiff('chrome/file.cc', SVN_PATCH, []) |
| 355 lines = SVN_PATCH.splitlines(True) | 227 lines = SVN_PATCH.splitlines(True) |
| 356 header = ''.join(lines[:4]) | 228 header = ''.join(lines[:4]) |
| 357 hunks = ''.join(lines[4:]) | 229 hunks = ''.join(lines[4:]) |
| 358 self.assertEquals(header, p.diff_header) | 230 self.assertEquals(header, p.diff_header) |
| 359 self.assertEquals(hunks, p.diff_hunks) | 231 self.assertEquals(hunks, p.diff_hunks) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 if line.startswith('@@'): | 277 if line.startswith('@@'): |
| 406 break | 278 break |
| 407 if line[:3] == '---': | 279 if line[:3] == '---': |
| 408 line = line.replace(orig_source_name, new_source_name) | 280 line = line.replace(orig_source_name, new_source_name) |
| 409 if line[:3] == '+++': | 281 if line[:3] == '+++': |
| 410 line = line.replace(orig_name, new_name) | 282 line = line.replace(orig_name, new_name) |
| 411 header.append(line) | 283 header.append(line) |
| 412 header = ''.join(header) | 284 header = ''.join(header) |
| 413 self.assertEquals(header, patches.patches[0].diff_header) | 285 self.assertEquals(header, patches.patches[0].diff_header) |
| 414 | 286 |
| 415 def testRelPathBad(self): | |
| 416 patches = patch.PatchSet([ | |
| 417 patch.FilePatchDiff('chrome\\file.cc', SVN_PATCH, []), | |
| 418 patch.FilePatchDelete('other\\place\\foo', True), | |
| 419 ]) | |
| 420 try: | |
| 421 patches.set_relpath('..') | |
| 422 self.fail() | |
| 423 except patch.UnsupportedPatchFormat: | |
| 424 pass | |
| 425 | |
| 426 def testRelPathEmpty(self): | 287 def testRelPathEmpty(self): |
| 427 patches = patch.PatchSet([ | 288 patches = patch.PatchSet([ |
| 428 patch.FilePatchDiff('chrome\\file.cc', SVN_PATCH, []), | 289 patch.FilePatchDiff('chrome\\file.cc', SVN_PATCH, []), |
| 429 patch.FilePatchDelete('other\\place\\foo', True), | 290 patch.FilePatchDelete('other\\place\\foo', True), |
| 430 ]) | 291 ]) |
| 431 patches.set_relpath('') | 292 patches.set_relpath('') |
| 432 self.assertEquals( | 293 self.assertEquals( |
| 433 ['chrome/file.cc', 'other/place/foo'], | 294 ['chrome/file.cc', 'other/place/foo'], |
| 434 [f.filename for f in patches]) | 295 [f.filename for f in patches]) |
| 435 self.assertEquals([None, None], [f.source_filename for f in patches]) | 296 self.assertEquals([None, None], [f.source_filename for f in patches]) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 477 def testOnlyHeader(self): | 338 def testOnlyHeader(self): |
| 478 diff = '--- file_a\n+++ file_a\n' | 339 diff = '--- file_a\n+++ file_a\n' |
| 479 p = patch.FilePatchDiff('file_a', diff, []) | 340 p = patch.FilePatchDiff('file_a', diff, []) |
| 480 self._check_patch(p, 'file_a', diff) | 341 self._check_patch(p, 'file_a', diff) |
| 481 | 342 |
| 482 def testSmallest(self): | 343 def testSmallest(self): |
| 483 diff = '--- file_a\n+++ file_a\n@@ -0,0 +1 @@\n+foo\n' | 344 diff = '--- file_a\n+++ file_a\n@@ -0,0 +1 @@\n+foo\n' |
| 484 p = patch.FilePatchDiff('file_a', diff, []) | 345 p = patch.FilePatchDiff('file_a', diff, []) |
| 485 self._check_patch(p, 'file_a', diff) | 346 self._check_patch(p, 'file_a', diff) |
| 486 | 347 |
| 487 def testInverted(self): | |
| 488 try: | |
| 489 patch.FilePatchDiff( | |
| 490 'file_a', '+++ file_a\n--- file_a\n@@ -0,0 +1 @@\n+foo\n', []) | |
| 491 self.fail() | |
| 492 except patch.UnsupportedPatchFormat: | |
| 493 pass | |
| 494 | |
| 495 def testInvertedOnlyHeader(self): | |
| 496 try: | |
| 497 patch.FilePatchDiff('file_a', '+++ file_a\n--- file_a\n', []) | |
| 498 self.fail() | |
| 499 except patch.UnsupportedPatchFormat: | |
| 500 pass | |
| 501 | |
| 502 def testRenameOnlyHeader(self): | 348 def testRenameOnlyHeader(self): |
| 503 diff = '--- file_a\n+++ file_b\n' | 349 diff = '--- file_a\n+++ file_b\n' |
| 504 p = patch.FilePatchDiff('file_b', diff, []) | 350 p = patch.FilePatchDiff('file_b', diff, []) |
| 505 # Should it be marked as new? | 351 # Should it be marked as new? |
| 506 self._check_patch(p, 'file_b', diff, source_filename='file_a', is_new=True) | 352 self._check_patch(p, 'file_b', diff, source_filename='file_a', is_new=True) |
| 507 | 353 |
| 508 def testGitCopyPartial(self): | 354 def testGitCopyPartial(self): |
| 509 diff = ( | 355 diff = ( |
| 510 'diff --git a/wtf b/wtf2\n' | 356 'diff --git a/wtf b/wtf2\n' |
| 511 'similarity index 98%\n' | 357 'similarity index 98%\n' |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 545 'new file mode 100644\n' | 391 'new file mode 100644\n' |
| 546 '--- /dev/null\n' | 392 '--- /dev/null\n' |
| 547 '+++ b/natsort_test.py\n' | 393 '+++ b/natsort_test.py\n' |
| 548 '@@ -0,0 +1,1 @@\n' | 394 '@@ -0,0 +1,1 @@\n' |
| 549 '+#!/usr/bin/env python\n') | 395 '+#!/usr/bin/env python\n') |
| 550 p = patch.FilePatchDiff('natsort_test.py', diff, []) | 396 p = patch.FilePatchDiff('natsort_test.py', diff, []) |
| 551 self._check_patch( | 397 self._check_patch( |
| 552 p, 'natsort_test.py', diff, is_new=True, is_git_diff=True, patchlevel=1) | 398 p, 'natsort_test.py', diff, is_new=True, is_git_diff=True, patchlevel=1) |
| 553 | 399 |
| 554 | 400 |
| 401 class PatchTestFail(unittest.TestCase): |
| 402 # All patches that should throw. |
| 403 def testFilePatchDelete(self): |
| 404 p = patch.FilePatchDelete('foo', False) |
| 405 try: |
| 406 p.get() |
| 407 self.fail() |
| 408 except NotImplementedError: |
| 409 pass |
| 410 |
| 411 def testFilePatchDeleteBin(self): |
| 412 p = patch.FilePatchDelete('foo', True) |
| 413 try: |
| 414 p.get() |
| 415 self.fail() |
| 416 except NotImplementedError: |
| 417 pass |
| 418 |
| 419 def testFilePatchDiffBad(self): |
| 420 try: |
| 421 patch.FilePatchDiff('foo', 'data', []) |
| 422 self.fail() |
| 423 except patch.UnsupportedPatchFormat: |
| 424 pass |
| 425 |
| 426 def testFilePatchDiffEmpty(self): |
| 427 try: |
| 428 patch.FilePatchDiff('foo', '', []) |
| 429 self.fail() |
| 430 except patch.UnsupportedPatchFormat: |
| 431 pass |
| 432 |
| 433 def testFilePatchDiffNone(self): |
| 434 try: |
| 435 patch.FilePatchDiff('foo', None, []) |
| 436 self.fail() |
| 437 except patch.UnsupportedPatchFormat: |
| 438 pass |
| 439 |
| 440 def testFilePatchBadDiffName(self): |
| 441 try: |
| 442 patch.FilePatchDiff('foo', SVN_PATCH, []) |
| 443 self.fail() |
| 444 except patch.UnsupportedPatchFormat, e: |
| 445 self.assertEquals( |
| 446 "Can't process patch for file foo.\nUnexpected diff: chrome/file.cc.", |
| 447 str(e)) |
| 448 |
| 449 def testFilePatchDiffBadHeader(self): |
| 450 try: |
| 451 diff = ( |
| 452 '+++ b/foo\n' |
| 453 '@@ -0,0 +1 @@\n' |
| 454 '+bar\n') |
| 455 patch.FilePatchDiff('foo', diff, []) |
| 456 self.fail() |
| 457 except patch.UnsupportedPatchFormat: |
| 458 pass |
| 459 |
| 460 def testFilePatchDiffBadGitHeader(self): |
| 461 try: |
| 462 diff = ( |
| 463 'diff --git a/foo b/foo\n' |
| 464 '+++ b/foo\n' |
| 465 '@@ -0,0 +1 @@\n' |
| 466 '+bar\n') |
| 467 patch.FilePatchDiff('foo', diff, []) |
| 468 self.fail() |
| 469 except patch.UnsupportedPatchFormat: |
| 470 pass |
| 471 |
| 472 def testFilePatchDiffBadHeaderReversed(self): |
| 473 try: |
| 474 diff = ( |
| 475 '+++ b/foo\n' |
| 476 '--- b/foo\n' |
| 477 '@@ -0,0 +1 @@\n' |
| 478 '+bar\n') |
| 479 patch.FilePatchDiff('foo', diff, []) |
| 480 self.fail() |
| 481 except patch.UnsupportedPatchFormat: |
| 482 pass |
| 483 |
| 484 def testFilePatchDiffGitBadHeaderReversed(self): |
| 485 try: |
| 486 diff = ( |
| 487 'diff --git a/foo b/foo\n' |
| 488 '+++ b/foo\n' |
| 489 '--- b/foo\n' |
| 490 '@@ -0,0 +1 @@\n' |
| 491 '+bar\n') |
| 492 patch.FilePatchDiff('foo', diff, []) |
| 493 self.fail() |
| 494 except patch.UnsupportedPatchFormat: |
| 495 pass |
| 496 |
| 497 def testFilePatchDiffInvalidGit(self): |
| 498 try: |
| 499 patch.FilePatchDiff('svn_utils_test.txt', ( |
| 500 'diff --git a/tests/svn_utils_test_data/svn_utils_test.txt ' |
| 501 'b/tests/svn_utils_test_data/svn_utils_test.txt\n' |
| 502 'index 0e4de76..8320059 100644\n' |
| 503 '--- a/svn_utils_test.txt\n' |
| 504 '+++ b/svn_utils_test.txt\n' |
| 505 '@@ -3,6 +3,7 @@ bb\n' |
| 506 'ccc\n' |
| 507 'dd\n' |
| 508 'e\n' |
| 509 '+FOO!\n' |
| 510 'ff\n' |
| 511 'ggg\n' |
| 512 'hh\n'), |
| 513 []) |
| 514 self.fail() |
| 515 except patch.UnsupportedPatchFormat: |
| 516 pass |
| 517 try: |
| 518 patch.FilePatchDiff('svn_utils_test2.txt', ( |
| 519 'diff --git a/svn_utils_test_data/svn_utils_test.txt ' |
| 520 'b/svn_utils_test.txt\n' |
| 521 'index 0e4de76..8320059 100644\n' |
| 522 '--- a/svn_utils_test.txt\n' |
| 523 '+++ b/svn_utils_test.txt\n' |
| 524 '@@ -3,6 +3,7 @@ bb\n' |
| 525 'ccc\n' |
| 526 'dd\n' |
| 527 'e\n' |
| 528 '+FOO!\n' |
| 529 'ff\n' |
| 530 'ggg\n' |
| 531 'hh\n'), |
| 532 []) |
| 533 self.fail() |
| 534 except patch.UnsupportedPatchFormat: |
| 535 pass |
| 536 |
| 537 def testRelPathBad(self): |
| 538 patches = patch.PatchSet([ |
| 539 patch.FilePatchDiff('chrome\\file.cc', SVN_PATCH, []), |
| 540 patch.FilePatchDelete('other\\place\\foo', True), |
| 541 ]) |
| 542 try: |
| 543 patches.set_relpath('..') |
| 544 self.fail() |
| 545 except patch.UnsupportedPatchFormat: |
| 546 pass |
| 547 |
| 548 def testInverted(self): |
| 549 try: |
| 550 patch.FilePatchDiff( |
| 551 'file_a', '+++ file_a\n--- file_a\n@@ -0,0 +1 @@\n+foo\n', []) |
| 552 self.fail() |
| 553 except patch.UnsupportedPatchFormat: |
| 554 pass |
| 555 |
| 556 def testInvertedOnlyHeader(self): |
| 557 try: |
| 558 patch.FilePatchDiff('file_a', '+++ file_a\n--- file_a\n', []) |
| 559 self.fail() |
| 560 except patch.UnsupportedPatchFormat: |
| 561 pass |
| 562 |
| 563 |
| 555 if __name__ == '__main__': | 564 if __name__ == '__main__': |
| 556 logging.basicConfig(level= | 565 logging.basicConfig(level= |
| 557 [logging.WARNING, logging.INFO, logging.DEBUG][ | 566 [logging.WARNING, logging.INFO, logging.DEBUG][ |
| 558 min(2, sys.argv.count('-v'))]) | 567 min(2, sys.argv.count('-v'))]) |
| 559 unittest.main() | 568 unittest.main() |
| OLD | NEW |