Chromium Code Reviews| 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 |
| (...skipping 682 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 693 def testMembersChanged(self): | 693 def testMembersChanged(self): |
| 694 self.mox.ReplayAll() | 694 self.mox.ReplayAll() |
| 695 members = [ | 695 members = [ |
| 696 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', | 696 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', |
| 697 'AffectedTextFiles', | 697 'AffectedTextFiles', |
| 698 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', | 698 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', |
| 699 'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', | 699 'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', |
| 700 'LocalToDepotPath', | 700 'LocalToDepotPath', |
| 701 'PresubmitLocalPath', 'ReadFile', 'RightHandSideLines', 'ServerPaths', | 701 'PresubmitLocalPath', 'ReadFile', 'RightHandSideLines', 'ServerPaths', |
| 702 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', 'environ', | 702 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', 'environ', |
| 703 'is_committing', 'json', 'marshal', 'os_path', 'owners_db', 'pickle', | 703 'host_url', 'is_committing', 'is_tbr', 'json', 'marshal', 'os_path', |
| 704 'platform', 'python_executable', 're', 'subprocess', 'tempfile', | 704 'owners_db', 'pickle', 'platform', 'python_executable', 're', |
| 705 'traceback', 'unittest', 'urllib2', 'version', | 705 'subprocess', 'tempfile', 'traceback', 'unittest', 'urllib2', 'version', |
| 706 ] | 706 ] |
| 707 # If this test fails, you should add the relevant test. | 707 # If this test fails, you should add the relevant test. |
| 708 self.compareMembers(presubmit.InputApi(self.fake_change, './.', False), | 708 self.compareMembers(presubmit.InputApi(self.fake_change, './.', False), |
| 709 members) | 709 members) |
| 710 | 710 |
| 711 def testDepotToLocalPath(self): | 711 def testDepotToLocalPath(self): |
| 712 presubmit.scm.SVN.CaptureInfo('svn://foo/smurf').AndReturn( | 712 presubmit.scm.SVN.CaptureInfo('svn://foo/smurf').AndReturn( |
| 713 {'Path': 'prout'}) | 713 {'Path': 'prout'}) |
| 714 presubmit.scm.SVN.CaptureInfo('svn:/foo/notfound/burp').AndReturn({}) | 714 presubmit.scm.SVN.CaptureInfo('svn:/foo/notfound/burp').AndReturn({}) |
| 715 self.mox.ReplayAll() | 715 self.mox.ReplayAll() |
| (...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1180 self.failUnless(len(output) == 1) | 1180 self.failUnless(len(output) == 1) |
| 1181 self.failUnless(files[0] == output[0]) | 1181 self.failUnless(files[0] == output[0]) |
| 1182 | 1182 |
| 1183 | 1183 |
| 1184 class GclChangeUnittest(PresubmitTestsBase): | 1184 class GclChangeUnittest(PresubmitTestsBase): |
| 1185 def testMembersChanged(self): | 1185 def testMembersChanged(self): |
| 1186 members = [ | 1186 members = [ |
| 1187 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', | 1187 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', |
| 1188 'DescriptionText', 'FullDescriptionText', 'LocalPaths', 'Name', | 1188 'DescriptionText', 'FullDescriptionText', 'LocalPaths', 'Name', |
| 1189 'RepositoryRoot', 'RightHandSideLines', 'ServerPaths', | 1189 'RepositoryRoot', 'RightHandSideLines', 'ServerPaths', |
| 1190 'approvers', 'issue', 'patchset', 'scm', 'tags', | 1190 'issue', 'patchset', 'scm', 'tags', |
| 1191 ] | 1191 ] |
| 1192 # If this test fails, you should add the relevant test. | 1192 # If this test fails, you should add the relevant test. |
| 1193 self.mox.ReplayAll() | 1193 self.mox.ReplayAll() |
| 1194 | 1194 |
| 1195 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 1195 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], |
| 1196 0, 0) | 1196 0, 0) |
| 1197 self.compareMembers(change, members) | 1197 self.compareMembers(change, members) |
| 1198 | 1198 |
| 1199 | 1199 |
| 1200 class CannedChecksUnittest(PresubmitTestsBase): | 1200 class CannedChecksUnittest(PresubmitTestsBase): |
| 1201 """Tests presubmit_canned_checks.py.""" | 1201 """Tests presubmit_canned_checks.py.""" |
| 1202 | 1202 |
| 1203 def setUp(self): | 1203 def setUp(self): |
| 1204 PresubmitTestsBase.setUp(self) | 1204 PresubmitTestsBase.setUp(self) |
| 1205 | 1205 |
| 1206 def MockInputApi(self, change, committing): | 1206 def MockInputApi(self, change, committing): |
| 1207 input_api = self.mox.CreateMock(presubmit.InputApi) | 1207 input_api = self.mox.CreateMock(presubmit.InputApi) |
| 1208 input_api.cStringIO = presubmit.cStringIO | 1208 input_api.cStringIO = presubmit.cStringIO |
| 1209 input_api.os_path = presubmit.os.path | 1209 input_api.os_path = presubmit.os.path |
| 1210 input_api.re = presubmit.re | 1210 input_api.re = presubmit.re |
| 1211 input_api.traceback = presubmit.traceback | 1211 input_api.traceback = presubmit.traceback |
| 1212 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) | 1212 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) |
| 1213 input_api.unittest = unittest | 1213 input_api.unittest = unittest |
| 1214 input_api.subprocess = self.mox.CreateMock(presubmit.subprocess) | 1214 input_api.subprocess = self.mox.CreateMock(presubmit.subprocess) |
| 1215 | 1215 |
| 1216 input_api.change = change | 1216 input_api.change = change |
| 1217 input_api.host_url = 'http://localhost' | |
| 1217 input_api.is_committing = committing | 1218 input_api.is_committing = committing |
| 1219 input_api.ts_tbr = False | |
|
M-A Ruel
2011/03/11 21:19:09
is_tbr?
This line is probably unneeded since it o
| |
| 1218 input_api.python_executable = 'pyyyyython' | 1220 input_api.python_executable = 'pyyyyython' |
| 1219 return input_api | 1221 return input_api |
| 1220 | 1222 |
| 1221 def testMembersChanged(self): | 1223 def testMembersChanged(self): |
| 1222 self.mox.ReplayAll() | 1224 self.mox.ReplayAll() |
| 1223 members = [ | 1225 members = [ |
| 1224 'CheckChangeHasBugField', 'CheckChangeHasDescription', | 1226 'CheckChangeHasBugField', 'CheckChangeHasDescription', |
| 1225 'CheckChangeHasNoStrayWhitespace', | 1227 'CheckChangeHasNoStrayWhitespace', |
| 1226 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', | 1228 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', |
| 1227 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', | 1229 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', |
| (...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1850 }""") | 1852 }""") |
| 1851 connection.close() | 1853 connection.close() |
| 1852 self.mox.ReplayAll() | 1854 self.mox.ReplayAll() |
| 1853 | 1855 |
| 1854 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( | 1856 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( |
| 1855 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) | 1857 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) |
| 1856 self.assertEquals(len(results), 1) | 1858 self.assertEquals(len(results), 1) |
| 1857 self.assertEquals(results[0].__class__, | 1859 self.assertEquals(results[0].__class__, |
| 1858 presubmit.OutputApi.PresubmitNotifyResult) | 1860 presubmit.OutputApi.PresubmitNotifyResult) |
| 1859 | 1861 |
| 1860 def OwnersTest(self, is_committing, change_tags=None, | 1862 def OwnersTest(self, is_committing, is_tbr=False, change_tags=None, |
| 1861 suggested_reviewers=None, approvers=None, | 1863 suggested_reviewers=None, approvers=None, |
| 1862 uncovered_files=None, expected_results=None): | 1864 uncovered_files=None, expected_results=None): |
| 1863 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1865 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 1864 affected_file.LocalPath().AndReturn('foo.cc') | 1866 affected_file.LocalPath().AndReturn('foo.cc') |
| 1865 change = self.mox.CreateMock(presubmit.Change) | 1867 change = self.mox.CreateMock(presubmit.Change) |
| 1866 change.AffectedFiles(None).AndReturn([affected_file]) | 1868 change.AffectedFiles(None).AndReturn([affected_file]) |
| 1867 | 1869 |
| 1868 input_api = self.MockInputApi(change, False) | 1870 input_api = self.MockInputApi(change, False) |
| 1869 fake_db = self.mox.CreateMock(owners.Database) | 1871 fake_db = self.mox.CreateMock(owners.Database) |
| 1872 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP) | |
| 1870 input_api.owners_db = fake_db | 1873 input_api.owners_db = fake_db |
| 1871 input_api.is_committing = is_committing | 1874 input_api.is_committing = is_committing |
| 1875 input_api.is_tbr = is_tbr | |
| 1872 | 1876 |
| 1873 if is_committing: | 1877 if is_committing and not is_tbr: |
| 1874 change.approvers = approvers | 1878 change.issue = '1' |
| 1879 messages = list('{"sender": "' + a + '","text": "lgtm"}' for | |
| 1880 a in approvers) | |
| 1881 rietveld_response = ('{"owner": "john@example.com",' | |
| 1882 '"messages": [' + ','.join(messages) + ']}') | |
| 1883 input_api.urllib2.urlopen( | |
| 1884 input_api.host_url + '/api/1?messages=true').AndReturn( | |
| 1885 StringIO.StringIO(rietveld_response)) | |
| 1886 input_api.json = presubmit.json | |
| 1875 fake_db.files_not_covered_by(set(['foo.cc']), approvers).AndReturn( | 1887 fake_db.files_not_covered_by(set(['foo.cc']), approvers).AndReturn( |
| 1876 uncovered_files) | 1888 uncovered_files) |
| 1877 else: | 1889 elif not is_committing: |
| 1878 change.tags = change_tags | 1890 change.tags = change_tags |
| 1879 if not change_tags.get('R'): | 1891 if not change_tags.get('R'): |
| 1880 fake_db.reviewers_for(set(['foo.cc'])).AndReturn(suggested_reviewers) | 1892 fake_db.reviewers_for(set(['foo.cc'])).AndReturn(suggested_reviewers) |
| 1881 | 1893 |
| 1882 self.mox.ReplayAll() | 1894 self.mox.ReplayAll() |
| 1883 results = presubmit_canned_checks.CheckOwners(input_api, | 1895 results = presubmit_canned_checks.CheckOwners(input_api, |
| 1884 presubmit.OutputApi, None) | 1896 presubmit.OutputApi) |
| 1885 self.assertEquals(len(results), len(expected_results)) | 1897 self.assertEquals(len(results), len(expected_results)) |
| 1886 if results and expected_results: | 1898 if results and expected_results: |
| 1887 output = StringIO.StringIO() | 1899 output = StringIO.StringIO() |
| 1888 unused_input = StringIO.StringIO() | 1900 unused_input = StringIO.StringIO() |
| 1889 results[0]._Handle(output, unused_input) | 1901 results[0]._Handle(output, unused_input) |
| 1890 self.assertEquals(output.getvalue(), expected_results[0]) | 1902 self.assertEquals(output.getvalue(), expected_results[0]) |
| 1891 | 1903 |
| 1892 def testCannedCheckOwners_WithReviewer(self): | 1904 def testCannedCheckOwners_WithReviewer(self): |
| 1893 self.OwnersTest(is_committing=False, change_tags={'R': 'ben@example.com'}, | 1905 self.OwnersTest(is_committing=False, change_tags={'R': 'ben@example.com'}, |
| 1894 expected_results=[]) | 1906 expected_results=[]) |
| 1907 self.OwnersTest(is_committing=False, is_tbr=True, | |
| 1908 change_tags={'R': 'ben@example.com'}, expected_results=[]) | |
| 1895 | 1909 |
| 1896 def testCannedCheckOwners_NoReviewer(self): | 1910 def testCannedCheckOwners_NoReviewer(self): |
| 1897 self.OwnersTest(is_committing=False, change_tags={}, | 1911 self.OwnersTest(is_committing=False, change_tags={}, |
| 1898 suggested_reviewers=['ben@example.com'], | 1912 suggested_reviewers=['ben@example.com'], |
| 1899 expected_results=['ADD: R=ben@example.com\n']) | 1913 expected_results=['ADD: R=ben@example.com\n']) |
| 1914 self.OwnersTest(is_committing=False, is_tbr=True, change_tags={}, | |
| 1915 suggested_reviewers=['ben@example.com'], | |
| 1916 expected_results=['ADD: R=ben@example.com\n']) | |
| 1900 | 1917 |
| 1901 def testCannedCheckOwners_CommittingWithoutOwnerLGTM(self): | 1918 def testCannedCheckOwners_CommittingWithoutOwnerLGTM(self): |
| 1902 self.OwnersTest(is_committing=True, | 1919 self.OwnersTest(is_committing=True, |
| 1903 approvers=set(), | 1920 approvers=set(), |
| 1904 uncovered_files=set(['foo.cc']), | 1921 uncovered_files=set(['foo.cc']), |
| 1905 expected_results=['Missing owner LGTM for: foo.cc\n']) | 1922 expected_results=['Missing LGTM from an OWNER for: foo.cc\n']) |
| 1906 | 1923 |
| 1907 def testCannedCheckOwners_CommittingWithLGTMs(self): | 1924 def testCannedCheckOwners_CommittingWithLGTMs(self): |
| 1908 self.OwnersTest(is_committing=True, | 1925 self.OwnersTest(is_committing=True, |
| 1909 approvers=set('ben@example.com'), | 1926 approvers=set(['ben@example.com']), |
| 1910 uncovered_files=set(), | 1927 uncovered_files=set(), |
| 1911 expected_results=[]) | 1928 expected_results=[]) |
| 1912 | 1929 |
| 1930 def testCannedCheckOwners_TBR(self): | |
| 1931 self.OwnersTest(is_committing=True, is_tbr=True, | |
| 1932 approvers=set(), | |
| 1933 uncovered_files=set(), | |
| 1934 expected_results=['--tbr was specified, skipping OWNERS check\n']) | |
| 1913 | 1935 |
| 1914 if __name__ == '__main__': | 1936 if __name__ == '__main__': |
| 1915 import unittest | 1937 import unittest |
| 1916 unittest.main() | 1938 unittest.main() |
| OLD | NEW |