| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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: disable=E1101,E1103 | 8 # pylint: disable=E1101,E1103 |
| 9 | 9 |
| 10 import StringIO | 10 import StringIO |
| (...skipping 2545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2556 | 2556 |
| 2557 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( | 2557 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( |
| 2558 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) | 2558 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) |
| 2559 self.assertEquals(len(results), 1) | 2559 self.assertEquals(len(results), 1) |
| 2560 self.assertEquals(results[0].__class__, | 2560 self.assertEquals(results[0].__class__, |
| 2561 presubmit.OutputApi.PresubmitNotifyResult) | 2561 presubmit.OutputApi.PresubmitNotifyResult) |
| 2562 | 2562 |
| 2563 def AssertOwnersWorks(self, tbr=False, issue='1', approvers=None, | 2563 def AssertOwnersWorks(self, tbr=False, issue='1', approvers=None, |
| 2564 reviewers=None, is_committing=True, rietveld_response=None, | 2564 reviewers=None, is_committing=True, rietveld_response=None, |
| 2565 uncovered_files=None, expected_output='', | 2565 uncovered_files=None, expected_output='', |
| 2566 manually_specified_reviewers=None, cq_dry_run=False): | 2566 manually_specified_reviewers=None, dry_run=None): |
| 2567 if approvers is None: | 2567 if approvers is None: |
| 2568 # The set of people who lgtm'ed a change. | 2568 # The set of people who lgtm'ed a change. |
| 2569 approvers = set() | 2569 approvers = set() |
| 2570 if reviewers is None: | 2570 if reviewers is None: |
| 2571 # The set of people needed to lgtm a change. We default to | 2571 # The set of people needed to lgtm a change. We default to |
| 2572 # the same list as the people who approved it. We use 'reviewers' | 2572 # the same list as the people who approved it. We use 'reviewers' |
| 2573 # to avoid a name collision w/ owners.py. | 2573 # to avoid a name collision w/ owners.py. |
| 2574 reviewers = approvers | 2574 reviewers = approvers |
| 2575 if uncovered_files is None: | 2575 if uncovered_files is None: |
| 2576 uncovered_files = set() | 2576 uncovered_files = set() |
| 2577 if manually_specified_reviewers is None: | 2577 if manually_specified_reviewers is None: |
| 2578 manually_specified_reviewers = [] | 2578 manually_specified_reviewers = [] |
| 2579 | 2579 |
| 2580 change = self.mox.CreateMock(presubmit.Change) | 2580 change = self.mox.CreateMock(presubmit.Change) |
| 2581 change.issue = issue | 2581 change.issue = issue |
| 2582 change.author_email = 'john@example.com' | 2582 change.author_email = 'john@example.com' |
| 2583 change.R = ','.join(manually_specified_reviewers) | 2583 change.R = ','.join(manually_specified_reviewers) |
| 2584 change.TBR = '' | 2584 change.TBR = '' |
| 2585 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) | 2585 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 2586 input_api = self.MockInputApi(change, False) | 2586 input_api = self.MockInputApi(change, False) |
| 2587 fake_db = self.mox.CreateMock(owners.Database) | 2587 fake_db = self.mox.CreateMock(owners.Database) |
| 2588 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP) | 2588 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP) |
| 2589 input_api.owners_db = fake_db | 2589 input_api.owners_db = fake_db |
| 2590 input_api.is_committing = is_committing | 2590 input_api.is_committing = is_committing |
| 2591 input_api.tbr = tbr | 2591 input_api.tbr = tbr |
| 2592 input_api.dry_run = dry_run |
| 2592 | 2593 |
| 2593 if not is_committing or (not tbr and issue): | 2594 if not is_committing or (not tbr and issue): |
| 2594 if not cq_dry_run: | 2595 if not dry_run: |
| 2595 affected_file.LocalPath().AndReturn('foo/xyz.cc') | 2596 affected_file.LocalPath().AndReturn('foo/xyz.cc') |
| 2596 change.AffectedFiles(file_filter=None).AndReturn([affected_file]) | 2597 change.AffectedFiles(file_filter=None).AndReturn([affected_file]) |
| 2597 if issue and not rietveld_response: | 2598 if issue and not rietveld_response: |
| 2598 rietveld_response = { | 2599 rietveld_response = { |
| 2599 "owner_email": change.author_email, | 2600 "owner_email": change.author_email, |
| 2600 "messages": [ | 2601 "messages": [ |
| 2601 {"sender": a, "text": "I approve", "approval": True} | 2602 {"sender": a, "text": "I approve", "approval": True} |
| 2602 for a in approvers | 2603 for a in approvers |
| 2603 ], | 2604 ], |
| 2604 "reviewers": reviewers | 2605 "reviewers": reviewers |
| 2605 } | 2606 } |
| 2606 | 2607 |
| 2607 if is_committing: | 2608 if is_committing: |
| 2608 people = approvers | 2609 people = approvers |
| 2609 if issue: | |
| 2610 input_api.rietveld.get_issue_properties( | |
| 2611 issue=int(input_api.change.issue), messages=None).AndReturn( | |
| 2612 rietveld_response) | |
| 2613 else: | 2610 else: |
| 2614 people = reviewers | 2611 people = reviewers |
| 2615 | 2612 |
| 2616 if not cq_dry_run: | 2613 if not dry_run: |
| 2617 if issue: | 2614 if issue: |
| 2618 input_api.rietveld.get_issue_properties( | 2615 input_api.rietveld.get_issue_properties( |
| 2619 issue=int(input_api.change.issue), messages=True).AndReturn( | 2616 issue=int(input_api.change.issue), messages=True).AndReturn( |
| 2620 rietveld_response) | 2617 rietveld_response) |
| 2621 | 2618 |
| 2622 people.add(change.author_email) | 2619 people.add(change.author_email) |
| 2623 fake_db.files_not_covered_by(set(['foo/xyz.cc']), | 2620 fake_db.files_not_covered_by(set(['foo/xyz.cc']), |
| 2624 people).AndReturn(uncovered_files) | 2621 people).AndReturn(uncovered_files) |
| 2625 if not is_committing and uncovered_files: | 2622 if not is_committing and uncovered_files: |
| 2626 fake_db.reviewers_for(set(['foo']), | 2623 fake_db.reviewers_for(set(['foo']), |
| 2627 change.author_email).AndReturn(change.author_email) | 2624 change.author_email).AndReturn(change.author_email) |
| 2628 | 2625 |
| 2629 self.mox.ReplayAll() | 2626 self.mox.ReplayAll() |
| 2630 output = presubmit.PresubmitOutput() | 2627 output = presubmit.PresubmitOutput() |
| 2631 results = presubmit_canned_checks.CheckOwners(input_api, | 2628 results = presubmit_canned_checks.CheckOwners(input_api, |
| 2632 presubmit.OutputApi) | 2629 presubmit.OutputApi) |
| 2633 if results: | 2630 if results: |
| 2634 results[0].handle(output) | 2631 results[0].handle(output) |
| 2635 self.assertEquals(output.getvalue(), expected_output) | 2632 self.assertEquals(output.getvalue(), expected_output) |
| 2636 | 2633 |
| 2637 def testCannedCheckOwners_DryRun(self): | 2634 def testCannedCheckOwners_DryRun(self): |
| 2638 response = { | 2635 response = { |
| 2639 "owner_email": "john@example.com", | 2636 "owner_email": "john@example.com", |
| 2640 "cq_dry_run": True, | |
| 2641 "reviewers": ["ben@example.com"], | 2637 "reviewers": ["ben@example.com"], |
| 2642 } | 2638 } |
| 2643 self.AssertOwnersWorks(approvers=set(), | 2639 self.AssertOwnersWorks(approvers=set(), |
| 2644 cq_dry_run=True, | 2640 dry_run=True, |
| 2645 rietveld_response=response, | 2641 rietveld_response=response, |
| 2646 reviewers=set(["ben@example.com"]), | 2642 reviewers=set(["ben@example.com"]), |
| 2647 expected_output='This is a dry run, skipping OWNERS check\n') | 2643 expected_output='This is a dry run, skipping OWNERS check\n') |
| 2648 | 2644 |
| 2649 self.AssertOwnersWorks(approvers=set(['ben@example.com']), | 2645 self.AssertOwnersWorks(approvers=set(['ben@example.com']), |
| 2650 is_committing=False, | 2646 is_committing=False, |
| 2651 rietveld_response=response, | 2647 rietveld_response=response, |
| 2652 expected_output='') | 2648 expected_output='') |
| 2653 | 2649 |
| 2654 def testCannedCheckOwners_Approved(self): | 2650 def testCannedCheckOwners_Approved(self): |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2890 owners_check=False) | 2886 owners_check=False) |
| 2891 self.assertEqual(2, len(results)) | 2887 self.assertEqual(2, len(results)) |
| 2892 self.assertEqual( | 2888 self.assertEqual( |
| 2893 'Found line ending with white spaces in:', results[0]._message) | 2889 'Found line ending with white spaces in:', results[0]._message) |
| 2894 self.checkstdout('') | 2890 self.checkstdout('') |
| 2895 | 2891 |
| 2896 | 2892 |
| 2897 if __name__ == '__main__': | 2893 if __name__ == '__main__': |
| 2898 import unittest | 2894 import unittest |
| 2899 unittest.main() | 2895 unittest.main() |
| OLD | NEW |