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_support.py and presubmit_canned_checks.py.""" |
7 | 7 |
8 import os | 8 import os |
9 import StringIO | 9 import StringIO |
10 import sys | 10 import sys |
11 import unittest | 11 import unittest |
12 | 12 |
13 # Local imports | 13 # Local imports |
14 import gcl | 14 import gcl |
15 import gclient | 15 import gclient |
16 import presubmit_support as presubmit | 16 import presubmit_support as presubmit |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 def compareMembers(self, object, members): | 104 def compareMembers(self, object, members): |
105 """If you add a member, be sure to add the relevant test!""" | 105 """If you add a member, be sure to add the relevant test!""" |
106 # Skip over members starting with '_' since they are usually not meant to | 106 # Skip over members starting with '_' since they are usually not meant to |
107 # be for public use. | 107 # be for public use. |
108 actual_members = [x for x in sorted(dir(object)) | 108 actual_members = [x for x in sorted(dir(object)) |
109 if not x.startswith('_')] | 109 if not x.startswith('_')] |
110 self.assertEqual(actual_members, sorted(members)) | 110 self.assertEqual(actual_members, sorted(members)) |
111 | 111 |
112 | 112 |
113 class PresubmitUnittest(PresubmitTestsBase): | 113 class PresubmitUnittest(PresubmitTestsBase): |
114 """General presubmit.py tests (excluding InputApi and OutputApi).""" | 114 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" |
115 def testMembersChanged(self): | 115 def testMembersChanged(self): |
116 members = [ | 116 members = [ |
117 'AffectedFile', 'DoPresubmitChecks', 'GclChange', 'InputApi', | 117 'AffectedFile', 'DoPresubmitChecks', 'GclChange', 'InputApi', |
118 'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException', | 118 'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException', |
119 'OutputApi', 'ParseFiles', 'PresubmitExecuter', 'SPECIAL_KEYS', | 119 'OutputApi', 'ParseFiles', 'PresubmitExecuter', 'SPECIAL_KEYS', |
120 'ScanSubDirs', 'cPickle', 'cStringIO', 'exceptions', | 120 'ScanSubDirs', 'SvnAffectedFile', 'cPickle', 'cStringIO', 'exceptions', |
121 'fnmatch', 'gcl', 'gclient', 'glob', 'marshal', 'normpath', 'optparse', | 121 'fnmatch', 'gcl', 'gclient', 'glob', 'marshal', 'normpath', 'optparse', |
122 'os', 'pickle', 'presubmit_canned_checks', 're', 'subprocess', 'sys', | 122 'os', 'pickle', 'presubmit_canned_checks', 're', 'subprocess', 'sys', |
123 'tempfile', 'types', 'urllib2', | 123 'tempfile', 'types', 'urllib2', |
124 ] | 124 ] |
125 # If this test fails, you should add the relevant test. | 125 # If this test fails, you should add the relevant test. |
126 self.compareMembers(presubmit, members) | 126 self.compareMembers(presubmit, members) |
127 | 127 |
128 def testListRelevantPresubmitFiles(self): | 128 def testListRelevantPresubmitFiles(self): |
129 presubmit_files = presubmit.ListRelevantPresubmitFiles([ | 129 presubmit_files = presubmit.ListRelevantPresubmitFiles([ |
130 'blat.cc', | 130 'blat.cc', |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 self.failUnless(rhs_lines[1][2] == 'two:%s' % files[0][1]) | 209 self.failUnless(rhs_lines[1][2] == 'two:%s' % files[0][1]) |
210 | 210 |
211 self.failUnless(rhs_lines[2][0].LocalPath() == files[3][1]) | 211 self.failUnless(rhs_lines[2][0].LocalPath() == files[3][1]) |
212 self.failUnless(rhs_lines[2][1] == 1) | 212 self.failUnless(rhs_lines[2][1] == 1) |
213 self.failUnless(rhs_lines[2][2] == 'one:%s' % files[3][1]) | 213 self.failUnless(rhs_lines[2][2] == 'one:%s' % files[3][1]) |
214 | 214 |
215 self.failUnless(rhs_lines[3][0].LocalPath() == files[3][1]) | 215 self.failUnless(rhs_lines[3][0].LocalPath() == files[3][1]) |
216 self.failUnless(rhs_lines[3][1] == 2) | 216 self.failUnless(rhs_lines[3][1] == 2) |
217 self.failUnless(rhs_lines[3][2] == 'two:%s' % files[3][1]) | 217 self.failUnless(rhs_lines[3][2] == 'two:%s' % files[3][1]) |
218 | 218 |
219 def testAffectedFile(self): | |
220 af = presubmit.AffectedFile('foo/blat.cc', 'M') | |
221 self.failUnless(af.ServerPath() == 'svn:/foo/foo/blat.cc') | |
222 self.failUnless(af.LocalPath() == presubmit.normpath('foo/blat.cc')) | |
223 self.failUnless(af.Action() == 'M') | |
224 self.failUnless(af.NewContents() == ['one:%s' % af.LocalPath(), | |
225 'two:%s' % af.LocalPath()]) | |
226 | |
227 af = presubmit.AffectedFile('notfound.cc', 'A') | |
228 self.failUnless(af.ServerPath() == '') | |
229 | |
230 def testExecPresubmitScript(self): | 219 def testExecPresubmitScript(self): |
231 description_lines = ('Hello there', | 220 description_lines = ('Hello there', |
232 'this is a change', | 221 'this is a change', |
233 'STORY=http://tracker/123') | 222 'STORY=http://tracker/123') |
234 files = [ | 223 files = [ |
235 ['A', 'foo\\blat.cc'], | 224 ['A', 'foo\\blat.cc'], |
236 ] | 225 ] |
237 ci = gcl.ChangeInfo(name='mychange', | 226 ci = gcl.ChangeInfo(name='mychange', |
238 description='\n'.join(description_lines), | 227 description='\n'.join(description_lines), |
239 files=files) | 228 files=files) |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 files=files) | 387 files=files) |
399 change = presubmit.GclChange(ci) | 388 change = presubmit.GclChange(ci) |
400 | 389 |
401 affected_files = change.AffectedFiles(include_dirs=False) | 390 affected_files = change.AffectedFiles(include_dirs=False) |
402 self.failUnless(len(affected_files) == 1) | 391 self.failUnless(len(affected_files) == 1) |
403 self.failUnless(affected_files[0].LocalPath().endswith('blat.cc')) | 392 self.failUnless(affected_files[0].LocalPath().endswith('blat.cc')) |
404 | 393 |
405 affected_files_and_dirs = change.AffectedFiles(include_dirs=True) | 394 affected_files_and_dirs = change.AffectedFiles(include_dirs=True) |
406 self.failUnless(len(affected_files_and_dirs) == 2) | 395 self.failUnless(len(affected_files_and_dirs) == 2) |
407 | 396 |
408 def testSvnProperty(self): | |
409 affected_file = presubmit.AffectedFile('foo.cc', 'A') | |
410 self.failUnless(affected_file.SvnProperty('svn:secret-property') == | |
411 'secret-property-value') | |
412 | |
413 | 397 |
414 class InputApiUnittest(PresubmitTestsBase): | 398 class InputApiUnittest(PresubmitTestsBase): |
415 """Tests presubmit.InputApi.""" | 399 """Tests presubmit.InputApi.""" |
416 def testMembersChanged(self): | 400 def testMembersChanged(self): |
417 members = [ | 401 members = [ |
418 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', | 402 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedTextFiles', |
419 'DepotToLocalPath', 'FilterTextFiles', 'LocalPaths', 'LocalToDepotPath', | 403 'DepotToLocalPath', 'FilterTextFiles', 'LocalPaths', 'LocalToDepotPath', |
420 'PresubmitLocalPath', 'RightHandSideLines', 'ServerPaths', | 404 'PresubmitLocalPath', 'RightHandSideLines', 'ServerPaths', |
421 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', | 405 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', |
422 'marshal', 'os_path', 'pickle', 'platform', | 406 'marshal', 'os_path', 'pickle', 'platform', |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 self.failIf(warning._Handle(output, input)) | 574 self.failIf(warning._Handle(output, input)) |
591 self.failUnless(output.getvalue().count('???')) | 575 self.failUnless(output.getvalue().count('???')) |
592 | 576 |
593 output = StringIO.StringIO() | 577 output = StringIO.StringIO() |
594 input = StringIO.StringIO('\n') | 578 input = StringIO.StringIO('\n') |
595 warning = presubmit.OutputApi.PresubmitPromptWarning('???') | 579 warning = presubmit.OutputApi.PresubmitPromptWarning('???') |
596 self.failIf(warning._Handle(output, input)) | 580 self.failIf(warning._Handle(output, input)) |
597 self.failUnless(output.getvalue().count('???')) | 581 self.failUnless(output.getvalue().count('???')) |
598 | 582 |
599 | 583 |
| 584 class AffectedFileUnittest(PresubmitTestsBase): |
| 585 def testMembersChanged(self): |
| 586 members = [ |
| 587 'AbsoluteLocalPath', 'Action', 'IsDirectory', 'LocalPath', 'NewContents', |
| 588 'OldContents', 'OldFileTempPath', 'Property', 'ServerPath', 'action', |
| 589 'is_directory', 'path', 'properties', 'repository_root', 'server_path', |
| 590 ] |
| 591 # If this test fails, you should add the relevant test. |
| 592 self.compareMembers(presubmit.AffectedFile('a', 'b'), members) |
| 593 self.compareMembers(presubmit.SvnAffectedFile('a', 'b'), members) |
| 594 |
| 595 def testAffectedFile(self): |
| 596 af = presubmit.SvnAffectedFile('foo/blat.cc', 'M') |
| 597 self.failUnless(af.ServerPath() == 'svn:/foo/foo/blat.cc') |
| 598 self.failUnless(af.LocalPath() == presubmit.normpath('foo/blat.cc')) |
| 599 self.failUnless(af.Action() == 'M') |
| 600 self.failUnless(af.NewContents() == ['one:%s' % af.LocalPath(), |
| 601 'two:%s' % af.LocalPath()]) |
| 602 |
| 603 af = presubmit.AffectedFile('notfound.cc', 'A') |
| 604 self.failUnless(af.ServerPath() == '') |
| 605 |
| 606 def testProperty(self): |
| 607 affected_file = presubmit.SvnAffectedFile('foo.cc', 'A') |
| 608 self.failUnless(affected_file.Property('svn:secret-property') == |
| 609 'secret-property-value') |
| 610 |
| 611 |
600 class CannedChecksUnittest(PresubmitTestsBase): | 612 class CannedChecksUnittest(PresubmitTestsBase): |
601 """Tests presubmit_canned_checks.py.""" | 613 """Tests presubmit_canned_checks.py.""" |
602 class MockInputApi(object): | 614 class MockInputApi(object): |
603 class MockUrllib2(object): | 615 class MockUrllib2(object): |
604 class urlopen(object): | 616 class urlopen(object): |
605 def __init__(self, url): | 617 def __init__(self, url): |
606 if url == 'url_to_open': | 618 if url == 'url_to_open': |
607 self.result = '1' | 619 self.result = '1' |
608 else: | 620 else: |
609 self.result = '0' | 621 self.result = '0' |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 # TODO(maruel): Add real tests. | 744 # TODO(maruel): Add real tests. |
733 self.failIf(presubmit_canned_checks.RunPythonUnitTests( | 745 self.failIf(presubmit_canned_checks.RunPythonUnitTests( |
734 self.MockInputApi(), | 746 self.MockInputApi(), |
735 presubmit.OutputApi, [])) | 747 presubmit.OutputApi, [])) |
736 self.failUnless(presubmit_canned_checks.RunPythonUnitTests( | 748 self.failUnless(presubmit_canned_checks.RunPythonUnitTests( |
737 self.MockInputApi(), | 749 self.MockInputApi(), |
738 presubmit.OutputApi, ['non_existent_module'])) | 750 presubmit.OutputApi, ['non_existent_module'])) |
739 | 751 |
740 if __name__ == '__main__': | 752 if __name__ == '__main__': |
741 unittest.main() | 753 unittest.main() |
OLD | NEW |