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 1865 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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=None, |
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=None, expected_output=''): |
1889 host_url=None): | 1889 if approvers is None: |
| 1890 approvers = set() |
| 1891 if uncovered_files is None: |
| 1892 uncovered_files = set() |
| 1893 |
| 1894 change = self.mox.CreateMock(presubmit.Change) |
| 1895 change.issue = issue |
1890 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1896 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) | 1897 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) | 1898 fake_db = self.mox.CreateMock(owners.Database) |
1902 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP) | 1899 fake_db.email_regexp = input_api.re.compile(owners.BASIC_EMAIL_REGEXP) |
1903 input_api.owners_db = fake_db | 1900 input_api.owners_db = fake_db |
1904 input_api.is_committing = is_committing | 1901 input_api.is_committing = True |
1905 input_api.tbr = tbr | 1902 input_api.tbr = tbr |
1906 | 1903 |
1907 if is_committing and not tbr: | 1904 if not tbr and issue: |
1908 change.issue = '1' | 1905 affected_file.LocalPath().AndReturn('foo.cc') |
| 1906 change.AffectedFiles(None).AndReturn([affected_file]) |
| 1907 |
| 1908 expected_host = 'http://localhost' |
| 1909 if host_url: |
| 1910 input_api.host_url = host_url |
| 1911 if host_url.startswith('https'): |
| 1912 expected_host = host_url |
| 1913 |
| 1914 owner = 'john@example.com' |
1909 messages = list('{"sender": "' + a + '","text": "lgtm"}' for | 1915 messages = list('{"sender": "' + a + '","text": "lgtm"}' for |
1910 a in approvers) | 1916 a in approvers) |
1911 rietveld_response = ('{"owner": "john@example.com",' | 1917 if not rietveld_response: |
1912 '"messages": [' + ','.join(messages) + ']}') | 1918 rietveld_response = ('{"owner": "' + owner + '",' |
| 1919 '"messages": [' + ','.join(messages) + ']}') |
1913 input_api.urllib2.urlopen( | 1920 input_api.urllib2.urlopen( |
1914 expected_host + '/api/1?messages=true').AndReturn( | 1921 expected_host + '/api/1?messages=true').AndReturn( |
1915 StringIO.StringIO(rietveld_response)) | 1922 StringIO.StringIO(rietveld_response)) |
1916 input_api.json = presubmit.json | 1923 input_api.json = presubmit.json |
1917 fake_db.files_not_covered_by(set(['foo.cc']), approvers).AndReturn( | 1924 fake_db.files_not_covered_by(set(['foo.cc']), |
1918 uncovered_files) | 1925 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 | 1926 |
1924 self.mox.ReplayAll() | 1927 self.mox.ReplayAll() |
1925 output = presubmit.PresubmitOutput() | 1928 output = presubmit.PresubmitOutput() |
1926 results = presubmit_canned_checks.CheckOwners(input_api, | 1929 results = presubmit_canned_checks.CheckOwners(input_api, |
1927 presubmit.OutputApi) | 1930 presubmit.OutputApi) |
1928 if results: | 1931 if results: |
1929 results[0].handle(output) | 1932 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) | 1933 self.assertEquals(output.getvalue(), expected_output) |
1933 | 1934 |
1934 def testCannedCheckOwners_WithReviewer(self): | 1935 def testCannedCheckOwners_LGTMPhrases(self): |
1935 self.OwnersTest(is_committing=False, change_tags={'R': 'ben@example.com'}) | 1936 def phrase_test(phrase, approvers=None, expected_output=''): |
1936 self.OwnersTest(is_committing=False, tbr=True, | 1937 if approvers is None: |
1937 change_tags={'R': 'ben@example.com'}) | 1938 approvers = set(['ben@example.com']) |
| 1939 self.AssertOwnersWorks(approvers=approvers, |
| 1940 rietveld_response='{"owner": "john@example.com",' + |
| 1941 '"messages": [{"sender": "ben@example.com",' + |
| 1942 '"text": "' + phrase + '"}]}', |
| 1943 expected_output=expected_output) |
1938 | 1944 |
1939 def testCannedCheckOwners_NoReviewer(self): | 1945 phrase_test('Looks Good To Me', approvers=set(), |
1940 self.OwnersTest(is_committing=False, change_tags={}, | 1946 expected_output='Missing LGTM from someone other than ' |
1941 suggested_reviewers=['ben@example.com'], | 1947 'john@example.com\n') |
1942 expected_reviewers=['ben@example.com']) | 1948 phrase_test('looks good to me', approvers=set(), |
1943 self.OwnersTest(is_committing=False, tbr=True, change_tags={}, | 1949 expected_output='Missing LGTM from someone other than ' |
1944 suggested_reviewers=['ben@example.com'], | 1950 'john@example.com\n') |
1945 expected_reviewers=['ben@example.com']) | 1951 phrase_test('LGTM') |
| 1952 phrase_test('\\nlgtm') |
| 1953 phrase_test('> foo\\n> bar\\nlgtm\\n') |
1946 | 1954 |
1947 def testCannedCheckOwners_CommittingWithoutOwnerLGTM(self): | 1955 # TODO(dpranke): this probably shouldn't pass. |
1948 self.OwnersTest(is_committing=True, | 1956 phrase_test('no lgtm for you') |
1949 approvers=set(), | 1957 |
1950 uncovered_files=set(['foo.cc']), | 1958 def testCannedCheckOwners_HTTPS_HostURL(self): |
| 1959 self.AssertOwnersWorks(approvers=set(['ben@example.com']), |
| 1960 host_url='https://localhost') |
| 1961 |
| 1962 def testCannedCheckOwners_MissingSchemeInHostURL(self): |
| 1963 self.AssertOwnersWorks(approvers=set(['ben@example.com']), |
| 1964 host_url='localhost') |
| 1965 |
| 1966 def testCannedCheckOwners_NoIssue(self): |
| 1967 self.AssertOwnersWorks(issue=None, |
| 1968 expected_output='Change not uploaded for review\n') |
| 1969 |
| 1970 def testCannedCheckOwners_NoLGTM(self): |
| 1971 self.AssertOwnersWorks(expected_output='Missing LGTM from someone ' |
| 1972 'other than john@example.com\n') |
| 1973 |
| 1974 def testCannedCheckOwners_OnlyOwnerLGTM(self): |
| 1975 self.AssertOwnersWorks(approvers=set(['john@example.com']), |
| 1976 expected_output='Missing LGTM from someone ' |
| 1977 'other than john@example.com\n') |
| 1978 |
| 1979 def testCannedCheckOwners_TBR(self): |
| 1980 self.AssertOwnersWorks(tbr=True, |
| 1981 expected_output='--tbr was specified, skipping OWNERS check\n') |
| 1982 |
| 1983 def testCannedCheckOwners_Upload(self): |
| 1984 class FakeInputAPI(object): |
| 1985 is_committing = False |
| 1986 |
| 1987 results = presubmit_canned_checks.CheckOwners(FakeInputAPI(), |
| 1988 presubmit.OutputApi) |
| 1989 self.assertEqual(results, []) |
| 1990 |
| 1991 def testCannedCheckOwners_WithoutOwnerLGTM(self): |
| 1992 self.AssertOwnersWorks(uncovered_files=set(['foo.cc']), |
1951 expected_output='Missing LGTM from an OWNER for: foo.cc\n') | 1993 expected_output='Missing LGTM from an OWNER for: foo.cc\n') |
1952 | 1994 |
1953 def testCannedCheckOwners_CommittingWithLGTMs(self): | 1995 def testCannedCheckOwners_WithLGTMs(self): |
1954 self.OwnersTest(is_committing=True, | 1996 self.AssertOwnersWorks(approvers=set(['ben@example.com']), |
1955 approvers=set(['ben@example.com']), | 1997 uncovered_files=set()) |
1956 uncovered_files=set()) | |
1957 | 1998 |
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 | 1999 |
1974 | 2000 |
1975 if __name__ == '__main__': | 2001 if __name__ == '__main__': |
1976 import unittest | 2002 import unittest |
1977 unittest.main() | 2003 unittest.main() |
OLD | NEW |