Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(432)

Side by Side Diff: tests/presubmit_unittest.py

Issue 6730020: Clean up the parsing of approvals for OWNERS checks. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: rebase to head Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « presubmit_canned_checks.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 1243 matching lines...) Expand 10 before | Expand all | Expand 10 after
1254 'CheckChangeTodoHasOwner', 1254 'CheckChangeTodoHasOwner',
1255 'CheckChangeHasQaField', 'CheckChangeHasTestedField', 1255 'CheckChangeHasQaField', 'CheckChangeHasTestedField',
1256 'CheckChangeHasTestField', 1256 'CheckChangeHasTestField',
1257 'CheckChangeLintsClean', 1257 'CheckChangeLintsClean',
1258 'CheckChangeSvnEolStyle', 1258 'CheckChangeSvnEolStyle',
1259 'CheckLicense', 1259 'CheckLicense',
1260 'CheckSvnModifiedDirectories', 1260 'CheckSvnModifiedDirectories',
1261 'CheckSvnForCommonMimeTypes', 'CheckSvnProperty', 1261 'CheckSvnForCommonMimeTypes', 'CheckSvnProperty',
1262 'CheckDoNotSubmit', 1262 'CheckDoNotSubmit',
1263 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', 1263 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles',
1264 'CheckLongLines', 'CheckTreeIsOpen', 'RunPythonUnitTests', 1264 'CheckLongLines', 'CheckTreeIsOpen', 'PanProjectChecks',
1265 'RunPythonUnitTests',
1265 'RunPylint', 1266 'RunPylint',
1266 'CheckBuildbotPendingBuilds', 'CheckRietveldTryJobExecution', 1267 'CheckBuildbotPendingBuilds', 'CheckRietveldTryJobExecution',
1267 'CheckOwners', 1268 'CheckOwners',
1269 'time',
1268 ] 1270 ]
1269 # If this test fails, you should add the relevant test. 1271 # If this test fails, you should add the relevant test.
1270 self.compareMembers(presubmit_canned_checks, members) 1272 self.compareMembers(presubmit_canned_checks, members)
1271 1273
1272 def DescriptionTest(self, check, description1, description2, error_type, 1274 def DescriptionTest(self, check, description1, description2, error_type,
1273 committing): 1275 committing):
1274 change1 = presubmit.Change('foo1', description1, self.fake_root_dir, None, 1276 change1 = presubmit.Change('foo1', description1, self.fake_root_dir, None,
1275 0, 0) 1277 0, 0)
1276 input_api1 = self.MockInputApi(change1, committing) 1278 input_api1 = self.MockInputApi(change1, committing)
1277 change2 = presubmit.Change('foo2', description2, self.fake_root_dir, None, 1279 change2 = presubmit.Change('foo2', description2, self.fake_root_dir, None,
(...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after
1876 }""") 1878 }""")
1877 connection.close() 1879 connection.close()
1878 self.mox.ReplayAll() 1880 self.mox.ReplayAll()
1879 1881
1880 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( 1882 results = presubmit_canned_checks.CheckBuildbotPendingBuilds(
1881 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) 1883 input_api, presubmit.OutputApi, 'uurl', 2, ('foo'))
1882 self.assertEquals(len(results), 1) 1884 self.assertEquals(len(results), 1)
1883 self.assertEquals(results[0].__class__, 1885 self.assertEquals(results[0].__class__,
1884 presubmit.OutputApi.PresubmitNotifyResult) 1886 presubmit.OutputApi.PresubmitNotifyResult)
1885 1887
1886 def OwnersTest(self, is_committing, tbr=False, change_tags=None, 1888 def AssertOwnersWorks(self, tbr=False, issue='1', approvers=None,
1887 suggested_reviewers=None, approvers=None, 1889 rietveld_response=None, host_url=None,
1888 uncovered_files=None, expected_reviewers=None, expected_output='', 1890 uncovered_files=None, expected_output=''):
1889 host_url=None): 1891 if approvers is None:
1892 approvers = set()
1893 if uncovered_files is None:
1894 uncovered_files = set()
1895
1896 change = self.mox.CreateMock(presubmit.Change)
1897 change.issue = issue
1890 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) 1898 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile)
1891 affected_file.LocalPath().AndReturn('foo.cc')
1892 change = self.mox.CreateMock(presubmit.Change)
1893 change.AffectedFiles(None).AndReturn([affected_file])
1894
1895 input_api = self.MockInputApi(change, False) 1899 input_api = self.MockInputApi(change, False)
1896 expected_host = 'http://localhost'
1897 if host_url:
1898 input_api.host_url = host_url
1899 if host_url.startswith('https'):
1900 expected_host = host_url
1901 fake_db = self.mox.CreateMock(owners.Database) 1900 fake_db = self.mox.CreateMock(owners.Database)
1902 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP) 1901 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP)
1903 input_api.owners_db = fake_db 1902 input_api.owners_db = fake_db
1904 input_api.is_committing = is_committing 1903 input_api.is_committing = True
1905 input_api.tbr = tbr 1904 input_api.tbr = tbr
1906 1905
1907 if is_committing and not tbr: 1906 if not tbr and issue:
1908 change.issue = '1' 1907 affected_file.LocalPath().AndReturn('foo.cc')
1908 change.AffectedFiles(None).AndReturn([affected_file])
1909
1910 expected_host = 'http://localhost'
1911 if host_url:
1912 input_api.host_url = host_url
1913 if host_url.startswith('https'):
1914 expected_host = host_url
1915
1916 owner_email = 'john@example.com'
1909 messages = list('{"sender": "' + a + '","text": "lgtm"}' for 1917 messages = list('{"sender": "' + a + '","text": "lgtm"}' for
1910 a in approvers) 1918 a in approvers)
1911 rietveld_response = ('{"owner": "john@example.com",' 1919 if not rietveld_response:
1912 '"messages": [' + ','.join(messages) + ']}') 1920 rietveld_response = ('{"owner_email": "' + owner_email + '",'
1921 '"messages": [' + ','.join(messages) + ']}')
1913 input_api.urllib2.urlopen( 1922 input_api.urllib2.urlopen(
1914 expected_host + '/api/1?messages=true').AndReturn( 1923 expected_host + '/api/1?messages=true').AndReturn(
1915 StringIO.StringIO(rietveld_response)) 1924 StringIO.StringIO(rietveld_response))
1916 input_api.json = presubmit.json 1925 input_api.json = presubmit.json
1917 fake_db.files_not_covered_by(set(['foo.cc']), approvers).AndReturn( 1926 fake_db.files_not_covered_by(set(['foo.cc']),
1918 uncovered_files) 1927 approvers.union(set([owner_email]))).AndReturn(uncovered_files)
1919 elif not is_committing:
1920 change.tags = change_tags
1921 if not change_tags.get('R'):
1922 fake_db.reviewers_for(set(['foo.cc'])).AndReturn(suggested_reviewers)
1923 1928
1924 self.mox.ReplayAll() 1929 self.mox.ReplayAll()
1925 output = presubmit.PresubmitOutput() 1930 output = presubmit.PresubmitOutput()
1926 results = presubmit_canned_checks.CheckOwners(input_api, 1931 results = presubmit_canned_checks.CheckOwners(input_api,
1927 presubmit.OutputApi) 1932 presubmit.OutputApi)
1928 if results: 1933 if results:
1929 results[0].handle(output) 1934 results[0].handle(output)
1930 if expected_reviewers is not None:
1931 self.assertEquals(output.reviewers, expected_reviewers)
1932 self.assertEquals(output.getvalue(), expected_output) 1935 self.assertEquals(output.getvalue(), expected_output)
1933 1936
1934 def testCannedCheckOwners_WithReviewer(self): 1937 def testCannedCheckOwners_LGTMPhrases(self):
1935 self.OwnersTest(is_committing=False, change_tags={'R': 'ben@example.com'}) 1938 def phrase_test(phrase, approvers=None, expected_output=''):
1936 self.OwnersTest(is_committing=False, tbr=True, 1939 if approvers is None:
1937 change_tags={'R': 'ben@example.com'}) 1940 approvers = set(['ben@example.com'])
1941 self.AssertOwnersWorks(approvers=approvers,
1942 rietveld_response='{"owner_email": "john@example.com",' +
1943 '"messages": [{"sender": "ben@example.com",' +
1944 '"text": "' + phrase + '"}]}',
1945 expected_output=expected_output)
1938 1946
1939 def testCannedCheckOwners_NoReviewer(self): 1947 phrase_test('LGTM')
1940 self.OwnersTest(is_committing=False, change_tags={}, 1948 phrase_test('\\nlgtm')
1941 suggested_reviewers=['ben@example.com'], 1949 phrase_test('> foo\\n> bar\\nlgtm\\n')
1942 expected_reviewers=['ben@example.com']) 1950 phrase_test('> LGTM', approvers=set(),
1943 self.OwnersTest(is_committing=False, tbr=True, change_tags={}, 1951 expected_output='Missing LGTM from someone other than '
1944 suggested_reviewers=['ben@example.com'], 1952 'john@example.com\n')
1945 expected_reviewers=['ben@example.com'])
1946 1953
1947 def testCannedCheckOwners_CommittingWithoutOwnerLGTM(self): 1954 # TODO(dpranke): these probably should pass.
1948 self.OwnersTest(is_committing=True, 1955 phrase_test('Looks Good To Me', approvers=set(),
1949 approvers=set(), 1956 expected_output='Missing LGTM from someone other than '
1950 uncovered_files=set(['foo.cc']), 1957 'john@example.com\n')
1958 phrase_test('looks good to me', approvers=set(),
1959 expected_output='Missing LGTM from someone other than '
1960 'john@example.com\n')
1961
1962 # TODO(dpranke): this probably shouldn't pass.
1963 phrase_test('no lgtm for you')
1964
1965 def testCannedCheckOwners_HTTPS_HostURL(self):
1966 self.AssertOwnersWorks(approvers=set(['ben@example.com']),
1967 host_url='https://localhost')
1968
1969 def testCannedCheckOwners_MissingSchemeInHostURL(self):
1970 self.AssertOwnersWorks(approvers=set(['ben@example.com']),
1971 host_url='localhost')
1972
1973 def testCannedCheckOwners_NoIssue(self):
1974 self.AssertOwnersWorks(issue=None,
1975 expected_output='Change not uploaded for review\n')
1976
1977 def testCannedCheckOwners_NoLGTM(self):
1978 self.AssertOwnersWorks(expected_output='Missing LGTM from someone '
1979 'other than john@example.com\n')
1980
1981 def testCannedCheckOwners_OnlyOwnerLGTM(self):
1982 self.AssertOwnersWorks(approvers=set(['john@example.com']),
1983 expected_output='Missing LGTM from someone '
1984 'other than john@example.com\n')
1985
1986 def testCannedCheckOwners_TBR(self):
1987 self.AssertOwnersWorks(tbr=True,
1988 expected_output='--tbr was specified, skipping OWNERS check\n')
1989
1990 def testCannedCheckOwners_Upload(self):
1991 class FakeInputAPI(object):
1992 is_committing = False
1993
1994 results = presubmit_canned_checks.CheckOwners(FakeInputAPI(),
1995 presubmit.OutputApi)
1996 self.assertEqual(results, [])
1997
1998 def testCannedCheckOwners_WithoutOwnerLGTM(self):
1999 self.AssertOwnersWorks(uncovered_files=set(['foo.cc']),
1951 expected_output='Missing LGTM from an OWNER for: foo.cc\n') 2000 expected_output='Missing LGTM from an OWNER for: foo.cc\n')
1952 2001
1953 def testCannedCheckOwners_CommittingWithLGTMs(self): 2002 def testCannedCheckOwners_WithLGTMs(self):
1954 self.OwnersTest(is_committing=True, 2003 self.AssertOwnersWorks(approvers=set(['ben@example.com']),
1955 approvers=set(['ben@example.com']), 2004 uncovered_files=set())
1956 uncovered_files=set())
1957 2005
1958 def testCannedCheckOwners_TBR(self):
1959 self.OwnersTest(is_committing=True, tbr=True,
1960 approvers=set(),
1961 uncovered_files=set(),
1962 expected_output='--tbr was specified, skipping OWNERS check\n')
1963
1964 def testCannedCheckOwners_MissingSchemeInHostURL(self):
1965 self.OwnersTest(is_committing=True,
1966 approvers=set(['ben@example.com']),
1967 uncovered_files=set(), host_url='localhost')
1968
1969 def testCannedCheckOwners_HTTPS_HostURL(self):
1970 self.OwnersTest(is_committing=True,
1971 approvers=set(['ben@example.com']),
1972 uncovered_files=set(), host_url='https://localhost')
1973 2006
1974 2007
1975 if __name__ == '__main__': 2008 if __name__ == '__main__':
1976 import unittest 2009 import unittest
1977 unittest.main() 2010 unittest.main()
OLDNEW
« no previous file with comments | « presubmit_canned_checks.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698