| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 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,R0201,W0212,W0403 | 9 # pylint: disable=E1101,E1103,R0201,W0212,W0403 |
| 10 | 10 |
| (...skipping 2011 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2022 affected_file.LocalPath().AndReturn('foo.cc') | 2022 affected_file.LocalPath().AndReturn('foo.cc') |
| 2023 change.AffectedFiles(file_filter=None).AndReturn([affected_file]) | 2023 change.AffectedFiles(file_filter=None).AndReturn([affected_file]) |
| 2024 | 2024 |
| 2025 expected_host = 'http://localhost' | 2025 expected_host = 'http://localhost' |
| 2026 if host_url: | 2026 if host_url: |
| 2027 input_api.host_url = host_url | 2027 input_api.host_url = host_url |
| 2028 if host_url.startswith('https'): | 2028 if host_url.startswith('https'): |
| 2029 expected_host = host_url | 2029 expected_host = host_url |
| 2030 | 2030 |
| 2031 owner_email = 'john@example.com' | 2031 owner_email = 'john@example.com' |
| 2032 messages = list('{"sender": "' + a + '","text": "lgtm"}' for | |
| 2033 a in approvers) | |
| 2034 if not rietveld_response: | 2032 if not rietveld_response: |
| 2035 rietveld_response = ('{"owner_email": "' + owner_email + '",' | 2033 messages = ( |
| 2036 '"messages": [' + ','.join(messages) + ']}') | 2034 '{"sender": "%s", "text": "I approve", "approval": true}' % a |
| 2035 for a in approvers) |
| 2036 rietveld_response = '{"owner_email": "%s", "messages": [%s]}' % ( |
| 2037 owner_email, ','.join(messages)) |
| 2037 input_api.urllib2.urlopen( | 2038 input_api.urllib2.urlopen( |
| 2038 expected_host + '/api/1?messages=true').AndReturn( | 2039 expected_host + '/api/1?messages=true').AndReturn( |
| 2039 StringIO.StringIO(rietveld_response)) | 2040 StringIO.StringIO(rietveld_response)) |
| 2040 input_api.json = presubmit.json | 2041 input_api.json = presubmit.json |
| 2041 fake_db.files_not_covered_by(set(['foo.cc']), | 2042 fake_db.files_not_covered_by(set(['foo.cc']), |
| 2042 approvers.union(set([owner_email]))).AndReturn(uncovered_files) | 2043 approvers.union(set([owner_email]))).AndReturn(uncovered_files) |
| 2043 | 2044 |
| 2044 self.mox.ReplayAll() | 2045 self.mox.ReplayAll() |
| 2045 output = presubmit.PresubmitOutput() | 2046 output = presubmit.PresubmitOutput() |
| 2046 results = presubmit_canned_checks.CheckOwners(input_api, | 2047 results = presubmit_canned_checks.CheckOwners(input_api, |
| 2047 presubmit.OutputApi) | 2048 presubmit.OutputApi) |
| 2048 if results: | 2049 if results: |
| 2049 results[0].handle(output) | 2050 results[0].handle(output) |
| 2050 self.assertEquals(output.getvalue(), expected_output) | 2051 self.assertEquals(output.getvalue(), expected_output) |
| 2051 | 2052 |
| 2052 def testCannedCheckOwners_LGTMPhrases(self): | 2053 def testCannedCheckOwners_LGTMPhrases(self): |
| 2053 def phrase_test(phrase, approvers=None, expected_output=''): | 2054 def phrase_test(approval, approvers=None, expected_output=''): |
| 2054 if approvers is None: | 2055 if approvers is None: |
| 2055 approvers = set(['ben@example.com']) | 2056 approvers = set(['ben@example.com']) |
| 2056 self.AssertOwnersWorks(approvers=approvers, | 2057 self.AssertOwnersWorks(approvers=approvers, |
| 2057 rietveld_response='{"owner_email": "john@example.com",' + | 2058 rietveld_response=( |
| 2058 '"messages": [{"sender": "ben@example.com",' + | 2059 '{"owner_email": "john@example.com",' |
| 2059 '"text": "' + phrase + '"}]}', | 2060 '"messages": [{"sender": "ben@example.com",' |
| 2061 ' "text": "foo", "approval": %s}]}') % approval, |
| 2060 expected_output=expected_output) | 2062 expected_output=expected_output) |
| 2061 | 2063 |
| 2062 phrase_test('LGTM') | 2064 phrase_test('true') |
| 2063 phrase_test('\\nlgtm') | 2065 phrase_test('false', approvers=set(), |
| 2064 phrase_test('> foo\\n> bar\\nlgtm\\n') | |
| 2065 phrase_test('> LGTM', approvers=set(), | |
| 2066 expected_output='Missing LGTM from someone other than ' | 2066 expected_output='Missing LGTM from someone other than ' |
| 2067 'john@example.com\n') | 2067 'john@example.com\n') |
| 2068 | 2068 |
| 2069 # TODO(dpranke): these probably should pass. | |
| 2070 phrase_test('Looks Good To Me', approvers=set(), | |
| 2071 expected_output='Missing LGTM from someone other than ' | |
| 2072 'john@example.com\n') | |
| 2073 phrase_test('looks good to me', approvers=set(), | |
| 2074 expected_output='Missing LGTM from someone other than ' | |
| 2075 'john@example.com\n') | |
| 2076 | |
| 2077 # TODO(dpranke): this probably shouldn't pass. | |
| 2078 phrase_test('no lgtm for you') | |
| 2079 | |
| 2080 def testCannedCheckOwners_HTTPS_HostURL(self): | 2069 def testCannedCheckOwners_HTTPS_HostURL(self): |
| 2081 self.AssertOwnersWorks(approvers=set(['ben@example.com']), | 2070 self.AssertOwnersWorks(approvers=set(['ben@example.com']), |
| 2082 host_url='https://localhost') | 2071 host_url='https://localhost') |
| 2083 | 2072 |
| 2084 def testCannedCheckOwners_MissingSchemeInHostURL(self): | 2073 def testCannedCheckOwners_MissingSchemeInHostURL(self): |
| 2085 self.AssertOwnersWorks(approvers=set(['ben@example.com']), | 2074 self.AssertOwnersWorks(approvers=set(['ben@example.com']), |
| 2086 host_url='localhost') | 2075 host_url='localhost') |
| 2087 | 2076 |
| 2088 def testCannedCheckOwners_NoIssue(self): | 2077 def testCannedCheckOwners_NoIssue(self): |
| 2089 self.AssertOwnersWorks(issue=None, | 2078 self.AssertOwnersWorks(issue=None, |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2168 whitelist=['^a$', '^b$'], | 2157 whitelist=['^a$', '^b$'], |
| 2169 blacklist=['a']) | 2158 blacklist=['a']) |
| 2170 self.assertEqual(results, []) | 2159 self.assertEqual(results, []) |
| 2171 self.checkstdout( | 2160 self.checkstdout( |
| 2172 'Running %s\n' % presubmit.os.path.join('random_directory', 'b')) | 2161 'Running %s\n' % presubmit.os.path.join('random_directory', 'b')) |
| 2173 | 2162 |
| 2174 | 2163 |
| 2175 if __name__ == '__main__': | 2164 if __name__ == '__main__': |
| 2176 import unittest | 2165 import unittest |
| 2177 unittest.main() | 2166 unittest.main() |
| OLD | NEW |