OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 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,W0212,W0403 | 9 # pylint: disable=E1101,E1103,W0212,W0403 |
10 | 10 |
11 import StringIO | 11 import StringIO |
12 | 12 |
13 # Fixes include path. | 13 # Fixes include path. |
14 from super_mox import mox, SuperMoxTestBase | 14 from super_mox import mox, SuperMoxTestBase |
15 | 15 |
| 16 import owners |
16 import presubmit_support as presubmit | 17 import presubmit_support as presubmit |
17 # Shortcut. | 18 # Shortcut. |
18 presubmit_canned_checks = presubmit.presubmit_canned_checks | 19 presubmit_canned_checks = presubmit.presubmit_canned_checks |
19 | 20 |
20 | 21 |
21 class PresubmitTestsBase(SuperMoxTestBase): | 22 class PresubmitTestsBase(SuperMoxTestBase): |
22 """Setups and tear downs the mocks but doesn't test anything as-is.""" | 23 """Setups and tear downs the mocks but doesn't test anything as-is.""" |
23 presubmit_text = """ | 24 presubmit_text = """ |
24 def CheckChangeOnUpload(input_api, output_api): | 25 def CheckChangeOnUpload(input_api, output_api): |
25 if not input_api.change.NOSUCHKEY: | 26 if not input_api.change.NOSUCHKEY: |
(...skipping 1002 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1028 input_api = presubmit.InputApi( | 1029 input_api = presubmit.InputApi( |
1029 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False) | 1030 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False) |
1030 input_api.ReadFile(fileobj, 'x') | 1031 input_api.ReadFile(fileobj, 'x') |
1031 | 1032 |
1032 | 1033 |
1033 class OuputApiUnittest(PresubmitTestsBase): | 1034 class OuputApiUnittest(PresubmitTestsBase): |
1034 """Tests presubmit.OutputApi.""" | 1035 """Tests presubmit.OutputApi.""" |
1035 def testMembersChanged(self): | 1036 def testMembersChanged(self): |
1036 self.mox.ReplayAll() | 1037 self.mox.ReplayAll() |
1037 members = [ | 1038 members = [ |
1038 'MailTextResult', 'PresubmitError', 'PresubmitNotifyResult', | 1039 'MailTextResult', 'PresubmitAddText', 'PresubmitError', |
1039 'PresubmitPromptWarning', 'PresubmitResult', | 1040 'PresubmitNotifyResult', 'PresubmitPromptWarning', 'PresubmitResult', |
1040 ] | 1041 ] |
1041 # If this test fails, you should add the relevant test. | 1042 # If this test fails, you should add the relevant test. |
1042 self.compareMembers(presubmit.OutputApi(), members) | 1043 self.compareMembers(presubmit.OutputApi(), members) |
1043 | 1044 |
1044 def testOutputApiBasics(self): | 1045 def testOutputApiBasics(self): |
1045 self.mox.ReplayAll() | 1046 self.mox.ReplayAll() |
1046 self.failUnless(presubmit.OutputApi.PresubmitError('').IsFatal()) | 1047 self.failUnless(presubmit.OutputApi.PresubmitError('').IsFatal()) |
1047 self.failIf(presubmit.OutputApi.PresubmitError('').ShouldPrompt()) | 1048 self.failIf(presubmit.OutputApi.PresubmitError('').ShouldPrompt()) |
1048 | 1049 |
1049 self.failIf(presubmit.OutputApi.PresubmitPromptWarning('').IsFatal()) | 1050 self.failIf(presubmit.OutputApi.PresubmitPromptWarning('').IsFatal()) |
1050 self.failUnless( | 1051 self.failUnless( |
1051 presubmit.OutputApi.PresubmitPromptWarning('').ShouldPrompt()) | 1052 presubmit.OutputApi.PresubmitPromptWarning('').ShouldPrompt()) |
1052 | 1053 |
1053 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').IsFatal()) | 1054 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').IsFatal()) |
1054 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').ShouldPrompt()) | 1055 self.failIf(presubmit.OutputApi.PresubmitNotifyResult('').ShouldPrompt()) |
1055 | 1056 |
| 1057 self.failIf(presubmit.OutputApi.PresubmitAddText('foo').IsFatal()) |
| 1058 self.failIf(presubmit.OutputApi.PresubmitAddText('foo').ShouldPrompt()) |
| 1059 |
1056 # TODO(joi) Test MailTextResult once implemented. | 1060 # TODO(joi) Test MailTextResult once implemented. |
1057 | 1061 |
1058 def testOutputApiHandling(self): | 1062 def testOutputApiHandling(self): |
1059 self.mox.ReplayAll() | 1063 self.mox.ReplayAll() |
| 1064 |
| 1065 output = StringIO.StringIO() |
| 1066 unused_input = StringIO.StringIO() |
| 1067 added_text = presubmit.OutputApi.PresubmitAddText('R=ben@example.com') |
| 1068 self.failUnless(added_text._Handle(output, unused_input)) |
| 1069 self.failUnlessEqual(output.getvalue(), 'ADD: R=ben@example.com\n') |
| 1070 |
1060 output = StringIO.StringIO() | 1071 output = StringIO.StringIO() |
1061 unused_input = StringIO.StringIO() | 1072 unused_input = StringIO.StringIO() |
1062 error = presubmit.OutputApi.PresubmitError('!!!') | 1073 error = presubmit.OutputApi.PresubmitError('!!!') |
1063 self.failIf(error._Handle(output, unused_input)) | 1074 self.failIf(error._Handle(output, unused_input)) |
1064 self.failUnless(output.getvalue().count('!!!')) | 1075 self.failUnless(output.getvalue().count('!!!')) |
1065 | 1076 |
1066 output = StringIO.StringIO() | 1077 output = StringIO.StringIO() |
1067 notify = presubmit.OutputApi.PresubmitNotifyResult('?see?') | 1078 notify = presubmit.OutputApi.PresubmitNotifyResult('?see?') |
1068 self.failUnless(notify._Handle(output, unused_input)) | 1079 self.failUnless(notify._Handle(output, unused_input)) |
1069 self.failUnless(output.getvalue().count('?see?')) | 1080 self.failUnless(output.getvalue().count('?see?')) |
(...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1833 }""") | 1844 }""") |
1834 connection.close() | 1845 connection.close() |
1835 self.mox.ReplayAll() | 1846 self.mox.ReplayAll() |
1836 | 1847 |
1837 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( | 1848 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( |
1838 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) | 1849 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) |
1839 self.assertEquals(len(results), 1) | 1850 self.assertEquals(len(results), 1) |
1840 self.assertEquals(results[0].__class__, | 1851 self.assertEquals(results[0].__class__, |
1841 presubmit.OutputApi.PresubmitNotifyResult) | 1852 presubmit.OutputApi.PresubmitNotifyResult) |
1842 | 1853 |
| 1854 def OwnersTest(self, is_committing, change_tags=None, |
| 1855 suggested_reviewers=None, approvers=None, |
| 1856 uncovered_files=None, expected_results=None): |
| 1857 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 1858 affected_file.LocalPath().AndReturn('foo.cc') |
| 1859 change = self.mox.CreateMock(presubmit.Change) |
| 1860 change.AffectedFiles(None).AndReturn([affected_file]) |
| 1861 |
| 1862 input_api = self.MockInputApi(change, False) |
| 1863 fake_db = self.mox.CreateMock(owners.Database) |
| 1864 input_api.owners_db = fake_db |
| 1865 input_api.is_committing = is_committing |
| 1866 |
| 1867 if is_committing: |
| 1868 change.approvers = approvers |
| 1869 fake_db.files_not_covered_by(set(['foo.cc']), approvers).AndReturn( |
| 1870 uncovered_files) |
| 1871 else: |
| 1872 change.tags = change_tags |
| 1873 if not change_tags.get('R'): |
| 1874 fake_db.reviewers_for(set(['foo.cc'])).AndReturn(suggested_reviewers) |
| 1875 |
| 1876 self.mox.ReplayAll() |
| 1877 results = presubmit_canned_checks.CheckOwners(input_api, |
| 1878 presubmit.OutputApi, None) |
| 1879 self.assertEquals(len(results), len(expected_results)) |
| 1880 if results and expected_results: |
| 1881 output = StringIO.StringIO() |
| 1882 unused_input = StringIO.StringIO() |
| 1883 results[0]._Handle(output, unused_input) |
| 1884 self.assertEquals(output.getvalue(), expected_results[0]) |
| 1885 |
| 1886 def testCannedCheckOwners_WithReviewer(self): |
| 1887 self.OwnersTest(is_committing=False, change_tags={'R': 'ben@example.com'}, |
| 1888 expected_results=[]) |
| 1889 |
| 1890 def testCannedCheckOwners_NoReviewer(self): |
| 1891 self.OwnersTest(is_committing=False, change_tags={}, |
| 1892 suggested_reviewers=['ben@example.com'], |
| 1893 expected_results=['ADD: R=ben@example.com\n']) |
| 1894 |
| 1895 def testCannedCheckOwners_CommittingWithoutOwnerLGTM(self): |
| 1896 self.OwnersTest(is_committing=True, |
| 1897 approvers=set(), |
| 1898 uncovered_files=set(['foo.cc']), |
| 1899 expected_results=['Missing owner LGTM for: foo.cc\n']) |
| 1900 |
| 1901 def testCannedCheckOwners_CommittingWithLGTMs(self): |
| 1902 self.OwnersTest(is_committing=True, |
| 1903 approvers=set('ben@example.com'), |
| 1904 uncovered_files=set(), |
| 1905 expected_results=[]) |
| 1906 |
1843 | 1907 |
1844 if __name__ == '__main__': | 1908 if __name__ == '__main__': |
1845 import unittest | 1909 import unittest |
1846 unittest.main() | 1910 unittest.main() |
OLD | NEW |