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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 self.fake_root_dir = self.RootDir() | 112 self.fake_root_dir = self.RootDir() |
113 # Special mocks. | 113 # Special mocks. |
114 def MockAbsPath(f): | 114 def MockAbsPath(f): |
115 return f | 115 return f |
116 def MockChdir(f): | 116 def MockChdir(f): |
117 return None | 117 return None |
118 # SuperMoxTestBase already mock these but simplify our life. | 118 # SuperMoxTestBase already mock these but simplify our life. |
119 presubmit.os.path.abspath = MockAbsPath | 119 presubmit.os.path.abspath = MockAbsPath |
120 presubmit.os.getcwd = self.RootDir | 120 presubmit.os.getcwd = self.RootDir |
121 presubmit.os.chdir = MockChdir | 121 presubmit.os.chdir = MockChdir |
| 122 self.mox.StubOutWithMock(presubmit.scm, 'determine_scm') |
122 self.mox.StubOutWithMock(presubmit.scm.SVN, 'CaptureInfo') | 123 self.mox.StubOutWithMock(presubmit.scm.SVN, 'CaptureInfo') |
123 self.mox.StubOutWithMock(presubmit.scm.SVN, 'GetFileProperty') | 124 self.mox.StubOutWithMock(presubmit.scm.SVN, 'GetFileProperty') |
124 self.mox.StubOutWithMock(presubmit.gclient_utils, 'FileRead') | 125 self.mox.StubOutWithMock(presubmit.gclient_utils, 'FileRead') |
125 self.mox.StubOutWithMock(presubmit.gclient_utils, 'FileWrite') | 126 self.mox.StubOutWithMock(presubmit.gclient_utils, 'FileWrite') |
126 self.mox.StubOutWithMock(presubmit.scm.SVN, 'GenerateDiff') | 127 self.mox.StubOutWithMock(presubmit.scm.SVN, 'GenerateDiff') |
127 | 128 |
128 | 129 |
129 class PresubmitUnittest(PresubmitTestsBase): | 130 class PresubmitUnittest(PresubmitTestsBase): |
130 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" | 131 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" |
131 | 132 |
132 _INHERIT_SETTINGS = 'inherit-review-settings-ok' | 133 _INHERIT_SETTINGS = 'inherit-review-settings-ok' |
133 | 134 |
134 def testMembersChanged(self): | 135 def testMembersChanged(self): |
135 self.mox.ReplayAll() | 136 self.mox.ReplayAll() |
136 members = [ | 137 members = [ |
137 'AffectedFile', 'Change', 'DoGetTrySlaves', 'DoPresubmitChecks', | 138 'AffectedFile', 'Change', 'DoGetTrySlaves', 'DoPresubmitChecks', |
138 'GetTrySlavesExecuter', 'GitAffectedFile', 'GitChange', | 139 'GetTrySlavesExecuter', 'GitAffectedFile', |
139 'InputApi', 'ListRelevantPresubmitFiles', 'Main', | 140 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', |
140 'NotImplementedException', 'OutputApi', 'ParseFiles', | 141 'NotImplementedException', 'OutputApi', 'ParseFiles', |
141 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', | 142 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', |
142 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cStringIO', | 143 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cStringIO', |
143 'exceptions', 'fnmatch', 'gclient_utils', 'glob', 'json', | 144 'exceptions', 'fnmatch', 'gclient_utils', 'glob', 'json', 'load_files', |
144 'logging', 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', | 145 'logging', 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', |
145 'presubmit_canned_checks', 'random', 're', 'scm', 'subprocess', | 146 'presubmit_canned_checks', 'random', 're', 'scm', 'subprocess', |
146 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', 'urllib2', | 147 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', 'urllib2', |
147 'warn', | 148 'warn', |
148 ] | 149 ] |
149 # If this test fails, you should add the relevant test. | 150 # If this test fails, you should add the relevant test. |
150 self.compareMembers(presubmit, members) | 151 self.compareMembers(presubmit, members) |
151 | 152 |
152 def testListRelevantPresubmitFiles(self): | 153 def testListRelevantPresubmitFiles(self): |
153 join = presubmit.os.path.join | 154 join = presubmit.os.path.join |
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 output = StringIO.StringIO() | 656 output = StringIO.StringIO() |
656 self.assertEqual(['win'], | 657 self.assertEqual(['win'], |
657 presubmit.DoGetTrySlaves([filename], self.fake_root_dir, | 658 presubmit.DoGetTrySlaves([filename], self.fake_root_dir, |
658 None, False, output)) | 659 None, False, output)) |
659 output = StringIO.StringIO() | 660 output = StringIO.StringIO() |
660 self.assertEqual(['win', 'linux'], | 661 self.assertEqual(['win', 'linux'], |
661 presubmit.DoGetTrySlaves([filename, filename_linux], | 662 presubmit.DoGetTrySlaves([filename, filename_linux], |
662 self.fake_root_dir, None, False, | 663 self.fake_root_dir, None, False, |
663 output)) | 664 output)) |
664 | 665 |
665 def testMain(self): | 666 def testMainUnversioned(self): |
666 # OptParser calls presubmit.os.path.exists and is a pain when mocked. | 667 # OptParser calls presubmit.os.path.exists and is a pain when mocked. |
667 self.UnMock(presubmit.os.path, 'exists') | 668 self.UnMock(presubmit.os.path, 'exists') |
668 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') | 669 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') |
669 self.mox.StubOutWithMock(presubmit, 'ParseFiles') | 670 self.mox.StubOutWithMock(presubmit, 'ParseFiles') |
670 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.svn') | 671 presubmit.scm.determine_scm(self.fake_root_dir).AndReturn(None) |
671 ).AndReturn(False) | 672 presubmit.ParseFiles(['random_file.txt'], None |
672 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.git') | 673 ).AndReturn(['random_file.txt']) |
673 ).AndReturn(False) | |
674 presubmit.subprocess.call( | |
675 ['git', 'rev-parse', '--show-cdup'], | |
676 cwd=self.fake_root_dir, | |
677 stdout=presubmit.subprocess.PIPE).AndReturn(1) | |
678 output = self.mox.CreateMock(presubmit.PresubmitOutput) | 674 output = self.mox.CreateMock(presubmit.PresubmitOutput) |
679 output.should_continue().AndReturn(False) | 675 output.should_continue().AndReturn(False) |
680 | 676 |
681 presubmit.DoPresubmitChecks(mox.IgnoreArg(), False, False, | 677 presubmit.DoPresubmitChecks(mox.IgnoreArg(), False, False, |
682 mox.IgnoreArg(), | 678 mox.IgnoreArg(), |
683 mox.IgnoreArg(), | 679 mox.IgnoreArg(), |
684 None, False).AndReturn(output) | 680 None, False).AndReturn(output) |
685 self.mox.ReplayAll() | 681 self.mox.ReplayAll() |
686 | 682 |
687 self.assertEquals(True, | 683 self.assertEquals( |
688 presubmit.Main(['presubmit', '--root', | 684 True, |
689 self.fake_root_dir])) | 685 presubmit.Main(['--root', self.fake_root_dir, 'random_file.txt'])) |
| 686 |
| 687 def testMainUnversionedFail(self): |
| 688 # OptParser calls presubmit.os.path.exists and is a pain when mocked. |
| 689 self.UnMock(presubmit.os.path, 'exists') |
| 690 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') |
| 691 self.mox.StubOutWithMock(presubmit, 'ParseFiles') |
| 692 presubmit.scm.determine_scm(self.fake_root_dir).AndReturn(None) |
| 693 self.mox.StubOutWithMock(presubmit.sys, 'stderr') |
| 694 presubmit.sys.stderr.write( |
| 695 'Usage: presubmit_unittest.py [options] <files...>\n') |
| 696 presubmit.sys.stderr.write('\n') |
| 697 presubmit.sys.stderr.write( |
| 698 'presubmit_unittest.py: error: For unversioned directory, <files> is ' |
| 699 'not optional.\n') |
| 700 self.mox.ReplayAll() |
| 701 |
| 702 try: |
| 703 presubmit.Main(['--root', self.fake_root_dir]) |
| 704 self.fail() |
| 705 except SystemExit, e: |
| 706 self.assertEquals(2, e.code) |
690 | 707 |
691 | 708 |
692 class InputApiUnittest(PresubmitTestsBase): | 709 class InputApiUnittest(PresubmitTestsBase): |
693 """Tests presubmit.InputApi.""" | 710 """Tests presubmit.InputApi.""" |
694 def testMembersChanged(self): | 711 def testMembersChanged(self): |
695 self.mox.ReplayAll() | 712 self.mox.ReplayAll() |
696 members = [ | 713 members = [ |
697 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', | 714 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', |
698 'AffectedTextFiles', | 715 'AffectedTextFiles', |
699 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', | 716 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', |
(...skipping 1250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1950 | 1967 |
1951 def testCannedCheckOwners_HTTPS_HostURL(self): | 1968 def testCannedCheckOwners_HTTPS_HostURL(self): |
1952 self.OwnersTest(is_committing=True, | 1969 self.OwnersTest(is_committing=True, |
1953 approvers=set(['ben@example.com']), | 1970 approvers=set(['ben@example.com']), |
1954 uncovered_files=set(), host_url='https://localhost') | 1971 uncovered_files=set(), host_url='https://localhost') |
1955 | 1972 |
1956 | 1973 |
1957 if __name__ == '__main__': | 1974 if __name__ == '__main__': |
1958 import unittest | 1975 import unittest |
1959 unittest.main() | 1976 unittest.main() |
OLD | NEW |