| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2009 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.py and presubmit_canned_checks.py.""" | 6 """Unit tests for presubmit.py and presubmit_canned_checks.py.""" |
| 7 | 7 |
| 8 import os | 8 import os |
| 9 import StringIO | 9 import StringIO |
| 10 import unittest | 10 import unittest |
| 11 | 11 |
| 12 # Local imports | 12 # Local imports |
| 13 import gcl | 13 import gcl |
| 14 import gclient |
| 14 import presubmit | 15 import presubmit |
| 15 import presubmit_canned_checks | 16 import presubmit_canned_checks |
| 16 | 17 |
| 17 | 18 |
| 18 class PresubmitTestsBase(unittest.TestCase): | 19 class PresubmitTestsBase(unittest.TestCase): |
| 19 """Setups and tear downs the mocks but doesn't test anything as-is.""" | 20 """Setups and tear downs the mocks but doesn't test anything as-is.""" |
| 20 def setUp(self): | 21 def setUp(self): |
| 21 self.original_IsFile = os.path.isfile | 22 self.original_IsFile = os.path.isfile |
| 22 def MockIsFile(f): | 23 def MockIsFile(f): |
| 23 dir = os.path.dirname(f) | 24 dir = os.path.dirname(f) |
| 24 return dir.endswith('haspresubmit') or dir == '' | 25 return dir.endswith('haspresubmit') or dir == '' |
| 25 os.path.isfile = MockIsFile | 26 os.path.isfile = MockIsFile |
| 26 | 27 |
| 27 self.original_GetSVNFileInfo = gcl.GetSVNFileInfo | 28 self.original_CaptureSVNInfo = gclient.CaptureSVNInfo |
| 28 def MockGetSVNFileInfo(path): | 29 def MockCaptureSVNInfo(path): |
| 29 if path.count('notfound'): | 30 if path.count('notfound'): |
| 30 return {} | 31 return {} |
| 31 results = { | 32 results = { |
| 32 'Path': path[len('svn:/foo/'):], | 33 'Path': path[len('svn:/foo/'):], |
| 33 'URL': 'svn:/foo/%s' % path.replace('\\', '/'), | 34 'URL': 'svn:/foo/%s' % path.replace('\\', '/'), |
| 34 } | 35 } |
| 35 if path.endswith('isdir'): | 36 if path.endswith('isdir'): |
| 36 results['Node Kind'] = 'directory' | 37 results['Node Kind'] = 'directory' |
| 37 else: | 38 else: |
| 38 results['Node Kind'] = 'file' | 39 results['Node Kind'] = 'file' |
| 39 return results | 40 return results |
| 40 gcl.GetSVNFileInfo = MockGetSVNFileInfo | 41 gclient.CaptureSVNInfo = MockCaptureSVNInfo |
| 41 | 42 |
| 42 self.original_GetSVNFileProperty = gcl.GetSVNFileProperty | 43 self.original_GetSVNFileProperty = gcl.GetSVNFileProperty |
| 43 def MockGetSVNFileProperty(path, property_name): | 44 def MockGetSVNFileProperty(path, property_name): |
| 44 if property_name == 'svn:secret-property': | 45 if property_name == 'svn:secret-property': |
| 45 return 'secret-property-value' | 46 return 'secret-property-value' |
| 46 elif path.count('binary'): | 47 elif path.count('binary'): |
| 47 return 'application/octet-stream' | 48 return 'application/octet-stream' |
| 48 else: | 49 else: |
| 49 if len(path) % 2: | 50 if len(path) % 2: |
| 50 return 'text/plain' | 51 return 'text/plain' |
| (...skipping 23 matching lines...) Expand all Loading... |
| 74 return 'one:%s\r\ntwo:%s' % (path, path) | 75 return 'one:%s\r\ntwo:%s' % (path, path) |
| 75 gcl.ReadFile = MockReadFile | 76 gcl.ReadFile = MockReadFile |
| 76 | 77 |
| 77 self.original_GetRepositoryRoot = gcl.GetRepositoryRoot | 78 self.original_GetRepositoryRoot = gcl.GetRepositoryRoot |
| 78 def MockGetRepositoryRoot(): | 79 def MockGetRepositoryRoot(): |
| 79 return '' | 80 return '' |
| 80 gcl.GetRepositoryRoot = MockGetRepositoryRoot | 81 gcl.GetRepositoryRoot = MockGetRepositoryRoot |
| 81 | 82 |
| 82 def tearDown(self): | 83 def tearDown(self): |
| 83 os.path.isfile = self.original_IsFile | 84 os.path.isfile = self.original_IsFile |
| 84 gcl.GetSVNFileInfo = self.original_GetSVNFileInfo | 85 gclient.CaptureSVNInfo = self.original_CaptureSVNInfo |
| 85 gcl.GetSVNFileProperty = self.original_GetSVNFileProperty | 86 gcl.GetSVNFileProperty = self.original_GetSVNFileProperty |
| 86 gcl.ReadFile = self.original_ReadFile | 87 gcl.ReadFile = self.original_ReadFile |
| 87 gcl.GetRepositoryRoot = self.original_GetRepositoryRoot | 88 gcl.GetRepositoryRoot = self.original_GetRepositoryRoot |
| 88 | 89 |
| 89 @staticmethod | 90 @staticmethod |
| 90 def MakeBasicChange(name, description): | 91 def MakeBasicChange(name, description): |
| 91 ci = gcl.ChangeInfo(name=name, | 92 ci = gcl.ChangeInfo(name=name, |
| 92 description=description, | 93 description=description, |
| 93 files=[]) | 94 files=[]) |
| 94 change = presubmit.GclChange(ci) | 95 change = presubmit.GclChange(ci) |
| 95 return change | 96 return change |
| 96 | 97 |
| 97 def compareMembers(self, object, members): | 98 def compareMembers(self, object, members): |
| 98 """If you add a member, be sure to add the relevant test!""" | 99 """If you add a member, be sure to add the relevant test!""" |
| 99 # Skip over members starting with '_' since they are usually not meant to | 100 # Skip over members starting with '_' since they are usually not meant to |
| 100 # be for public use. | 101 # be for public use. |
| 101 actual_members = [x for x in sorted(dir(object)) | 102 actual_members = [x for x in sorted(dir(object)) |
| 102 if not x.startswith('_')] | 103 if not x.startswith('_')] |
| 103 self.assertEqual(actual_members, sorted(members)) | 104 self.assertEqual(actual_members, sorted(members)) |
| 104 | 105 |
| 105 | 106 |
| 106 class PresubmitUnittest(PresubmitTestsBase): | 107 class PresubmitUnittest(PresubmitTestsBase): |
| 107 """General presubmit.py tests (excluding InputApi and OutputApi).""" | 108 """General presubmit.py tests (excluding InputApi and OutputApi).""" |
| 108 def testMembersChanged(self): | 109 def testMembersChanged(self): |
| 109 members = [ | 110 members = [ |
| 110 'AffectedFile', 'DoPresubmitChecks', 'GclChange', 'InputApi', | 111 'AffectedFile', 'DoPresubmitChecks', 'GclChange', 'InputApi', |
| 111 'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException', | 112 'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException', |
| 112 'OutputApi', 'ParseFiles', 'PresubmitExecuter', 'SPECIAL_KEYS', | 113 'OutputApi', 'ParseFiles', 'PresubmitExecuter', 'SPECIAL_KEYS', |
| 113 'ScanSubDirs', 'cPickle', 'cStringIO', 'exceptions', | 114 'ScanSubDirs', 'cPickle', 'cStringIO', 'exceptions', |
| 114 'fnmatch', 'gcl', 'glob', 'marshal', 'normpath', 'optparse', 'os', | 115 'fnmatch', 'gcl', 'gclient', 'glob', 'marshal', 'normpath', 'optparse', |
| 115 'pickle', 'presubmit_canned_checks', 're', 'subprocess', 'sys', | 116 'os', 'pickle', 'presubmit_canned_checks', 're', 'subprocess', 'sys', |
| 116 'tempfile', 'types', 'urllib2', | 117 'tempfile', 'types', 'urllib2', |
| 117 ] | 118 ] |
| 118 # If this test fails, you should add the relevant test. | 119 # If this test fails, you should add the relevant test. |
| 119 self.compareMembers(presubmit, members) | 120 self.compareMembers(presubmit, members) |
| 120 | 121 |
| 121 def testListRelevantPresubmitFiles(self): | 122 def testListRelevantPresubmitFiles(self): |
| 122 presubmit_files = presubmit.ListRelevantPresubmitFiles([ | 123 presubmit_files = presubmit.ListRelevantPresubmitFiles([ |
| 123 'blat.cc', | 124 'blat.cc', |
| 124 'foo/haspresubmit/yodle/smart.h', | 125 'foo/haspresubmit/yodle/smart.h', |
| 125 'moo/mat/gat/yo.h', | 126 'moo/mat/gat/yo.h', |
| (...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 self.failIf(presubmit_canned_checks.CheckTreeIsOpen( | 658 self.failIf(presubmit_canned_checks.CheckTreeIsOpen( |
| 658 self.MockInputApi(), presubmit.OutputApi, url='url_to_open', closed='0' | 659 self.MockInputApi(), presubmit.OutputApi, url='url_to_open', closed='0' |
| 659 )) | 660 )) |
| 660 self.failUnless(presubmit_canned_checks.CheckTreeIsOpen( | 661 self.failUnless(presubmit_canned_checks.CheckTreeIsOpen( |
| 661 self.MockInputApi(), presubmit.OutputApi, url='url_to_closed', closed='0' | 662 self.MockInputApi(), presubmit.OutputApi, url='url_to_closed', closed='0' |
| 662 )) | 663 )) |
| 663 | 664 |
| 664 | 665 |
| 665 if __name__ == '__main__': | 666 if __name__ == '__main__': |
| 666 unittest.main() | 667 unittest.main() |
| OLD | NEW |