Chromium Code Reviews

Side by Side Diff: tests/presubmit_unittest.py

Issue 6461011: Modify presubmit checks to work on changed lines only. (Closed)
Patch Set: test Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« presubmit_support.py ('K') | « presubmit_support.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 19 matching lines...)
30 return [output_api.PresubmitPromptWarning("??"), 30 return [output_api.PresubmitPromptWarning("??"),
31 output_api.PresubmitError("XX!!XX")] 31 output_api.PresubmitError("XX!!XX")]
32 else: 32 else:
33 return () 33 return ()
34 """ 34 """
35 presubmit_tryslave = """ 35 presubmit_tryslave = """
36 def GetPreferredTrySlaves(): 36 def GetPreferredTrySlaves():
37 return %s 37 return %s
38 """ 38 """
39 39
40 presubmit_diffs = """
41 --- /tmp/file1 2011-02-08 21:35:31.998479201 -0800
42 +++ /tmp/file2 2011-02-08 21:36:50.487240201 -0800
43 @@ -1,6 +1,5 @@
44 this is line number 0
45 this is line number 1
46 -this is line number 2 to be deleted
47 this is line number 3
48 this is line number 4
49 this is line number 5
50 @@ -8,7 +7,7 @@
51 this is line number 7
52 this is line number 8
53 this is line number 9
54 -this is line number 10 to be modified
55 +this is line number 10
56 this is line number 11
57 this is line number 12
58 this is line number 13
59 @@ -21,9 +20,8 @@
60 this is line number 20
61 this is line number 21
62 this is line number 22
63 -this is line number 23
64 -this is line number 24
65 -this is line number 25
66 +this is line number 23.1
67 +this is line number 25.1
68 this is line number 26
69 this is line number 27
70 this is line number 28
71 @@ -31,6 +29,7 @@
72 this is line number 30
73 this is line number 31
74 this is line number 32
75 +this is line number 32.1
76 this is line number 33
77 this is line number 34
78 this is line number 35
79 """
80
40 def setUp(self): 81 def setUp(self):
41 SuperMoxTestBase.setUp(self) 82 SuperMoxTestBase.setUp(self)
42 self.mox.StubOutWithMock(presubmit, 'random') 83 self.mox.StubOutWithMock(presubmit, 'random')
43 self.mox.StubOutWithMock(presubmit, 'warn') 84 self.mox.StubOutWithMock(presubmit, 'warn')
44 presubmit._ASKED_FOR_FEEDBACK = False 85 presubmit._ASKED_FOR_FEEDBACK = False
45 self.fake_root_dir = self.RootDir() 86 self.fake_root_dir = self.RootDir()
46 # Special mocks. 87 # Special mocks.
47 def MockAbsPath(f): 88 def MockAbsPath(f):
48 return f 89 return f
49 def MockChdir(f): 90 def MockChdir(f):
50 return None 91 return None
51 # SuperMoxTestBase already mock these but simplify our life. 92 # SuperMoxTestBase already mock these but simplify our life.
52 presubmit.os.path.abspath = MockAbsPath 93 presubmit.os.path.abspath = MockAbsPath
53 presubmit.os.getcwd = self.RootDir 94 presubmit.os.getcwd = self.RootDir
54 presubmit.os.chdir = MockChdir 95 presubmit.os.chdir = MockChdir
55 self.mox.StubOutWithMock(presubmit.scm.SVN, 'CaptureInfo') 96 self.mox.StubOutWithMock(presubmit.scm.SVN, 'CaptureInfo')
56 self.mox.StubOutWithMock(presubmit.scm.SVN, 'GetFileProperty') 97 self.mox.StubOutWithMock(presubmit.scm.SVN, 'GetFileProperty')
57 self.mox.StubOutWithMock(presubmit.gclient_utils, 'FileRead') 98 self.mox.StubOutWithMock(presubmit.gclient_utils, 'FileRead')
58 self.mox.StubOutWithMock(presubmit.gclient_utils, 'FileWrite') 99 self.mox.StubOutWithMock(presubmit.gclient_utils, 'FileWrite')
100 self.mox.StubOutWithMock(presubmit.scm.SVN, 'GenerateDiff')
59 101
60 102
61 class PresubmitUnittest(PresubmitTestsBase): 103 class PresubmitUnittest(PresubmitTestsBase):
62 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" 104 """General presubmit_support.py tests (excluding InputApi and OutputApi)."""
63 105
64 _INHERIT_SETTINGS = 'inherit-review-settings-ok' 106 _INHERIT_SETTINGS = 'inherit-review-settings-ok'
65 107
66 def testMembersChanged(self): 108 def testMembersChanged(self):
67 self.mox.ReplayAll() 109 self.mox.ReplayAll()
68 members = [ 110 members = [
(...skipping 121 matching lines...)
190 binary, 'svn:mime-type').AndReturn('application/octet-stream') 232 binary, 'svn:mime-type').AndReturn('application/octet-stream')
191 presubmit.scm.SVN.GetFileProperty( 233 presubmit.scm.SVN.GetFileProperty(
192 notfound, 'svn:mime-type').AndReturn('') 234 notfound, 'svn:mime-type').AndReturn('')
193 presubmit.scm.SVN.CaptureInfo(blat).AndReturn( 235 presubmit.scm.SVN.CaptureInfo(blat).AndReturn(
194 {'URL': 'svn:/foo/foo/blat.cc'}) 236 {'URL': 'svn:/foo/foo/blat.cc'})
195 presubmit.scm.SVN.CaptureInfo(binary).AndReturn( 237 presubmit.scm.SVN.CaptureInfo(binary).AndReturn(
196 {'URL': 'svn:/foo/binary.dll'}) 238 {'URL': 'svn:/foo/binary.dll'})
197 presubmit.scm.SVN.CaptureInfo(notfound).AndReturn({}) 239 presubmit.scm.SVN.CaptureInfo(notfound).AndReturn({})
198 presubmit.scm.SVN.CaptureInfo(flap).AndReturn( 240 presubmit.scm.SVN.CaptureInfo(flap).AndReturn(
199 {'URL': 'svn:/foo/boo/flap.h'}) 241 {'URL': 'svn:/foo/boo/flap.h'})
200 presubmit.gclient_utils.FileRead(blat, 'rU').AndReturn('boo!\nahh?') 242 presubmit.scm.SVN.GenerateDiff(blat).AndReturn(self.presubmit_diffs)
201 presubmit.gclient_utils.FileRead(notfound, 'rU').AndReturn('look!\nthere?') 243 presubmit.scm.SVN.GenerateDiff(notfound).AndReturn(self.presubmit_diffs)
244
202 self.mox.ReplayAll() 245 self.mox.ReplayAll()
203 246
204 change = presubmit.SvnChange('mychange', '\n'.join(description_lines), 247 change = presubmit.SvnChange('mychange', '\n'.join(description_lines),
205 self.fake_root_dir, files, 0, 0) 248 self.fake_root_dir, files, 0, 0)
206 self.failUnless(change.Name() == 'mychange') 249 self.failUnless(change.Name() == 'mychange')
207 self.failUnless(change.DescriptionText() == 250 self.failUnless(change.DescriptionText() ==
208 'Hello there\nthis is a change\nand some more regular text') 251 'Hello there\nthis is a change\nand some more regular text')
209 self.failUnless(change.FullDescriptionText() == 252 self.failUnless(change.FullDescriptionText() ==
210 '\n'.join(description_lines)) 253 '\n'.join(description_lines))
211 254
(...skipping 23 matching lines...)
235 expected_paths.append('') # one unknown file 278 expected_paths.append('') # one unknown file
236 self.assertEqual( 279 self.assertEqual(
237 len(filter(lambda x: x in expected_paths, server_paths)), 4) 280 len(filter(lambda x: x in expected_paths, server_paths)), 4)
238 281
239 files = [[x[0], presubmit.normpath(x[1])] for x in files] 282 files = [[x[0], presubmit.normpath(x[1])] for x in files]
240 283
241 rhs_lines = [] 284 rhs_lines = []
242 for line in change.RightHandSideLines(): 285 for line in change.RightHandSideLines():
243 rhs_lines.append(line) 286 rhs_lines.append(line)
244 self.assertEquals(rhs_lines[0][0].LocalPath(), files[0][1]) 287 self.assertEquals(rhs_lines[0][0].LocalPath(), files[0][1])
245 self.assertEquals(rhs_lines[0][1], 1) 288 self.assertEquals(rhs_lines[0][1], 10)
246 self.assertEquals(rhs_lines[0][2],'boo!') 289 self.assertEquals(rhs_lines[0][2],'this is line number 10')
247 290
248 self.assertEquals(rhs_lines[1][0].LocalPath(), files[0][1]) 291 self.assertEquals(rhs_lines[3][0].LocalPath(), files[0][1])
249 self.assertEquals(rhs_lines[1][1], 2) 292 self.assertEquals(rhs_lines[3][1], 32)
250 self.assertEquals(rhs_lines[1][2], 'ahh?') 293 self.assertEquals(rhs_lines[3][2], 'this is line number 32.1')
251 294
252 self.assertEquals(rhs_lines[2][0].LocalPath(), files[3][1]) 295 self.assertEquals(rhs_lines[5][0].LocalPath(), files[3][1])
253 self.assertEquals(rhs_lines[2][1], 1) 296 self.assertEquals(rhs_lines[5][1], 23)
254 self.assertEquals(rhs_lines[2][2], 'look!') 297 self.assertEquals(rhs_lines[5][2], 'this is line number 23.1')
255 298
256 self.assertEquals(rhs_lines[3][0].LocalPath(), files[3][1]) 299 self.assertEquals(rhs_lines[6][0].LocalPath(), files[3][1])
257 self.assertEquals(rhs_lines[3][1], 2) 300 self.assertEquals(rhs_lines[6][1], 24)
258 self.assertEquals(rhs_lines[3][2], 'there?') 301 self.assertEquals(rhs_lines[6][2], 'this is line number 25.1')
259 302
260 def testExecPresubmitScript(self): 303 def testExecPresubmitScript(self):
261 description_lines = ('Hello there', 304 description_lines = ('Hello there',
262 'this is a change', 305 'this is a change',
263 'STORY=http://tracker/123') 306 'STORY=http://tracker/123')
264 files = [ 307 files = [
265 ['A', 'foo\\blat.cc'], 308 ['A', 'foo\\blat.cc'],
266 ] 309 ]
267 fake_presubmit = presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py') 310 fake_presubmit = presubmit.os.path.join(self.fake_root_dir, 'PRESUBMIT.py')
268 self.mox.ReplayAll() 311 self.mox.ReplayAll()
(...skipping 432 matching lines...)
701 presubmit.scm.SVN.CaptureInfo(beingdeleted).AndReturn({}) 744 presubmit.scm.SVN.CaptureInfo(beingdeleted).AndReturn({})
702 presubmit.scm.SVN.CaptureInfo(notfound).AndReturn({}) 745 presubmit.scm.SVN.CaptureInfo(notfound).AndReturn({})
703 presubmit.scm.SVN.GetFileProperty(blat, 'svn:mime-type').AndReturn(None) 746 presubmit.scm.SVN.GetFileProperty(blat, 'svn:mime-type').AndReturn(None)
704 presubmit.scm.SVN.GetFileProperty(readme, 'svn:mime-type').AndReturn(None) 747 presubmit.scm.SVN.GetFileProperty(readme, 'svn:mime-type').AndReturn(None)
705 presubmit.scm.SVN.GetFileProperty(binary, 'svn:mime-type').AndReturn( 748 presubmit.scm.SVN.GetFileProperty(binary, 'svn:mime-type').AndReturn(
706 'application/octet-stream') 749 'application/octet-stream')
707 presubmit.scm.SVN.GetFileProperty(weird, 'svn:mime-type').AndReturn(None) 750 presubmit.scm.SVN.GetFileProperty(weird, 'svn:mime-type').AndReturn(None)
708 presubmit.scm.SVN.GetFileProperty(another, 'svn:mime-type').AndReturn(None) 751 presubmit.scm.SVN.GetFileProperty(another, 'svn:mime-type').AndReturn(None)
709 presubmit.scm.SVN.GetFileProperty(third_party, 'svn:mime-type' 752 presubmit.scm.SVN.GetFileProperty(third_party, 'svn:mime-type'
710 ).AndReturn(None) 753 ).AndReturn(None)
711 presubmit.gclient_utils.FileRead(blat, 'rU' 754 presubmit.scm.SVN.GenerateDiff(blat).AndReturn(self.presubmit_diffs)
712 ).AndReturn('whatever\ncookie') 755 presubmit.scm.SVN.GenerateDiff(another).AndReturn(self.presubmit_diffs)
713 presubmit.gclient_utils.FileRead(another, 'rU' 756
714 ).AndReturn('whatever\ncookie2')
715 self.mox.ReplayAll() 757 self.mox.ReplayAll()
716 758
717 change = presubmit.SvnChange('mychange', '\n'.join(description_lines), 759 change = presubmit.SvnChange('mychange', '\n'.join(description_lines),
718 self.fake_root_dir, files, 0, 0) 760 self.fake_root_dir, files, 0, 0)
719 input_api = presubmit.InputApi(change, 761 input_api = presubmit.InputApi(change,
720 join(self.fake_root_dir, 'foo', 762 join(self.fake_root_dir, 'foo',
721 'PRESUBMIT.py'), 763 'PRESUBMIT.py'),
722 False) 764 False)
723 # Doesn't filter much 765 # Doesn't filter much
724 got_files = input_api.AffectedFiles() 766 got_files = input_api.AffectedFiles()
725 self.assertEquals(len(got_files), 7) 767 self.assertEquals(len(got_files), 7)
726 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1])) 768 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1]))
727 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1])) 769 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1]))
728 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1])) 770 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1]))
729 self.assertEquals(got_files[3].LocalPath(), presubmit.normpath(files[3][1])) 771 self.assertEquals(got_files[3].LocalPath(), presubmit.normpath(files[3][1]))
730 self.assertEquals(got_files[4].LocalPath(), presubmit.normpath(files[4][1])) 772 self.assertEquals(got_files[4].LocalPath(), presubmit.normpath(files[4][1]))
731 self.assertEquals(got_files[5].LocalPath(), presubmit.normpath(files[5][1])) 773 self.assertEquals(got_files[5].LocalPath(), presubmit.normpath(files[5][1]))
732 self.assertEquals(got_files[6].LocalPath(), presubmit.normpath(files[6][1])) 774 self.assertEquals(got_files[6].LocalPath(), presubmit.normpath(files[6][1]))
733 # Ignores weird because of whitelist, third_party because of blacklist, 775 # Ignores weird because of whitelist, third_party because of blacklist,
734 # binary isn't a text file and beingdeleted doesn't exist. The rest is 776 # binary isn't a text file and beingdeleted doesn't exist. The rest is
735 # outside foo/. 777 # outside foo/.
736 rhs_lines = [x for x in input_api.RightHandSideLines(None)] 778 rhs_lines = [x for x in input_api.RightHandSideLines(None)]
737 self.assertEquals(len(rhs_lines), 4) 779 self.assertEquals(len(rhs_lines), 8)
738 self.assertEqual(rhs_lines[0][0].LocalPath(), 780 self.assertEqual(rhs_lines[0][0].LocalPath(),
739 presubmit.normpath(files[0][1])) 781 presubmit.normpath(files[0][1]))
740 self.assertEqual(rhs_lines[1][0].LocalPath(), 782 self.assertEqual(rhs_lines[3][0].LocalPath(),
741 presubmit.normpath(files[0][1])) 783 presubmit.normpath(files[0][1]))
742 self.assertEqual(rhs_lines[2][0].LocalPath(), 784 self.assertEqual(rhs_lines[4][0].LocalPath(),
743 presubmit.normpath(files[4][1])) 785 presubmit.normpath(files[4][1]))
744 self.assertEqual(rhs_lines[3][0].LocalPath(), 786 self.assertEqual(rhs_lines[7][0].LocalPath(),
745 presubmit.normpath(files[4][1])) 787 presubmit.normpath(files[4][1]))
746 788
747 def testDefaultWhiteListBlackListFilters(self): 789 def testDefaultWhiteListBlackListFilters(self):
748 def f(x): 790 def f(x):
749 return presubmit.AffectedFile(x, 'M') 791 return presubmit.AffectedFile(x, 'M')
750 files = [ 792 files = [
751 ( 793 (
752 [ 794 [
753 # To be tested. 795 # To be tested.
754 f('a/experimental/b'), 796 f('a/experimental/b'),
(...skipping 253 matching lines...)
1008 input_buf = StringIO.StringIO('\n') 1050 input_buf = StringIO.StringIO('\n')
1009 warning = presubmit.OutputApi.PresubmitPromptWarning('???') 1051 warning = presubmit.OutputApi.PresubmitPromptWarning('???')
1010 self.failIf(warning._Handle(output, input_buf)) 1052 self.failIf(warning._Handle(output, input_buf))
1011 self.failUnless(output.getvalue().count('???')) 1053 self.failUnless(output.getvalue().count('???'))
1012 1054
1013 1055
1014 class AffectedFileUnittest(PresubmitTestsBase): 1056 class AffectedFileUnittest(PresubmitTestsBase):
1015 def testMembersChanged(self): 1057 def testMembersChanged(self):
1016 self.mox.ReplayAll() 1058 self.mox.ReplayAll()
1017 members = [ 1059 members = [
1018 'AbsoluteLocalPath', 'Action', 'IsDirectory', 'IsTextFile', 'LocalPath', 1060 'AbsoluteLocalPath', 'Action', 'ChangedContents', 'IsDirectory',
1019 'NewContents', 'OldContents', 'OldFileTempPath', 'Property', 'ServerPath', 1061 'IsTextFile', 'LocalPath', 'NewContents', 'OldContents',
1062 'OldFileTempPath', 'Property', 'ServerPath',
1020 ] 1063 ]
1021 # If this test fails, you should add the relevant test. 1064 # If this test fails, you should add the relevant test.
1022 self.compareMembers(presubmit.AffectedFile('a', 'b'), members) 1065 self.compareMembers(presubmit.AffectedFile('a', 'b'), members)
1066 members.append('GenerateScmDiff')
1023 self.compareMembers(presubmit.SvnAffectedFile('a', 'b'), members) 1067 self.compareMembers(presubmit.SvnAffectedFile('a', 'b'), members)
1024 1068
1025 def testAffectedFile(self): 1069 def testAffectedFile(self):
1026 path = presubmit.os.path.join('foo', 'blat.cc') 1070 path = presubmit.os.path.join('foo', 'blat.cc')
1027 presubmit.os.path.exists(path).AndReturn(True) 1071 presubmit.os.path.exists(path).AndReturn(True)
1028 presubmit.os.path.isdir(path).AndReturn(False) 1072 presubmit.os.path.isdir(path).AndReturn(False)
1029 presubmit.gclient_utils.FileRead(path, 'rU').AndReturn('whatever\ncookie') 1073 presubmit.gclient_utils.FileRead(path, 'rU').AndReturn('whatever\ncookie')
1030 presubmit.scm.SVN.CaptureInfo(path).AndReturn( 1074 presubmit.scm.SVN.CaptureInfo(path).AndReturn(
1031 {'URL': 'svn:/foo/foo/blat.cc'}) 1075 {'URL': 'svn:/foo/foo/blat.cc'})
1032 self.mox.ReplayAll() 1076 self.mox.ReplayAll()
(...skipping 726 matching lines...)
1759 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( 1803 results = presubmit_canned_checks.CheckBuildbotPendingBuilds(
1760 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) 1804 input_api, presubmit.OutputApi, 'uurl', 2, ('foo'))
1761 self.assertEquals(len(results), 1) 1805 self.assertEquals(len(results), 1)
1762 self.assertEquals(results[0].__class__, 1806 self.assertEquals(results[0].__class__,
1763 presubmit.OutputApi.PresubmitNotifyResult) 1807 presubmit.OutputApi.PresubmitNotifyResult)
1764 1808
1765 1809
1766 if __name__ == '__main__': 1810 if __name__ == '__main__':
1767 import unittest 1811 import unittest
1768 unittest.main() 1812 unittest.main()
OLDNEW
« presubmit_support.py ('K') | « presubmit_support.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine