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

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: 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
« presubmit_canned_checks.py ('K') | « 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 1865 matching lines...) Expand 10 before | Expand all | Expand 10 after
1876 }""") 1876 }""")
1877 connection.close() 1877 connection.close()
1878 self.mox.ReplayAll() 1878 self.mox.ReplayAll()
1879 1879
1880 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( 1880 results = presubmit_canned_checks.CheckBuildbotPendingBuilds(
1881 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) 1881 input_api, presubmit.OutputApi, 'uurl', 2, ('foo'))
1882 self.assertEquals(len(results), 1) 1882 self.assertEquals(len(results), 1)
1883 self.assertEquals(results[0].__class__, 1883 self.assertEquals(results[0].__class__,
1884 presubmit.OutputApi.PresubmitNotifyResult) 1884 presubmit.OutputApi.PresubmitNotifyResult)
1885 1885
1886 def OwnersTest(self, is_committing, tbr=False, change_tags=None, 1886 def AssertOwnersWorks(self, tbr=False, issue='1', approvers=set(),
M-A Ruel 2011/03/24 15:09:23 (even if it's just a unit test, I don't want to en
1887 suggested_reviewers=None, approvers=None, 1887 rietveld_response=None, host_url=None,
1888 uncovered_files=None, expected_reviewers=None, expected_output='', 1888 uncovered_files=set(), expected_output=''):
1889 host_url=None): 1889 change = self.mox.CreateMock(presubmit.Change)
1890 change.issue = issue
1890 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) 1891 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) 1892 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) 1893 fake_db = self.mox.CreateMock(owners.Database)
1902 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP) 1894 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP)
1903 input_api.owners_db = fake_db 1895 input_api.owners_db = fake_db
1904 input_api.is_committing = is_committing 1896 input_api.is_committing = True
1905 input_api.tbr = tbr 1897 input_api.tbr = tbr
1906 1898
1907 if is_committing and not tbr: 1899 if not tbr and issue:
1908 change.issue = '1' 1900 affected_file.LocalPath().AndReturn('foo.cc')
1901 change.AffectedFiles(None).AndReturn([affected_file])
1902
1903 expected_host = 'http://localhost'
1904 if host_url:
1905 input_api.host_url = host_url
1906 if host_url.startswith('https'):
1907 expected_host = host_url
1908
1909 owner = 'john@example.com'
1909 messages = list('{"sender": "' + a + '","text": "lgtm"}' for 1910 messages = list('{"sender": "' + a + '","text": "lgtm"}' for
1910 a in approvers) 1911 a in approvers)
1911 rietveld_response = ('{"owner": "john@example.com",' 1912 if not rietveld_response:
1912 '"messages": [' + ','.join(messages) + ']}') 1913 rietveld_response = ('{"owner": "' + owner + '",'
1914 '"messages": [' + ','.join(messages) + ']}')
1913 input_api.urllib2.urlopen( 1915 input_api.urllib2.urlopen(
1914 expected_host + '/api/1?messages=true').AndReturn( 1916 expected_host + '/api/1?messages=true').AndReturn(
1915 StringIO.StringIO(rietveld_response)) 1917 StringIO.StringIO(rietveld_response))
1916 input_api.json = presubmit.json 1918 input_api.json = presubmit.json
1917 fake_db.files_not_covered_by(set(['foo.cc']), approvers).AndReturn( 1919 fake_db.files_not_covered_by(set(['foo.cc']),
1918 uncovered_files) 1920 approvers.union(set([owner]))).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 1921
1924 self.mox.ReplayAll() 1922 self.mox.ReplayAll()
1925 output = presubmit.PresubmitOutput() 1923 output = presubmit.PresubmitOutput()
1926 results = presubmit_canned_checks.CheckOwners(input_api, 1924 results = presubmit_canned_checks.CheckOwners(input_api,
1927 presubmit.OutputApi) 1925 presubmit.OutputApi)
1928 if results: 1926 if results:
1929 results[0].handle(output) 1927 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) 1928 self.assertEquals(output.getvalue(), expected_output)
1933 1929
1934 def testCannedCheckOwners_WithReviewer(self): 1930 def testCannedCheckOwners_LGTMPhrases(self):
1935 self.OwnersTest(is_committing=False, change_tags={'R': 'ben@example.com'}) 1931 def phrase_test(phrase, approvers=None, expected_output=''):
1936 self.OwnersTest(is_committing=False, tbr=True, 1932 if approvers is None:
M-A Ruel 2011/03/24 15:09:23 approvers = approvers or set(['ben@example.com'])
1937 change_tags={'R': 'ben@example.com'}) 1933 approvers = set(['ben@example.com'])
1934 self.AssertOwnersWorks(approvers=approvers,
1935 rietveld_response='{"owner": "john@example.com",' +
1936 '"messages": [{"sender": "ben@example.com",' +
1937 '"text": "' + phrase + '"}]}',
1938 expected_output=expected_output)
1938 1939
1939 def testCannedCheckOwners_NoReviewer(self): 1940 phrase_test('Looks Good To Me')
1940 self.OwnersTest(is_committing=False, change_tags={}, 1941 phrase_test('looks good to me')
1941 suggested_reviewers=['ben@example.com'], 1942 phrase_test('LGTM')
1942 expected_reviewers=['ben@example.com']) 1943 phrase_test('\\nlgtm')
1943 self.OwnersTest(is_committing=False, tbr=True, change_tags={}, 1944 phrase_test('> foo\\n> bar\\nlgtm\\n')
1944 suggested_reviewers=['ben@example.com'], 1945 phrase_test('no lgtm for you', approvers=set([]),
1945 expected_reviewers=['ben@example.com']) 1946 expected_output='Missing LGTM from someone other than '
1947 'john@example.com\n')
1946 1948
1947 def testCannedCheckOwners_CommittingWithoutOwnerLGTM(self): 1949 def testCannedCheckOwners_HTTPS_HostURL(self):
1948 self.OwnersTest(is_committing=True, 1950 self.AssertOwnersWorks(approvers=set(['ben@example.com']),
1949 approvers=set(), 1951 host_url='https://localhost')
1950 uncovered_files=set(['foo.cc']), 1952
1953 def testCannedCheckOwners_MissingSchemeInHostURL(self):
1954 self.AssertOwnersWorks(approvers=set(['ben@example.com']),
1955 host_url='localhost')
1956
1957 def testCannedCheckOwners_NoIssue(self):
1958 self.AssertOwnersWorks(issue=None,
1959 expected_output='Change not uploaded for review\n')
1960
1961 def testCannedCheckOwners_NoLGTM(self):
1962 self.AssertOwnersWorks(expected_output='Missing LGTM from someone '
1963 'other than john@example.com\n')
1964
1965 def testCannedCheckOwners_OnlyOwnerLGTM(self):
1966 self.AssertOwnersWorks(approvers=set(['john@example.com']),
1967 expected_output='Missing LGTM from someone '
1968 'other than john@example.com\n')
1969
1970 def testCannedCheckOwners_TBR(self):
1971 self.AssertOwnersWorks(tbr=True,
1972 expected_output='--tbr was specified, skipping OWNERS check\n')
1973
1974 def testCannedCheckOwners_Upload(self):
1975 class FakeInputAPI(object):
1976 is_committing = False
1977
1978 results = presubmit_canned_checks.CheckOwners(FakeInputAPI(),
1979 presubmit.OutputApi)
1980 self.assertEqual(results, [])
1981
1982 def testCannedCheckOwners_WithoutOwnerLGTM(self):
1983 self.AssertOwnersWorks(uncovered_files=set(['foo.cc']),
1951 expected_output='Missing LGTM from an OWNER for: foo.cc\n') 1984 expected_output='Missing LGTM from an OWNER for: foo.cc\n')
1952 1985
1953 def testCannedCheckOwners_CommittingWithLGTMs(self): 1986 def testCannedCheckOwners_WithLGTMs(self):
1954 self.OwnersTest(is_committing=True, 1987 self.AssertOwnersWorks(approvers=set(['ben@example.com']),
1955 approvers=set(['ben@example.com']), 1988 uncovered_files=set())
1956 uncovered_files=set())
1957 1989
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 1990
1974 1991
1975 if __name__ == '__main__': 1992 if __name__ == '__main__':
1976 import unittest 1993 import unittest
1977 unittest.main() 1994 unittest.main()
OLDNEW
« presubmit_canned_checks.py ('K') | « presubmit_canned_checks.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698