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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 | 127 |
128 | 128 |
129 class PresubmitUnittest(PresubmitTestsBase): | 129 class PresubmitUnittest(PresubmitTestsBase): |
130 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" | 130 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" |
131 | 131 |
132 _INHERIT_SETTINGS = 'inherit-review-settings-ok' | 132 _INHERIT_SETTINGS = 'inherit-review-settings-ok' |
133 | 133 |
134 def testMembersChanged(self): | 134 def testMembersChanged(self): |
135 self.mox.ReplayAll() | 135 self.mox.ReplayAll() |
136 members = [ | 136 members = [ |
137 'AffectedFile', 'Change', 'DoGetTrySlaves', 'DoPresubmitChecks', | 137 'AffectedFile', 'Change', 'DetermineScm', 'DoGetTrySlaves', |
138 'GetTrySlavesExecuter', 'GitAffectedFile', 'GitChange', | 138 'DoPresubmitChecks', 'GetTrySlavesExecuter', 'GitAffectedFile', |
139 'InputApi', 'ListRelevantPresubmitFiles', 'Main', | 139 'GitChange', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', |
140 'NotImplementedException', 'OutputApi', 'ParseFiles', | 140 'NotImplementedException', 'OutputApi', 'ParseFiles', |
141 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', | 141 'PresubmitExecuter', 'PresubmitOutput', 'ScanSubDirs', |
142 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cStringIO', | 142 'SvnAffectedFile', 'SvnChange', 'cPickle', 'cStringIO', |
143 'exceptions', 'fnmatch', 'gclient_utils', 'glob', 'json', | 143 'exceptions', 'fnmatch', 'gclient_utils', 'glob', 'json', |
144 'logging', 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', | 144 'logging', 'marshal', 'normpath', 'optparse', 'os', 'owners', 'pickle', |
145 'presubmit_canned_checks', 'random', 're', 'scm', 'subprocess', | 145 'presubmit_canned_checks', 'random', 're', 'scm', 'subprocess', |
146 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', 'urllib2', | 146 'sys', 'tempfile', 'time', 'traceback', 'types', 'unittest', 'urllib2', |
147 'warn', | 147 'warn', |
148 ] | 148 ] |
149 # If this test fails, you should add the relevant test. | 149 # If this test fails, you should add the relevant test. |
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
655 output = StringIO.StringIO() | 655 output = StringIO.StringIO() |
656 self.assertEqual(['win'], | 656 self.assertEqual(['win'], |
657 presubmit.DoGetTrySlaves([filename], self.fake_root_dir, | 657 presubmit.DoGetTrySlaves([filename], self.fake_root_dir, |
658 None, False, output)) | 658 None, False, output)) |
659 output = StringIO.StringIO() | 659 output = StringIO.StringIO() |
660 self.assertEqual(['win', 'linux'], | 660 self.assertEqual(['win', 'linux'], |
661 presubmit.DoGetTrySlaves([filename, filename_linux], | 661 presubmit.DoGetTrySlaves([filename, filename_linux], |
662 self.fake_root_dir, None, False, | 662 self.fake_root_dir, None, False, |
663 output)) | 663 output)) |
664 | 664 |
665 def testMain(self): | 665 def testMainUnversioned(self): |
666 # OptParser calls presubmit.os.path.exists and is a pain when mocked. | 666 # OptParser calls presubmit.os.path.exists and is a pain when mocked. |
667 self.UnMock(presubmit.os.path, 'exists') | 667 self.UnMock(presubmit.os.path, 'exists') |
668 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') | 668 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') |
669 self.mox.StubOutWithMock(presubmit, 'ParseFiles') | 669 self.mox.StubOutWithMock(presubmit, 'ParseFiles') |
670 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.svn') | 670 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.svn') |
671 ).AndReturn(False) | 671 ).AndReturn(False) |
672 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.git') | 672 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.git') |
673 ).AndReturn(False) | 673 ).AndReturn(False) |
674 presubmit.subprocess.call( | 674 presubmit.subprocess.call( |
675 ['git', 'rev-parse', '--show-cdup'], | 675 ['git', 'rev-parse', '--show-cdup'], |
676 cwd=self.fake_root_dir, | 676 cwd=self.fake_root_dir, |
677 stdout=presubmit.subprocess.PIPE).AndReturn(1) | 677 stdout=presubmit.subprocess.PIPE).AndReturn(1) |
678 presubmit.ParseFiles(['random_file.txt'], None | |
679 ).AndReturn(['random_file.txt']) | |
678 output = self.mox.CreateMock(presubmit.PresubmitOutput) | 680 output = self.mox.CreateMock(presubmit.PresubmitOutput) |
679 output.should_continue().AndReturn(False) | 681 output.should_continue().AndReturn(False) |
680 | 682 |
681 presubmit.DoPresubmitChecks(mox.IgnoreArg(), False, False, | 683 presubmit.DoPresubmitChecks(mox.IgnoreArg(), False, False, |
682 mox.IgnoreArg(), | 684 mox.IgnoreArg(), |
683 mox.IgnoreArg(), | 685 mox.IgnoreArg(), |
684 None, False).AndReturn(output) | 686 None, False).AndReturn(output) |
685 self.mox.ReplayAll() | 687 self.mox.ReplayAll() |
686 | 688 |
687 self.assertEquals(True, | 689 self.assertEquals( |
688 presubmit.Main(['presubmit', '--root', | 690 True, |
689 self.fake_root_dir])) | 691 presubmit.Main(['--root', self.fake_root_dir, 'random_file.txt'])) |
692 | |
693 def testMainUnversionedFail(self): | |
694 # OptParser calls presubmit.os.path.exists and is a pain when mocked. | |
695 self.UnMock(presubmit.os.path, 'exists') | |
696 self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') | |
697 self.mox.StubOutWithMock(presubmit, 'ParseFiles') | |
698 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.svn') | |
699 ).AndReturn(False) | |
700 presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.git') | |
701 ).AndReturn(False) | |
702 presubmit.subprocess.call( | |
703 ['git', 'rev-parse', '--show-cdup'], | |
704 cwd=self.fake_root_dir, | |
705 stdout=presubmit.subprocess.PIPE).AndReturn(1) | |
706 self.mox.StubOutWithMock(presubmit.sys, 'stderr') | |
707 presubmit.sys.stderr.write( | |
708 'Usage: presubmit_unittest.py [options] <files...>\n') | |
709 presubmit.sys.stderr.write('\n') | |
710 presubmit.sys.stderr.write( | |
711 'presubmit_unittest.py: error: For unversioned directory, <files> is ' | |
712 'not optional.\n') | |
713 self.mox.ReplayAll() | |
714 | |
715 try: | |
716 presubmit.Main(['--root', self.fake_root_dir]) | |
717 self.fail() | |
718 except SystemExit, e: | |
719 self.assertEquals(2, e.code) | |
Dirk Pranke
2011/03/17 19:12:56
This is the sort of thing that makes mock testing
| |
690 | 720 |
691 | 721 |
692 class InputApiUnittest(PresubmitTestsBase): | 722 class InputApiUnittest(PresubmitTestsBase): |
693 """Tests presubmit.InputApi.""" | 723 """Tests presubmit.InputApi.""" |
694 def testMembersChanged(self): | 724 def testMembersChanged(self): |
695 self.mox.ReplayAll() | 725 self.mox.ReplayAll() |
696 members = [ | 726 members = [ |
697 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', | 727 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', |
698 'AffectedTextFiles', | 728 'AffectedTextFiles', |
699 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', | 729 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', |
(...skipping 1233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1933 | 1963 |
1934 def testCannedCheckOwners_TBR(self): | 1964 def testCannedCheckOwners_TBR(self): |
1935 self.OwnersTest(is_committing=True, tbr=True, | 1965 self.OwnersTest(is_committing=True, tbr=True, |
1936 approvers=set(), | 1966 approvers=set(), |
1937 uncovered_files=set(), | 1967 uncovered_files=set(), |
1938 expected_output='--tbr was specified, skipping OWNERS check\n') | 1968 expected_output='--tbr was specified, skipping OWNERS check\n') |
1939 | 1969 |
1940 if __name__ == '__main__': | 1970 if __name__ == '__main__': |
1941 import unittest | 1971 import unittest |
1942 unittest.main() | 1972 unittest.main() |
OLD | NEW |