Chromium Code Reviews| 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 presubmit_support.py and presubmit_canned_checks.py.""" | 6 """Unit tests for presubmit_support.py and presubmit_canned_checks.py.""" |
| 7 | 7 |
| 8 # pylint is too confused. | 8 # pylint is too confused. |
| 9 # pylint: disable=E1101,E1103,R0201,W0212,W0403 | 9 # pylint: disable=E1101,E1103,R0201,W0212,W0403 |
| 10 | 10 |
| (...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 266 presubmit.scm.SVN.CaptureInfo(binary).AndReturn( | 266 presubmit.scm.SVN.CaptureInfo(binary).AndReturn( |
| 267 {'URL': 'svn:/foo/binary.dll'}) | 267 {'URL': 'svn:/foo/binary.dll'}) |
| 268 presubmit.scm.SVN.CaptureInfo(notfound).AndReturn({}) | 268 presubmit.scm.SVN.CaptureInfo(notfound).AndReturn({}) |
| 269 presubmit.scm.SVN.CaptureInfo(flap).AndReturn( | 269 presubmit.scm.SVN.CaptureInfo(flap).AndReturn( |
| 270 {'URL': 'svn:/foo/boo/flap.h'}) | 270 {'URL': 'svn:/foo/boo/flap.h'}) |
| 271 presubmit.scm.SVN.GenerateDiff([blat]).AndReturn(self.presubmit_diffs) | 271 presubmit.scm.SVN.GenerateDiff([blat]).AndReturn(self.presubmit_diffs) |
| 272 presubmit.scm.SVN.GenerateDiff([notfound]).AndReturn(self.presubmit_diffs) | 272 presubmit.scm.SVN.GenerateDiff([notfound]).AndReturn(self.presubmit_diffs) |
| 273 | 273 |
| 274 self.mox.ReplayAll() | 274 self.mox.ReplayAll() |
| 275 | 275 |
| 276 change = presubmit.SvnChange('mychange', '\n'.join(description_lines), | 276 change = presubmit.SvnChange( |
| 277 self.fake_root_dir, files, 0, 0) | 277 'mychange', '\n'.join(description_lines), |
|
Dirk Pranke
2011/04/12 20:49:24
Nit: if you're going to 1-per-line, maybe this sho
| |
| 278 self.fake_root_dir, | |
| 279 files, | |
| 280 0, | |
| 281 0, | |
| 282 None) | |
| 278 self.failUnless(change.Name() == 'mychange') | 283 self.failUnless(change.Name() == 'mychange') |
| 279 self.failUnless(change.DescriptionText() == | 284 self.failUnless(change.DescriptionText() == |
| 280 'Hello there\nthis is a change\nand some more regular text') | 285 'Hello there\nthis is a change\nand some more regular text') |
| 281 self.failUnless(change.FullDescriptionText() == | 286 self.failUnless(change.FullDescriptionText() == |
| 282 '\n'.join(description_lines)) | 287 '\n'.join(description_lines)) |
| 283 | 288 |
| 284 self.failUnless(change.BUG == '123') | 289 self.failUnless(change.BUG == '123') |
| 285 self.failUnless(change.STORY == 'http://foo/') | 290 self.failUnless(change.STORY == 'http://foo/') |
| 286 self.failUnless(change.BLEH == None) | 291 self.failUnless(change.BLEH == None) |
| 287 | 292 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 336 def testExecPresubmitScript(self): | 341 def testExecPresubmitScript(self): |
| 337 description_lines = ('Hello there', | 342 description_lines = ('Hello there', |
| 338 'this is a change', | 343 'this is a change', |
| 339 'STORY=http://tracker/123') | 344 'STORY=http://tracker/123') |
| 340 files = [ | 345 files = [ |
| 341 ['A', 'foo\\blat.cc'], | 346 ['A', 'foo\\blat.cc'], |
| 342 ] | 347 ] |
| 343 fake_presubmit = presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py') | 348 fake_presubmit = presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py') |
| 344 self.mox.ReplayAll() | 349 self.mox.ReplayAll() |
| 345 | 350 |
| 346 change = presubmit.Change('mychange', '\n'.join(description_lines), | 351 change = presubmit.Change( |
|
Dirk Pranke
2011/04/12 20:49:24
maybe you should pull this into a helper routine (
M-A Ruel
2011/04/12 23:13:24
I thought about that but didn't feel like doing it
| |
| 347 self.fake_root_dir, files, 0, 0) | 352 'mychange', |
| 353 '\n'.join(description_lines), | |
| 354 self.fake_root_dir, | |
| 355 files, | |
| 356 0, | |
| 357 0, | |
| 358 None) | |
| 348 executer = presubmit.PresubmitExecuter(change, False, False, None, False) | 359 executer = presubmit.PresubmitExecuter(change, False, False, None, False) |
| 349 self.failIf(executer.ExecPresubmitScript('', fake_presubmit)) | 360 self.failIf(executer.ExecPresubmitScript('', fake_presubmit)) |
| 350 # No error if no on-upload entry point | 361 # No error if no on-upload entry point |
| 351 self.failIf(executer.ExecPresubmitScript( | 362 self.failIf(executer.ExecPresubmitScript( |
| 352 ('def CheckChangeOnCommit(input_api, output_api):\n' | 363 ('def CheckChangeOnCommit(input_api, output_api):\n' |
| 353 ' return (output_api.PresubmitError("!!"))\n'), | 364 ' return (output_api.PresubmitError("!!"))\n'), |
| 354 fake_presubmit | 365 fake_presubmit |
| 355 )) | 366 )) |
| 356 | 367 |
| 357 executer = presubmit.PresubmitExecuter(change, True, False, None, False) | 368 executer = presubmit.PresubmitExecuter(change, True, False, None, False) |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 408 presubmit.os.path.isfile(root_path).AndReturn(True) | 419 presubmit.os.path.isfile(root_path).AndReturn(True) |
| 409 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) | 420 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) |
| 410 presubmit.gclient_utils.FileRead(root_path, | 421 presubmit.gclient_utils.FileRead(root_path, |
| 411 'rU').AndReturn(self.presubmit_text) | 422 'rU').AndReturn(self.presubmit_text) |
| 412 presubmit.gclient_utils.FileRead(haspresubmit_path, | 423 presubmit.gclient_utils.FileRead(haspresubmit_path, |
| 413 'rU').AndReturn(self.presubmit_text) | 424 'rU').AndReturn(self.presubmit_text) |
| 414 presubmit.random.randint(0, 4).AndReturn(1) | 425 presubmit.random.randint(0, 4).AndReturn(1) |
| 415 self.mox.ReplayAll() | 426 self.mox.ReplayAll() |
| 416 | 427 |
| 417 input_buf = StringIO.StringIO('y\n') | 428 input_buf = StringIO.StringIO('y\n') |
| 418 change = presubmit.Change('mychange', '\n'.join(description_lines), | 429 change = presubmit.Change( |
| 419 self.fake_root_dir, files, 0, 0) | 430 'mychange', |
| 431 '\n'.join(description_lines), | |
| 432 self.fake_root_dir, | |
| 433 files, | |
| 434 0, | |
| 435 0, | |
| 436 None) | |
| 420 output = presubmit.DoPresubmitChecks( | 437 output = presubmit.DoPresubmitChecks( |
| 421 change, False, True, None, input_buf, None, False, False, None) | 438 change, False, True, None, input_buf, None, False, False, None) |
| 422 self.failIf(output.should_continue()) | 439 self.failIf(output.should_continue()) |
| 423 self.assertEqual(output.getvalue().count('!!'), 2) | 440 self.assertEqual(output.getvalue().count('!!'), 2) |
| 424 self.assertEqual(output.getvalue().count( | 441 self.assertEqual(output.getvalue().count( |
| 425 'Running presubmit upload checks ...\n'), 1) | 442 'Running presubmit upload checks ...\n'), 1) |
| 426 | 443 |
| 427 def testDoPresubmitChecksPromptsAfterWarnings(self): | 444 def testDoPresubmitChecksPromptsAfterWarnings(self): |
| 428 join = presubmit.os.path.join | 445 join = presubmit.os.path.join |
| 429 description_lines = ('Hello there', | 446 description_lines = ('Hello there', |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 442 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) | 459 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) |
| 443 presubmit.gclient_utils.FileRead(presubmit_path, 'rU' | 460 presubmit.gclient_utils.FileRead(presubmit_path, 'rU' |
| 444 ).AndReturn(self.presubmit_text) | 461 ).AndReturn(self.presubmit_text) |
| 445 presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU' | 462 presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU' |
| 446 ).AndReturn(self.presubmit_text) | 463 ).AndReturn(self.presubmit_text) |
| 447 presubmit.random.randint(0, 4).AndReturn(1) | 464 presubmit.random.randint(0, 4).AndReturn(1) |
| 448 presubmit.random.randint(0, 4).AndReturn(1) | 465 presubmit.random.randint(0, 4).AndReturn(1) |
| 449 self.mox.ReplayAll() | 466 self.mox.ReplayAll() |
| 450 | 467 |
| 451 input_buf = StringIO.StringIO('n\n') # say no to the warning | 468 input_buf = StringIO.StringIO('n\n') # say no to the warning |
| 452 change = presubmit.Change('mychange', '\n'.join(description_lines), | 469 change = presubmit.Change( |
| 453 self.fake_root_dir, files, 0, 0) | 470 'mychange', |
| 471 '\n'.join(description_lines), | |
| 472 self.fake_root_dir, | |
| 473 files, | |
| 474 0, | |
| 475 0, | |
| 476 None) | |
| 454 output = presubmit.DoPresubmitChecks( | 477 output = presubmit.DoPresubmitChecks( |
| 455 change, False, True, None, input_buf, None, True, False, None) | 478 change, False, True, None, input_buf, None, True, False, None) |
| 456 self.failIf(output.should_continue()) | 479 self.failIf(output.should_continue()) |
| 457 self.assertEqual(output.getvalue().count('??'), 2) | 480 self.assertEqual(output.getvalue().count('??'), 2) |
| 458 | 481 |
| 459 input_buf = StringIO.StringIO('y\n') # say yes to the warning | 482 input_buf = StringIO.StringIO('y\n') # say yes to the warning |
| 460 output = presubmit.DoPresubmitChecks( | 483 output = presubmit.DoPresubmitChecks( |
| 461 change, False, True, None, input_buf, None, True, False, None) | 484 change, False, True, None, input_buf, None, True, False, None) |
| 462 self.failUnless(output.should_continue()) | 485 self.failUnless(output.should_continue()) |
| 463 self.assertEquals(output.getvalue().count('??'), 2) | 486 self.assertEquals(output.getvalue().count('??'), 2) |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 481 presubmit.os.path.isfile(inherit_path).AndReturn(False) | 504 presubmit.os.path.isfile(inherit_path).AndReturn(False) |
| 482 presubmit.os.path.isfile(presubmit_path).AndReturn(True) | 505 presubmit.os.path.isfile(presubmit_path).AndReturn(True) |
| 483 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) | 506 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) |
| 484 presubmit.gclient_utils.FileRead(presubmit_path, 'rU' | 507 presubmit.gclient_utils.FileRead(presubmit_path, 'rU' |
| 485 ).AndReturn(self.presubmit_text) | 508 ).AndReturn(self.presubmit_text) |
| 486 presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU').AndReturn( | 509 presubmit.gclient_utils.FileRead(haspresubmit_path, 'rU').AndReturn( |
| 487 self.presubmit_text) | 510 self.presubmit_text) |
| 488 presubmit.random.randint(0, 4).AndReturn(1) | 511 presubmit.random.randint(0, 4).AndReturn(1) |
| 489 self.mox.ReplayAll() | 512 self.mox.ReplayAll() |
| 490 | 513 |
| 491 change = presubmit.Change('mychange', '\n'.join(description_lines), | 514 change = presubmit.Change( |
| 492 self.fake_root_dir, files, 0, 0) | 515 'mychange', |
| 516 '\n'.join(description_lines), | |
| 517 self.fake_root_dir, | |
| 518 files, | |
| 519 0, | |
| 520 0, | |
| 521 None) | |
| 493 output = presubmit.DoPresubmitChecks(change, False, True, None, None, | 522 output = presubmit.DoPresubmitChecks(change, False, True, None, None, |
| 494 None, False, False, None) | 523 None, False, False, None) |
| 495 self.assertEqual(output.getvalue().count('??'), 2) | 524 self.assertEqual(output.getvalue().count('??'), 2) |
| 496 self.assertEqual(output.getvalue().count('XX!!XX'), 2) | 525 self.assertEqual(output.getvalue().count('XX!!XX'), 2) |
| 497 self.assertEqual(output.getvalue().count('(y/N)'), 0) | 526 self.assertEqual(output.getvalue().count('(y/N)'), 0) |
| 498 self.assertEqual(output.getvalue().count( | 527 self.assertEqual(output.getvalue().count( |
| 499 'Running presubmit upload checks ...\n'), 1) | 528 'Running presubmit upload checks ...\n'), 1) |
| 500 | 529 |
| 501 def testDoDefaultPresubmitChecksAndFeedback(self): | 530 def testDoDefaultPresubmitChecksAndFeedback(self): |
| 502 join = presubmit.os.path.join | 531 join = presubmit.os.path.join |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 518 presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py') | 547 presubmit.os.path.isfile(join(self.fake_root_dir, 'PRESUBMIT.py') |
| 519 ).AndReturn(False) | 548 ).AndReturn(False) |
| 520 presubmit.os.path.isfile(join(self.fake_root_dir, | 549 presubmit.os.path.isfile(join(self.fake_root_dir, |
| 521 'haspresubmit', | 550 'haspresubmit', |
| 522 'PRESUBMIT.py')).AndReturn(False) | 551 'PRESUBMIT.py')).AndReturn(False) |
| 523 presubmit.random.randint(0, 4).AndReturn(0) | 552 presubmit.random.randint(0, 4).AndReturn(0) |
| 524 self.mox.ReplayAll() | 553 self.mox.ReplayAll() |
| 525 | 554 |
| 526 input_buf = StringIO.StringIO('y\n') | 555 input_buf = StringIO.StringIO('y\n') |
| 527 # Always fail. | 556 # Always fail. |
| 528 change = presubmit.Change('mychange', '\n'.join(description_lines), | 557 change = presubmit.Change( |
| 529 self.fake_root_dir, files, 0, 0) | 558 'mychange', |
| 559 '\n'.join(description_lines), | |
| 560 self.fake_root_dir, | |
| 561 files, | |
| 562 0, | |
| 563 0, | |
| 564 None) | |
| 530 output = presubmit.DoPresubmitChecks( | 565 output = presubmit.DoPresubmitChecks( |
| 531 change, False, True, None, input_buf, DEFAULT_SCRIPT, False, False, | 566 change, False, True, None, input_buf, DEFAULT_SCRIPT, False, False, |
| 532 None) | 567 None) |
| 533 self.failIf(output.should_continue()) | 568 self.failIf(output.should_continue()) |
| 534 text = ('Running presubmit upload checks ...\n' | 569 text = ('Running presubmit upload checks ...\n' |
| 535 'Warning, no presubmit.py found.\n' | 570 'Warning, no presubmit.py found.\n' |
| 536 'Running default presubmit script.\n' | 571 'Running default presubmit script.\n' |
| 537 '\n' | 572 '\n' |
| 538 '** Presubmit ERRORS **\n!!\n\n' | 573 '** Presubmit ERRORS **\n!!\n\n' |
| 539 'Was the presubmit check useful? Please send feedback & hate mail ' | 574 'Was the presubmit check useful? Please send feedback & hate mail ' |
| 540 'to maruel@chromium.org!\n') | 575 'to maruel@chromium.org!\n') |
| 541 self.assertEquals(output.getvalue(), text) | 576 self.assertEquals(output.getvalue(), text) |
| 542 | 577 |
| 543 def testDirectoryHandling(self): | 578 def testDirectoryHandling(self): |
| 544 files = [ | 579 files = [ |
| 545 ['A', 'isdir'], | 580 ['A', 'isdir'], |
| 546 ['A', presubmit.os.path.join('isdir', 'blat.cc')], | 581 ['A', presubmit.os.path.join('isdir', 'blat.cc')], |
| 547 ] | 582 ] |
| 548 isdir = presubmit.os.path.join(self.fake_root_dir, 'isdir') | 583 isdir = presubmit.os.path.join(self.fake_root_dir, 'isdir') |
| 549 blat = presubmit.os.path.join(isdir, 'blat.cc') | 584 blat = presubmit.os.path.join(isdir, 'blat.cc') |
| 550 presubmit.os.path.exists(isdir).AndReturn(True) | 585 presubmit.os.path.exists(isdir).AndReturn(True) |
| 551 presubmit.os.path.isdir(isdir).AndReturn(True) | 586 presubmit.os.path.isdir(isdir).AndReturn(True) |
| 552 presubmit.os.path.exists(blat).AndReturn(True) | 587 presubmit.os.path.exists(blat).AndReturn(True) |
| 553 presubmit.os.path.isdir(blat).AndReturn(False) | 588 presubmit.os.path.isdir(blat).AndReturn(False) |
| 554 self.mox.ReplayAll() | 589 self.mox.ReplayAll() |
| 555 | 590 |
| 556 change = presubmit.Change('mychange', 'foo', self.fake_root_dir, files, | 591 change = presubmit.Change( |
| 557 0, 0) | 592 'mychange', 'foo', self.fake_root_dir, files, 0, 0, None) |
| 558 affected_files = change.AffectedFiles(include_dirs=False) | 593 affected_files = change.AffectedFiles(include_dirs=False) |
| 559 self.failUnless(len(affected_files) == 1) | 594 self.failUnless(len(affected_files) == 1) |
| 560 self.failUnless(affected_files[0].LocalPath().endswith('blat.cc')) | 595 self.failUnless(affected_files[0].LocalPath().endswith('blat.cc')) |
| 561 affected_files_and_dirs = change.AffectedFiles(include_dirs=True) | 596 affected_files_and_dirs = change.AffectedFiles(include_dirs=True) |
| 562 self.failUnless(len(affected_files_and_dirs) == 2) | 597 self.failUnless(len(affected_files_and_dirs) == 2) |
| 563 | 598 |
| 564 def testTags(self): | 599 def testTags(self): |
| 565 DEFAULT_SCRIPT = """ | 600 DEFAULT_SCRIPT = """ |
| 566 def CheckChangeOnUpload(input_api, output_api): | 601 def CheckChangeOnUpload(input_api, output_api): |
| 567 if input_api.change.tags['BUG'] != 'boo': | 602 if input_api.change.tags['BUG'] != 'boo': |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 594 """ | 629 """ |
| 595 presubmit.random.randint(0, 4).AndReturn(1) | 630 presubmit.random.randint(0, 4).AndReturn(1) |
| 596 inherit_path = presubmit.os.path.join(self.fake_root_dir, | 631 inherit_path = presubmit.os.path.join(self.fake_root_dir, |
| 597 self._INHERIT_SETTINGS) | 632 self._INHERIT_SETTINGS) |
| 598 presubmit.os.path.isfile(inherit_path).AndReturn(False) | 633 presubmit.os.path.isfile(inherit_path).AndReturn(False) |
| 599 self.mox.ReplayAll() | 634 self.mox.ReplayAll() |
| 600 | 635 |
| 601 output = StringIO.StringIO() | 636 output = StringIO.StringIO() |
| 602 input_buf = StringIO.StringIO('y\n') | 637 input_buf = StringIO.StringIO('y\n') |
| 603 change = presubmit.Change( | 638 change = presubmit.Change( |
| 604 'foo', "Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n", | 639 'foo', |
| 605 self.fake_root_dir, None, 0, 0) | 640 'Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n', |
| 641 self.fake_root_dir, | |
| 642 None, | |
| 643 0, | |
| 644 0, | |
| 645 None) | |
| 606 self.failUnless(presubmit.DoPresubmitChecks( | 646 self.failUnless(presubmit.DoPresubmitChecks( |
| 607 change, False, True, output, input_buf, DEFAULT_SCRIPT, False, False, | 647 change, False, True, output, input_buf, DEFAULT_SCRIPT, False, False, |
| 608 None)) | 648 None)) |
| 609 self.assertEquals(output.getvalue(), | 649 self.assertEquals(output.getvalue(), |
| 610 ('Running presubmit upload checks ...\n' | 650 ('Running presubmit upload checks ...\n' |
| 611 'Warning, no presubmit.py found.\n' | 651 'Warning, no presubmit.py found.\n' |
| 612 'Running default presubmit script.\n' | 652 'Running default presubmit script.\n' |
| 613 '\n' | 653 '\n' |
| 614 '** Presubmit Messages **\n' | 654 '** Presubmit Messages **\n' |
| 615 'http://tracker.com/42\n' | 655 'http://tracker.com/42\n' |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 820 'application/octet-stream') | 860 'application/octet-stream') |
| 821 presubmit.scm.SVN.GetFileProperty(weird, 'svn:mime-type').AndReturn(None) | 861 presubmit.scm.SVN.GetFileProperty(weird, 'svn:mime-type').AndReturn(None) |
| 822 presubmit.scm.SVN.GetFileProperty(another, 'svn:mime-type').AndReturn(None) | 862 presubmit.scm.SVN.GetFileProperty(another, 'svn:mime-type').AndReturn(None) |
| 823 presubmit.scm.SVN.GetFileProperty(third_party, 'svn:mime-type' | 863 presubmit.scm.SVN.GetFileProperty(third_party, 'svn:mime-type' |
| 824 ).AndReturn(None) | 864 ).AndReturn(None) |
| 825 presubmit.scm.SVN.GenerateDiff([blat]).AndReturn(self.presubmit_diffs) | 865 presubmit.scm.SVN.GenerateDiff([blat]).AndReturn(self.presubmit_diffs) |
| 826 presubmit.scm.SVN.GenerateDiff([another]).AndReturn(self.presubmit_diffs) | 866 presubmit.scm.SVN.GenerateDiff([another]).AndReturn(self.presubmit_diffs) |
| 827 | 867 |
| 828 self.mox.ReplayAll() | 868 self.mox.ReplayAll() |
| 829 | 869 |
| 830 change = presubmit.SvnChange('mychange', '\n'.join(description_lines), | 870 change = presubmit.SvnChange( |
| 831 self.fake_root_dir, files, 0, 0) | 871 'mychange', |
| 872 '\n'.join(description_lines), | |
| 873 self.fake_root_dir, | |
| 874 files, | |
| 875 0, | |
| 876 0, | |
| 877 None) | |
| 832 input_api = presubmit.InputApi( | 878 input_api = presubmit.InputApi( |
| 833 change, | 879 change, |
| 834 join(self.fake_root_dir, 'foo', 'PRESUBMIT.py'), | 880 join(self.fake_root_dir, 'foo', 'PRESUBMIT.py'), |
| 835 False, False, None, False) | 881 False, False, None, False) |
| 836 # Doesn't filter much | 882 # Doesn't filter much |
| 837 got_files = input_api.AffectedFiles() | 883 got_files = input_api.AffectedFiles() |
| 838 self.assertEquals(len(got_files), 7) | 884 self.assertEquals(len(got_files), 7) |
| 839 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1])) | 885 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1])) |
| 840 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1])) | 886 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1])) |
| 841 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1])) | 887 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1])) |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 938 def FilterSourceFile(affected_file): | 984 def FilterSourceFile(affected_file): |
| 939 return 'a' in affected_file.LocalPath() | 985 return 'a' in affected_file.LocalPath() |
| 940 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee')] | 986 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee')] |
| 941 for _, item in files: | 987 for _, item in files: |
| 942 item = presubmit.os.path.join(self.fake_root_dir, item) | 988 item = presubmit.os.path.join(self.fake_root_dir, item) |
| 943 presubmit.os.path.exists(item).AndReturn(True) | 989 presubmit.os.path.exists(item).AndReturn(True) |
| 944 presubmit.os.path.isdir(item).AndReturn(False) | 990 presubmit.os.path.isdir(item).AndReturn(False) |
| 945 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) | 991 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) |
| 946 self.mox.ReplayAll() | 992 self.mox.ReplayAll() |
| 947 | 993 |
| 948 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0, | 994 change = presubmit.SvnChange( |
| 949 0) | 995 'mychange', '', self.fake_root_dir, files, 0, 0, None) |
| 950 input_api = presubmit.InputApi( | 996 input_api = presubmit.InputApi( |
| 951 change, | 997 change, |
| 952 presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py'), | 998 presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py'), |
| 953 False, False, None, False) | 999 False, False, None, False) |
| 954 got_files = input_api.AffectedSourceFiles(FilterSourceFile) | 1000 got_files = input_api.AffectedSourceFiles(FilterSourceFile) |
| 955 self.assertEquals(len(got_files), 2) | 1001 self.assertEquals(len(got_files), 2) |
| 956 self.assertEquals(got_files[0].LocalPath(), 'eeaee') | 1002 self.assertEquals(got_files[0].LocalPath(), 'eeaee') |
| 957 self.assertEquals(got_files[1].LocalPath(), 'eeabee') | 1003 self.assertEquals(got_files[1].LocalPath(), 'eeabee') |
| 958 | 1004 |
| 959 def testLambdaFilter(self): | 1005 def testLambdaFilter(self): |
| 960 white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",) | 1006 white_list = presubmit.InputApi.DEFAULT_BLACK_LIST + (r".*?a.*?",) |
| 961 black_list = [r".*?b.*?"] | 1007 black_list = [r".*?b.*?"] |
| 962 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')] | 1008 files = [('A', 'eeaee'), ('M', 'eeabee'), ('M', 'eebcee'), ('M', 'eecaee')] |
| 963 for _, item in files: | 1009 for _, item in files: |
| 964 item = presubmit.os.path.join(self.fake_root_dir, item) | 1010 item = presubmit.os.path.join(self.fake_root_dir, item) |
| 965 presubmit.os.path.exists(item).AndReturn(True) | 1011 presubmit.os.path.exists(item).AndReturn(True) |
| 966 presubmit.os.path.isdir(item).AndReturn(False) | 1012 presubmit.os.path.isdir(item).AndReturn(False) |
| 967 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) | 1013 presubmit.scm.SVN.GetFileProperty(item, 'svn:mime-type').AndReturn(None) |
| 968 self.mox.ReplayAll() | 1014 self.mox.ReplayAll() |
| 969 | 1015 |
| 970 change = presubmit.SvnChange('mychange', '', self.fake_root_dir, files, 0, | 1016 change = presubmit.SvnChange( |
| 971 0) | 1017 'mychange', '', self.fake_root_dir, files, 0, 0, None) |
| 972 input_api = presubmit.InputApi( | 1018 input_api = presubmit.InputApi( |
| 973 change, './PRESUBMIT.py', False, False, None, False) | 1019 change, './PRESUBMIT.py', False, False, None, False) |
| 974 # Sample usage of overiding the default white and black lists. | 1020 # Sample usage of overiding the default white and black lists. |
| 975 got_files = input_api.AffectedSourceFiles( | 1021 got_files = input_api.AffectedSourceFiles( |
| 976 lambda x: input_api.FilterSourceFile(x, white_list, black_list)) | 1022 lambda x: input_api.FilterSourceFile(x, white_list, black_list)) |
| 977 self.assertEquals(len(got_files), 2) | 1023 self.assertEquals(len(got_files), 2) |
| 978 self.assertEquals(got_files[0].LocalPath(), 'eeaee') | 1024 self.assertEquals(got_files[0].LocalPath(), 'eeaee') |
| 979 self.assertEquals(got_files[1].LocalPath(), 'eecaee') | 1025 self.assertEquals(got_files[1].LocalPath(), 'eecaee') |
| 980 | 1026 |
| 981 def testGetAbsoluteLocalPath(self): | 1027 def testGetAbsoluteLocalPath(self): |
| 982 join = presubmit.os.path.join | 1028 join = presubmit.os.path.join |
| 983 normpath = presubmit.normpath | 1029 normpath = presubmit.normpath |
| 984 # Regression test for bug of presubmit stuff that relies on invoking | 1030 # Regression test for bug of presubmit stuff that relies on invoking |
| 985 # SVN (e.g. to get mime type of file) not working unless gcl invoked | 1031 # SVN (e.g. to get mime type of file) not working unless gcl invoked |
| 986 # from the client root (e.g. if you were at 'src' and did 'cd base' before | 1032 # from the client root (e.g. if you were at 'src' and did 'cd base' before |
| 987 # invoking 'gcl upload' it would fail because svn wouldn't find the files | 1033 # invoking 'gcl upload' it would fail because svn wouldn't find the files |
| 988 # the presubmit script was asking about). | 1034 # the presubmit script was asking about). |
| 989 files = [ | 1035 files = [ |
| 990 ['A', 'isdir'], | 1036 ['A', 'isdir'], |
| 991 ['A', join('isdir', 'blat.cc')], | 1037 ['A', join('isdir', 'blat.cc')], |
| 992 ['M', join('elsewhere', 'ouf.cc')], | 1038 ['M', join('elsewhere', 'ouf.cc')], |
| 993 ] | 1039 ] |
| 994 self.mox.ReplayAll() | 1040 self.mox.ReplayAll() |
| 995 | 1041 |
| 996 change = presubmit.Change('mychange', '', self.fake_root_dir, files, 0, 0) | 1042 change = presubmit.Change( |
| 1043 'mychange', '', self.fake_root_dir, files, 0, 0, None) | |
| 997 affected_files = change.AffectedFiles(include_dirs=True) | 1044 affected_files = change.AffectedFiles(include_dirs=True) |
| 998 # Local paths should remain the same | 1045 # Local paths should remain the same |
| 999 self.assertEquals(affected_files[0].LocalPath(), normpath('isdir')) | 1046 self.assertEquals(affected_files[0].LocalPath(), normpath('isdir')) |
| 1000 self.assertEquals(affected_files[1].LocalPath(), normpath('isdir/blat.cc')) | 1047 self.assertEquals(affected_files[1].LocalPath(), normpath('isdir/blat.cc')) |
| 1001 # Absolute paths should be prefixed | 1048 # Absolute paths should be prefixed |
| 1002 self.assertEquals(affected_files[0].AbsoluteLocalPath(), | 1049 self.assertEquals(affected_files[0].AbsoluteLocalPath(), |
| 1003 normpath(join(self.fake_root_dir, 'isdir'))) | 1050 normpath(join(self.fake_root_dir, 'isdir'))) |
| 1004 self.assertEquals(affected_files[1].AbsoluteLocalPath(), | 1051 self.assertEquals(affected_files[1].AbsoluteLocalPath(), |
| 1005 normpath(join(self.fake_root_dir, 'isdir/blat.cc'))) | 1052 normpath(join(self.fake_root_dir, 'isdir/blat.cc'))) |
| 1006 | 1053 |
| 1007 # New helper functions need to work | 1054 # New helper functions need to work |
| 1008 paths_from_change = change.AbsoluteLocalPaths(include_dirs=True) | 1055 paths_from_change = change.AbsoluteLocalPaths(include_dirs=True) |
| 1009 self.assertEqual(len(paths_from_change), 3) | 1056 self.assertEqual(len(paths_from_change), 3) |
| 1010 presubmit_path = join(self.fake_root_dir, 'isdir', 'PRESUBMIT.py') | 1057 presubmit_path = join(self.fake_root_dir, 'isdir', 'PRESUBMIT.py') |
| 1011 api = presubmit.InputApi( | 1058 api = presubmit.InputApi( |
| 1012 change=change, presubmit_path=presubmit_path, | 1059 change=change, presubmit_path=presubmit_path, |
| 1013 is_committing=True, tbr=False, rietveld=None, verbose=False) | 1060 is_committing=True, tbr=False, rietveld=None, verbose=False) |
| 1014 paths_from_api = api.AbsoluteLocalPaths(include_dirs=True) | 1061 paths_from_api = api.AbsoluteLocalPaths(include_dirs=True) |
| 1015 self.assertEqual(len(paths_from_api), 2) | 1062 self.assertEqual(len(paths_from_api), 2) |
| 1016 for absolute_paths in [paths_from_change, paths_from_api]: | 1063 for absolute_paths in [paths_from_change, paths_from_api]: |
| 1017 self.assertEqual(absolute_paths[0], | 1064 self.assertEqual(absolute_paths[0], |
| 1018 normpath(join(self.fake_root_dir, 'isdir'))) | 1065 normpath(join(self.fake_root_dir, 'isdir'))) |
| 1019 self.assertEqual(absolute_paths[1], | 1066 self.assertEqual(absolute_paths[1], |
| 1020 normpath(join(self.fake_root_dir, 'isdir', 'blat.cc'))) | 1067 normpath(join(self.fake_root_dir, 'isdir', 'blat.cc'))) |
| 1021 | 1068 |
| 1022 def testDeprecated(self): | 1069 def testDeprecated(self): |
| 1023 presubmit.warn(mox.IgnoreArg(), category=mox.IgnoreArg(), stacklevel=2) | 1070 presubmit.warn(mox.IgnoreArg(), category=mox.IgnoreArg(), stacklevel=2) |
| 1024 self.mox.ReplayAll() | 1071 self.mox.ReplayAll() |
| 1025 | 1072 |
| 1026 change = presubmit.Change('mychange', '', self.fake_root_dir, [], 0, 0) | 1073 change = presubmit.Change( |
| 1074 'mychange', '', self.fake_root_dir, [], 0, 0, None) | |
| 1027 api = presubmit.InputApi( | 1075 api = presubmit.InputApi( |
| 1028 change, | 1076 change, |
| 1029 presubmit.os.path.join(self.fake_root_dir, 'foo', 'PRESUBMIT.py'), True, | 1077 presubmit.os.path.join(self.fake_root_dir, 'foo', 'PRESUBMIT.py'), True, |
| 1030 False, None, False) | 1078 False, None, False) |
| 1031 api.AffectedTextFiles(include_deletes=False) | 1079 api.AffectedTextFiles(include_deletes=False) |
| 1032 | 1080 |
| 1033 def testReadFileStringDenied(self): | 1081 def testReadFileStringDenied(self): |
| 1034 self.mox.ReplayAll() | 1082 self.mox.ReplayAll() |
| 1035 | 1083 |
| 1036 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1084 change = presubmit.Change( |
| 1037 0, 0) | 1085 'foo', 'foo', self.fake_root_dir, [('M', 'AA')], 0, 0, None) |
| 1038 input_api = presubmit.InputApi( | 1086 input_api = presubmit.InputApi( |
| 1039 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, | 1087 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
| 1040 False, None, False) | 1088 False, None, False) |
| 1041 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x') | 1089 self.assertRaises(IOError, input_api.ReadFile, 'boo', 'x') |
| 1042 | 1090 |
| 1043 def testReadFileStringAccepted(self): | 1091 def testReadFileStringAccepted(self): |
| 1044 path = presubmit.os.path.join(self.fake_root_dir, 'AA/boo') | 1092 path = presubmit.os.path.join(self.fake_root_dir, 'AA/boo') |
| 1045 presubmit.gclient_utils.FileRead(path, 'x').AndReturn(None) | 1093 presubmit.gclient_utils.FileRead(path, 'x').AndReturn(None) |
| 1046 self.mox.ReplayAll() | 1094 self.mox.ReplayAll() |
| 1047 | 1095 |
| 1048 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1096 change = presubmit.Change( |
| 1049 0, 0) | 1097 'foo', 'foo', self.fake_root_dir, [('M', 'AA')], 0, 0, None) |
| 1050 input_api = presubmit.InputApi( | 1098 input_api = presubmit.InputApi( |
| 1051 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, | 1099 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
| 1052 False, None, False) | 1100 False, None, False) |
| 1053 input_api.ReadFile(path, 'x') | 1101 input_api.ReadFile(path, 'x') |
| 1054 | 1102 |
| 1055 def testReadFileAffectedFileDenied(self): | 1103 def testReadFileAffectedFileDenied(self): |
| 1056 fileobj = presubmit.AffectedFile('boo', 'M', 'Unrelated') | 1104 fileobj = presubmit.AffectedFile('boo', 'M', 'Unrelated') |
| 1057 self.mox.ReplayAll() | 1105 self.mox.ReplayAll() |
| 1058 | 1106 |
| 1059 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1107 change = presubmit.Change( |
| 1060 0, 0) | 1108 'foo', 'foo', self.fake_root_dir, [('M', 'AA')], 0, 0, None) |
| 1061 input_api = presubmit.InputApi( | 1109 input_api = presubmit.InputApi( |
| 1062 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, | 1110 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
| 1063 False, None, False) | 1111 False, None, False) |
| 1064 self.assertRaises(IOError, input_api.ReadFile, fileobj, 'x') | 1112 self.assertRaises(IOError, input_api.ReadFile, fileobj, 'x') |
| 1065 | 1113 |
| 1066 def testReadFileAffectedFileAccepted(self): | 1114 def testReadFileAffectedFileAccepted(self): |
| 1067 fileobj = presubmit.AffectedFile('AA/boo', 'M', self.fake_root_dir) | 1115 fileobj = presubmit.AffectedFile('AA/boo', 'M', self.fake_root_dir) |
| 1068 presubmit.gclient_utils.FileRead(fileobj.AbsoluteLocalPath(), 'x' | 1116 presubmit.gclient_utils.FileRead(fileobj.AbsoluteLocalPath(), 'x' |
| 1069 ).AndReturn(None) | 1117 ).AndReturn(None) |
| 1070 self.mox.ReplayAll() | 1118 self.mox.ReplayAll() |
| 1071 | 1119 |
| 1072 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1120 change = presubmit.Change( |
| 1073 0, 0) | 1121 'foo', 'foo', self.fake_root_dir, [('M', 'AA')], 0, 0, None) |
| 1074 input_api = presubmit.InputApi( | 1122 input_api = presubmit.InputApi( |
| 1075 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, | 1123 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
| 1076 False, None, False) | 1124 False, None, False) |
| 1077 input_api.ReadFile(fileobj, 'x') | 1125 input_api.ReadFile(fileobj, 'x') |
| 1078 | 1126 |
| 1079 | 1127 |
| 1080 class OuputApiUnittest(PresubmitTestsBase): | 1128 class OuputApiUnittest(PresubmitTestsBase): |
| 1081 """Tests presubmit.OutputApi.""" | 1129 """Tests presubmit.OutputApi.""" |
| 1082 def testMembersChanged(self): | 1130 def testMembersChanged(self): |
| 1083 self.mox.ReplayAll() | 1131 self.mox.ReplayAll() |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1215 presubmit.scm.SVN.GetFileProperty(blat, 'svn:mime-type').AndReturn(None) | 1263 presubmit.scm.SVN.GetFileProperty(blat, 'svn:mime-type').AndReturn(None) |
| 1216 presubmit.scm.SVN.GetFileProperty(blob, 'svn:mime-type' | 1264 presubmit.scm.SVN.GetFileProperty(blob, 'svn:mime-type' |
| 1217 ).AndReturn('application/octet-stream') | 1265 ).AndReturn('application/octet-stream') |
| 1218 self.mox.ReplayAll() | 1266 self.mox.ReplayAll() |
| 1219 | 1267 |
| 1220 output = filter(lambda x: x.IsTextFile(), files) | 1268 output = filter(lambda x: x.IsTextFile(), files) |
| 1221 self.failUnless(len(output) == 1) | 1269 self.failUnless(len(output) == 1) |
| 1222 self.failUnless(files[0] == output[0]) | 1270 self.failUnless(files[0] == output[0]) |
| 1223 | 1271 |
| 1224 | 1272 |
| 1225 class GclChangeUnittest(PresubmitTestsBase): | 1273 class ChangeUnittest(PresubmitTestsBase): |
| 1226 def testMembersChanged(self): | 1274 def testMembersChanged(self): |
| 1227 members = [ | 1275 members = [ |
| 1228 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', | 1276 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', |
| 1229 'DescriptionText', 'FullDescriptionText', 'LocalPaths', 'Name', | 1277 'DescriptionText', 'FullDescriptionText', 'LocalPaths', 'Name', |
| 1230 'RepositoryRoot', 'RightHandSideLines', 'ServerPaths', | 1278 'RepositoryRoot', 'RightHandSideLines', 'ServerPaths', |
| 1231 'issue', 'patchset', 'scm', 'tags', | 1279 'author_email', 'issue', 'patchset', 'scm', 'tags', |
| 1232 ] | 1280 ] |
| 1233 # If this test fails, you should add the relevant test. | 1281 # If this test fails, you should add the relevant test. |
| 1234 self.mox.ReplayAll() | 1282 self.mox.ReplayAll() |
| 1235 | 1283 |
| 1236 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1284 change = presubmit.Change( |
| 1237 0, 0) | 1285 'foo', 'foo', self.fake_root_dir, [('M', 'AA')], 0, 0, 'foo') |
| 1238 self.compareMembers(change, members) | 1286 self.compareMembers(change, members) |
| 1239 | 1287 |
| 1288 def testMembers(self): | |
| 1289 change = presubmit.Change( | |
| 1290 'foo1', 'foo2\nDRU=ro', self.fake_root_dir, [('Y', 'AA')], 3, 5, 'foo3') | |
| 1291 self.assertEquals('foo1', change.Name()) | |
| 1292 self.assertEquals('foo2', change.DescriptionText()) | |
| 1293 self.assertEquals('foo3', change.author_email) | |
| 1294 self.assertEquals('ro', change.DRU) | |
| 1295 self.assertEquals(3, change.issue) | |
| 1296 self.assertEquals(5, change.patchset) | |
| 1297 self.assertEquals(self.fake_root_dir, change.RepositoryRoot()) | |
| 1298 self.assertEquals(1, len(change.AffectedFiles(include_dirs=True))) | |
| 1299 self.assertEquals('Y', change.AffectedFiles(include_dirs=True)[0].Action()) | |
| 1300 | |
| 1240 | 1301 |
| 1241 class CannedChecksUnittest(PresubmitTestsBase): | 1302 class CannedChecksUnittest(PresubmitTestsBase): |
| 1242 """Tests presubmit_canned_checks.py.""" | 1303 """Tests presubmit_canned_checks.py.""" |
| 1243 | 1304 |
| 1244 def setUp(self): | 1305 def setUp(self): |
| 1245 PresubmitTestsBase.setUp(self) | 1306 PresubmitTestsBase.setUp(self) |
| 1246 | 1307 |
| 1247 def MockInputApi(self, change, committing): | 1308 def MockInputApi(self, change, committing): |
| 1248 input_api = self.mox.CreateMock(presubmit.InputApi) | 1309 input_api = self.mox.CreateMock(presubmit.InputApi) |
| 1249 input_api.cStringIO = presubmit.cStringIO | 1310 input_api.cStringIO = presubmit.cStringIO |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1290 'CheckSvnModifiedDirectories', | 1351 'CheckSvnModifiedDirectories', |
| 1291 'CheckSvnForCommonMimeTypes', 'CheckSvnProperty', | 1352 'CheckSvnForCommonMimeTypes', 'CheckSvnProperty', |
| 1292 'RunPythonUnitTests', 'RunPylint', | 1353 'RunPythonUnitTests', 'RunPylint', |
| 1293 'RunUnitTests', 'RunUnitTestsInDirectory', | 1354 'RunUnitTests', 'RunUnitTestsInDirectory', |
| 1294 ] | 1355 ] |
| 1295 # If this test fails, you should add the relevant test. | 1356 # If this test fails, you should add the relevant test. |
| 1296 self.compareMembers(presubmit_canned_checks, members) | 1357 self.compareMembers(presubmit_canned_checks, members) |
| 1297 | 1358 |
| 1298 def DescriptionTest(self, check, description1, description2, error_type, | 1359 def DescriptionTest(self, check, description1, description2, error_type, |
| 1299 committing): | 1360 committing): |
| 1300 change1 = presubmit.Change('foo1', description1, self.fake_root_dir, None, | 1361 change1 = presubmit.Change( |
| 1301 0, 0) | 1362 'foo1', description1, self.fake_root_dir, None, 0, 0, None) |
| 1302 input_api1 = self.MockInputApi(change1, committing) | 1363 input_api1 = self.MockInputApi(change1, committing) |
| 1303 change2 = presubmit.Change('foo2', description2, self.fake_root_dir, None, | 1364 change2 = presubmit.Change( |
| 1304 0, 0) | 1365 'foo2', description2, self.fake_root_dir, None, 0, 0, None) |
| 1305 input_api2 = self.MockInputApi(change2, committing) | 1366 input_api2 = self.MockInputApi(change2, committing) |
| 1306 self.mox.ReplayAll() | 1367 self.mox.ReplayAll() |
| 1307 | 1368 |
| 1308 results1 = check(input_api1, presubmit.OutputApi) | 1369 results1 = check(input_api1, presubmit.OutputApi) |
| 1309 self.assertEquals(results1, []) | 1370 self.assertEquals(results1, []) |
| 1310 results2 = check(input_api2, presubmit.OutputApi) | 1371 results2 = check(input_api2, presubmit.OutputApi) |
| 1311 self.assertEquals(len(results2), 1) | 1372 self.assertEquals(len(results2), 1) |
| 1312 self.assertEquals(results2[0].__class__, error_type) | 1373 self.assertEquals(results2[0].__class__, error_type) |
| 1313 | 1374 |
| 1314 def ContentTest(self, check, content1, content2, error_type): | 1375 def ContentTest(self, check, content1, content2, error_type): |
| 1315 change1 = presubmit.Change('foo1', 'foo1\n', self.fake_root_dir, None, | 1376 change1 = presubmit.Change( |
| 1316 0, 0) | 1377 'foo1', 'foo1\n', self.fake_root_dir, None, 0, 0, None) |
| 1317 input_api1 = self.MockInputApi(change1, False) | 1378 input_api1 = self.MockInputApi(change1, False) |
| 1318 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1379 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 1319 affected_file.LocalPath().AndReturn('foo.cc') | 1380 affected_file.LocalPath().AndReturn('foo.cc') |
| 1320 # Format is (file, line number, line content) | 1381 # Format is (file, line number, line content) |
| 1321 output1 = [ | 1382 output1 = [ |
| 1322 (affected_file, 42, 'yo, ' + content1), | 1383 (affected_file, 42, 'yo, ' + content1), |
| 1323 (affected_file, 43, 'yer'), | 1384 (affected_file, 43, 'yer'), |
| 1324 (affected_file, 23, 'ya'), | 1385 (affected_file, 23, 'ya'), |
| 1325 ] | 1386 ] |
| 1326 input_api1.RightHandSideLines(mox.IgnoreArg()).AndReturn(output1) | 1387 input_api1.RightHandSideLines(mox.IgnoreArg()).AndReturn(output1) |
| 1327 change2 = presubmit.Change('foo2', 'foo2\n', self.fake_root_dir, None, | 1388 change2 = presubmit.Change( |
| 1328 0, 0) | 1389 'foo2', 'foo2\n', self.fake_root_dir, None, 0, 0, None) |
| 1329 input_api2 = self.MockInputApi(change2, False) | 1390 input_api2 = self.MockInputApi(change2, False) |
| 1330 output2 = [ | 1391 output2 = [ |
| 1331 (affected_file, 42, 'yo, ' + content2), | 1392 (affected_file, 42, 'yo, ' + content2), |
| 1332 (affected_file, 43, 'yer'), | 1393 (affected_file, 43, 'yer'), |
| 1333 (affected_file, 23, 'ya'), | 1394 (affected_file, 23, 'ya'), |
| 1334 ] | 1395 ] |
| 1335 input_api2.RightHandSideLines(mox.IgnoreArg()).AndReturn(output2) | 1396 input_api2.RightHandSideLines(mox.IgnoreArg()).AndReturn(output2) |
| 1336 self.mox.ReplayAll() | 1397 self.mox.ReplayAll() |
| 1337 | 1398 |
| 1338 results1 = check(input_api1, presubmit.OutputApi, None) | 1399 results1 = check(input_api1, presubmit.OutputApi, None) |
| 1339 self.assertEquals(results1, []) | 1400 self.assertEquals(results1, []) |
| 1340 results2 = check(input_api2, presubmit.OutputApi, None) | 1401 results2 = check(input_api2, presubmit.OutputApi, None) |
| 1341 self.assertEquals(len(results2), 1) | 1402 self.assertEquals(len(results2), 1) |
| 1342 self.assertEquals(results2[0].__class__, error_type) | 1403 self.assertEquals(results2[0].__class__, error_type) |
| 1343 | 1404 |
| 1344 def ReadFileTest(self, check, content1, content2, error_type): | 1405 def ReadFileTest(self, check, content1, content2, error_type): |
| 1345 self.mox.StubOutWithMock(presubmit.InputApi, 'ReadFile') | 1406 self.mox.StubOutWithMock(presubmit.InputApi, 'ReadFile') |
| 1346 change1 = presubmit.Change('foo1', 'foo1\n', self.fake_root_dir, None, | 1407 change1 = presubmit.Change( |
| 1347 0, 0) | 1408 'foo1', 'foo1\n', self.fake_root_dir, None, 0, 0, None) |
| 1348 input_api1 = self.MockInputApi(change1, False) | 1409 input_api1 = self.MockInputApi(change1, False) |
| 1349 affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1410 affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 1350 input_api1.AffectedSourceFiles(None).AndReturn([affected_file1]) | 1411 input_api1.AffectedSourceFiles(None).AndReturn([affected_file1]) |
| 1351 input_api1.ReadFile(affected_file1, 'rb').AndReturn(content1) | 1412 input_api1.ReadFile(affected_file1, 'rb').AndReturn(content1) |
| 1352 change2 = presubmit.Change('foo2', 'foo2\n', self.fake_root_dir, None, | 1413 change2 = presubmit.Change( |
| 1353 0, 0) | 1414 'foo2', 'foo2\n', self.fake_root_dir, None, 0, 0, None) |
| 1354 input_api2 = self.MockInputApi(change2, False) | 1415 input_api2 = self.MockInputApi(change2, False) |
| 1355 affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1416 affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 1356 input_api2.AffectedSourceFiles(None).AndReturn([affected_file2]) | 1417 input_api2.AffectedSourceFiles(None).AndReturn([affected_file2]) |
| 1357 input_api2.ReadFile(affected_file2, 'rb').AndReturn(content2) | 1418 input_api2.ReadFile(affected_file2, 'rb').AndReturn(content2) |
| 1358 affected_file2.LocalPath().AndReturn('bar.cc') | 1419 affected_file2.LocalPath().AndReturn('bar.cc') |
| 1359 self.mox.ReplayAll() | 1420 self.mox.ReplayAll() |
| 1360 | 1421 |
| 1361 results = check(input_api1, presubmit.OutputApi) | 1422 results = check(input_api1, presubmit.OutputApi) |
| 1362 self.assertEquals(results, []) | 1423 self.assertEquals(results, []) |
| 1363 results2 = check(input_api2, presubmit.OutputApi) | 1424 results2 = check(input_api2, presubmit.OutputApi) |
| 1364 self.assertEquals(len(results2), 1) | 1425 self.assertEquals(len(results2), 1) |
| 1365 self.assertEquals(results2[0].__class__, error_type) | 1426 self.assertEquals(results2[0].__class__, error_type) |
| 1366 | 1427 |
| 1367 def SvnPropertyTest(self, check, property_name, value1, value2, committing, | 1428 def SvnPropertyTest(self, check, property_name, value1, value2, committing, |
| 1368 error_type, use_source_file): | 1429 error_type, use_source_file): |
| 1369 change1 = presubmit.SvnChange('mychange', '', self.fake_root_dir, [], 0, 0) | 1430 change1 = presubmit.SvnChange( |
| 1431 'mychange', '', self.fake_root_dir, [], 0, 0, None) | |
| 1370 input_api1 = self.MockInputApi(change1, committing) | 1432 input_api1 = self.MockInputApi(change1, committing) |
| 1371 files1 = [ | 1433 files1 = [ |
| 1372 presubmit.SvnAffectedFile('foo/bar.cc', 'A'), | 1434 presubmit.SvnAffectedFile('foo/bar.cc', 'A'), |
| 1373 presubmit.SvnAffectedFile('foo.cc', 'M'), | 1435 presubmit.SvnAffectedFile('foo.cc', 'M'), |
| 1374 ] | 1436 ] |
| 1375 if use_source_file: | 1437 if use_source_file: |
| 1376 input_api1.AffectedSourceFiles(None).AndReturn(files1) | 1438 input_api1.AffectedSourceFiles(None).AndReturn(files1) |
| 1377 else: | 1439 else: |
| 1378 input_api1.AffectedFiles(include_deleted=False).AndReturn(files1) | 1440 input_api1.AffectedFiles(include_deleted=False).AndReturn(files1) |
| 1379 presubmit.scm.SVN.GetFileProperty(presubmit.normpath('foo/bar.cc'), | 1441 presubmit.scm.SVN.GetFileProperty(presubmit.normpath('foo/bar.cc'), |
| 1380 property_name).AndReturn(value1) | 1442 property_name).AndReturn(value1) |
| 1381 presubmit.scm.SVN.GetFileProperty(presubmit.normpath('foo.cc'), | 1443 presubmit.scm.SVN.GetFileProperty(presubmit.normpath('foo.cc'), |
| 1382 property_name).AndReturn(value1) | 1444 property_name).AndReturn(value1) |
| 1383 change2 = presubmit.SvnChange('mychange', '', self.fake_root_dir, [], 0, 0) | 1445 change2 = presubmit.SvnChange( |
| 1446 'mychange', '', self.fake_root_dir, [], 0, 0, None) | |
| 1384 input_api2 = self.MockInputApi(change2, committing) | 1447 input_api2 = self.MockInputApi(change2, committing) |
| 1385 files2 = [ | 1448 files2 = [ |
| 1386 presubmit.SvnAffectedFile('foo/bar.cc', 'A'), | 1449 presubmit.SvnAffectedFile('foo/bar.cc', 'A'), |
| 1387 presubmit.SvnAffectedFile('foo.cc', 'M'), | 1450 presubmit.SvnAffectedFile('foo.cc', 'M'), |
| 1388 ] | 1451 ] |
| 1389 if use_source_file: | 1452 if use_source_file: |
| 1390 input_api2.AffectedSourceFiles(None).AndReturn(files2) | 1453 input_api2.AffectedSourceFiles(None).AndReturn(files2) |
| 1391 else: | 1454 else: |
| 1392 input_api2.AffectedFiles(include_deleted=False).AndReturn(files2) | 1455 input_api2.AffectedFiles(include_deleted=False).AndReturn(files2) |
| 1393 | 1456 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1482 self.ContentTest(presubmit_canned_checks.CheckChangeTodoHasOwner, | 1545 self.ContentTest(presubmit_canned_checks.CheckChangeTodoHasOwner, |
| 1483 "TODO(foo): bar", "TODO: bar", | 1546 "TODO(foo): bar", "TODO: bar", |
| 1484 presubmit.OutputApi.PresubmitPromptWarning) | 1547 presubmit.OutputApi.PresubmitPromptWarning) |
| 1485 | 1548 |
| 1486 def testCannedCheckChangeHasNoTabs(self): | 1549 def testCannedCheckChangeHasNoTabs(self): |
| 1487 self.ContentTest(presubmit_canned_checks.CheckChangeHasNoTabs, | 1550 self.ContentTest(presubmit_canned_checks.CheckChangeHasNoTabs, |
| 1488 'blah blah', 'blah\tblah', | 1551 'blah blah', 'blah\tblah', |
| 1489 presubmit.OutputApi.PresubmitPromptWarning) | 1552 presubmit.OutputApi.PresubmitPromptWarning) |
| 1490 | 1553 |
| 1491 # Make sure makefiles are ignored. | 1554 # Make sure makefiles are ignored. |
| 1492 change1 = presubmit.Change('foo1', 'foo1\n', self.fake_root_dir, None, | 1555 change1 = presubmit.Change( |
| 1493 0, 0) | 1556 'foo1', 'foo1\n', self.fake_root_dir, None, 0, 0, None) |
| 1494 input_api1 = self.MockInputApi(change1, False) | 1557 input_api1 = self.MockInputApi(change1, False) |
| 1495 affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1558 affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 1496 affected_file1.LocalPath().AndReturn('foo.cc') | 1559 affected_file1.LocalPath().AndReturn('foo.cc') |
| 1497 affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1560 affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 1498 affected_file2.LocalPath().AndReturn('foo/Makefile') | 1561 affected_file2.LocalPath().AndReturn('foo/Makefile') |
| 1499 affected_file3 = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1562 affected_file3 = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 1500 affected_file3.LocalPath().AndReturn('makefile') | 1563 affected_file3.LocalPath().AndReturn('makefile') |
| 1501 # Only this one will trigger. | 1564 # Only this one will trigger. |
| 1502 affected_file4 = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1565 affected_file4 = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 1503 affected_file4.LocalPath().AndReturn('makefile.foo') | 1566 affected_file4.LocalPath().AndReturn('makefile.foo') |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2001 def testCannedCheckOwners_WithoutOwnerLGTM(self): | 2064 def testCannedCheckOwners_WithoutOwnerLGTM(self): |
| 2002 self.AssertOwnersWorks(uncovered_files=set(['foo.cc']), | 2065 self.AssertOwnersWorks(uncovered_files=set(['foo.cc']), |
| 2003 expected_output='Missing LGTM from an OWNER for: foo.cc\n') | 2066 expected_output='Missing LGTM from an OWNER for: foo.cc\n') |
| 2004 | 2067 |
| 2005 def testCannedCheckOwners_WithLGTMs(self): | 2068 def testCannedCheckOwners_WithLGTMs(self): |
| 2006 self.AssertOwnersWorks(approvers=set(['ben@example.com']), | 2069 self.AssertOwnersWorks(approvers=set(['ben@example.com']), |
| 2007 uncovered_files=set()) | 2070 uncovered_files=set()) |
| 2008 | 2071 |
| 2009 def testCannedRunUnitTests(self): | 2072 def testCannedRunUnitTests(self): |
| 2010 change = presubmit.Change( | 2073 change = presubmit.Change( |
| 2011 'foo1', 'description1', self.fake_root_dir, None, 0, 0) | 2074 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) |
| 2012 input_api = self.MockInputApi(change, False) | 2075 input_api = self.MockInputApi(change, False) |
| 2013 input_api.verbose = True | 2076 input_api.verbose = True |
| 2014 unit_tests = ['allo', 'bar.py'] | 2077 unit_tests = ['allo', 'bar.py'] |
| 2015 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2078 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
| 2016 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2079 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
| 2017 input_api.subprocess.check_call( | 2080 input_api.subprocess.check_call( |
| 2018 ['allo', '--verbose'], cwd=self.fake_root_dir) | 2081 ['allo', '--verbose'], cwd=self.fake_root_dir) |
| 2019 cmd = ['bar.py', '--verbose'] | 2082 cmd = ['bar.py', '--verbose'] |
| 2020 if input_api.platform == 'win32': | 2083 if input_api.platform == 'win32': |
| 2021 cmd.insert(0, input_api.python_executable) | 2084 cmd.insert(0, input_api.python_executable) |
| 2022 input_api.subprocess.check_call( | 2085 input_api.subprocess.check_call( |
| 2023 cmd, cwd=self.fake_root_dir).AndRaise( | 2086 cmd, cwd=self.fake_root_dir).AndRaise( |
| 2024 input_api.subprocess.CalledProcessError()) | 2087 input_api.subprocess.CalledProcessError()) |
| 2025 | 2088 |
| 2026 self.mox.ReplayAll() | 2089 self.mox.ReplayAll() |
| 2027 results = presubmit_canned_checks.RunUnitTests( | 2090 results = presubmit_canned_checks.RunUnitTests( |
| 2028 input_api, | 2091 input_api, |
| 2029 presubmit.OutputApi, | 2092 presubmit.OutputApi, |
| 2030 unit_tests) | 2093 unit_tests) |
| 2031 self.assertEqual(1, len(results)) | 2094 self.assertEqual(1, len(results)) |
| 2032 self.assertEqual( | 2095 self.assertEqual( |
| 2033 presubmit.OutputApi.PresubmitPromptWarning, results[0].__class__) | 2096 presubmit.OutputApi.PresubmitPromptWarning, results[0].__class__) |
| 2034 self.checkstdout('Running allo\nRunning bar.py\n') | 2097 self.checkstdout('Running allo\nRunning bar.py\n') |
| 2035 | 2098 |
| 2036 def testCannedRunUnitTestsInDirectory(self): | 2099 def testCannedRunUnitTestsInDirectory(self): |
| 2037 change = presubmit.Change( | 2100 change = presubmit.Change( |
| 2038 'foo1', 'description1', self.fake_root_dir, None, 0, 0) | 2101 'foo1', 'description1', self.fake_root_dir, None, 0, 0, None) |
| 2039 input_api = self.MockInputApi(change, False) | 2102 input_api = self.MockInputApi(change, False) |
| 2040 input_api.verbose = True | 2103 input_api.verbose = True |
| 2041 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2104 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
| 2042 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) | 2105 input_api.PresubmitLocalPath().AndReturn(self.fake_root_dir) |
| 2043 path = presubmit.os.path.join(self.fake_root_dir, 'random_directory') | 2106 path = presubmit.os.path.join(self.fake_root_dir, 'random_directory') |
| 2044 input_api.os_listdir(path).AndReturn(['.', '..', 'a', 'b', 'c']) | 2107 input_api.os_listdir(path).AndReturn(['.', '..', 'a', 'b', 'c']) |
| 2045 input_api.os_path.isfile = lambda x: not x.endswith('.') | 2108 input_api.os_path.isfile = lambda x: not x.endswith('.') |
| 2046 input_api.subprocess.check_call( | 2109 input_api.subprocess.check_call( |
| 2047 [presubmit.os.path.join('random_directory', 'b'), '--verbose'], | 2110 [presubmit.os.path.join('random_directory', 'b'), '--verbose'], |
| 2048 cwd=self.fake_root_dir) | 2111 cwd=self.fake_root_dir) |
| 2049 | 2112 |
| 2050 self.mox.ReplayAll() | 2113 self.mox.ReplayAll() |
| 2051 results = presubmit_canned_checks.RunUnitTestsInDirectory( | 2114 results = presubmit_canned_checks.RunUnitTestsInDirectory( |
| 2052 input_api, | 2115 input_api, |
| 2053 presubmit.OutputApi, | 2116 presubmit.OutputApi, |
| 2054 'random_directory', | 2117 'random_directory', |
| 2055 whitelist=['^a$', '^b$'], | 2118 whitelist=['^a$', '^b$'], |
| 2056 blacklist=['a']) | 2119 blacklist=['a']) |
| 2057 self.assertEqual(results, []) | 2120 self.assertEqual(results, []) |
| 2058 self.checkstdout( | 2121 self.checkstdout( |
| 2059 'Running %s\n' % presubmit.os.path.join('random_directory', 'b')) | 2122 'Running %s\n' % presubmit.os.path.join('random_directory', 'b')) |
| 2060 | 2123 |
| 2061 | 2124 |
| 2062 if __name__ == '__main__': | 2125 if __name__ == '__main__': |
| 2063 import unittest | 2126 import unittest |
| 2064 unittest.main() | 2127 unittest.main() |
| OLD | NEW |