Chromium Code Reviews| 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 19 matching lines...) Expand all Loading... | |
| 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 | |
|
M-A Ruel
2011/02/09 18:30:26
Add a
+
on a single line to verify my concern abou
vb
2011/02/09 18:54:11
Done.
| |
| 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...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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...) Expand all Loading... | |
| 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 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 704 presubmit.scm.SVN.CaptureInfo(beingdeleted).AndReturn({}) | 747 presubmit.scm.SVN.CaptureInfo(beingdeleted).AndReturn({}) |
| 705 presubmit.scm.SVN.CaptureInfo(notfound).AndReturn({}) | 748 presubmit.scm.SVN.CaptureInfo(notfound).AndReturn({}) |
| 706 presubmit.scm.SVN.GetFileProperty(blat, 'svn:mime-type').AndReturn(None) | 749 presubmit.scm.SVN.GetFileProperty(blat, 'svn:mime-type').AndReturn(None) |
| 707 presubmit.scm.SVN.GetFileProperty(readme, 'svn:mime-type').AndReturn(None) | 750 presubmit.scm.SVN.GetFileProperty(readme, 'svn:mime-type').AndReturn(None) |
| 708 presubmit.scm.SVN.GetFileProperty(binary, 'svn:mime-type').AndReturn( | 751 presubmit.scm.SVN.GetFileProperty(binary, 'svn:mime-type').AndReturn( |
| 709 'application/octet-stream') | 752 'application/octet-stream') |
| 710 presubmit.scm.SVN.GetFileProperty(weird, 'svn:mime-type').AndReturn(None) | 753 presubmit.scm.SVN.GetFileProperty(weird, 'svn:mime-type').AndReturn(None) |
| 711 presubmit.scm.SVN.GetFileProperty(another, 'svn:mime-type').AndReturn(None) | 754 presubmit.scm.SVN.GetFileProperty(another, 'svn:mime-type').AndReturn(None) |
| 712 presubmit.scm.SVN.GetFileProperty(third_party, 'svn:mime-type' | 755 presubmit.scm.SVN.GetFileProperty(third_party, 'svn:mime-type' |
| 713 ).AndReturn(None) | 756 ).AndReturn(None) |
| 714 presubmit.gclient_utils.FileRead(blat, 'rU' | 757 presubmit.scm.SVN.GenerateDiff(blat).AndReturn(self.presubmit_diffs) |
| 715 ).AndReturn('whatever\ncookie') | 758 presubmit.scm.SVN.GenerateDiff(another).AndReturn(self.presubmit_diffs) |
| 716 presubmit.gclient_utils.FileRead(another, 'rU' | 759 |
| 717 ).AndReturn('whatever\ncookie2') | |
| 718 self.mox.ReplayAll() | 760 self.mox.ReplayAll() |
| 719 | 761 |
| 720 change = presubmit.SvnChange('mychange', '\n'.join(description_lines), | 762 change = presubmit.SvnChange('mychange', '\n'.join(description_lines), |
| 721 self.fake_root_dir, files, 0, 0) | 763 self.fake_root_dir, files, 0, 0) |
| 722 input_api = presubmit.InputApi(change, | 764 input_api = presubmit.InputApi(change, |
| 723 join(self.fake_root_dir, 'foo', | 765 join(self.fake_root_dir, 'foo', |
| 724 'PRESUBMIT.py'), | 766 'PRESUBMIT.py'), |
| 725 False) | 767 False) |
| 726 # Doesn't filter much | 768 # Doesn't filter much |
| 727 got_files = input_api.AffectedFiles() | 769 got_files = input_api.AffectedFiles() |
| 728 self.assertEquals(len(got_files), 7) | 770 self.assertEquals(len(got_files), 7) |
| 729 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1])) | 771 self.assertEquals(got_files[0].LocalPath(), presubmit.normpath(files[0][1])) |
| 730 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1])) | 772 self.assertEquals(got_files[1].LocalPath(), presubmit.normpath(files[1][1])) |
| 731 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1])) | 773 self.assertEquals(got_files[2].LocalPath(), presubmit.normpath(files[2][1])) |
| 732 self.assertEquals(got_files[3].LocalPath(), presubmit.normpath(files[3][1])) | 774 self.assertEquals(got_files[3].LocalPath(), presubmit.normpath(files[3][1])) |
| 733 self.assertEquals(got_files[4].LocalPath(), presubmit.normpath(files[4][1])) | 775 self.assertEquals(got_files[4].LocalPath(), presubmit.normpath(files[4][1])) |
| 734 self.assertEquals(got_files[5].LocalPath(), presubmit.normpath(files[5][1])) | 776 self.assertEquals(got_files[5].LocalPath(), presubmit.normpath(files[5][1])) |
| 735 self.assertEquals(got_files[6].LocalPath(), presubmit.normpath(files[6][1])) | 777 self.assertEquals(got_files[6].LocalPath(), presubmit.normpath(files[6][1])) |
| 736 # Ignores weird because of whitelist, third_party because of blacklist, | 778 # Ignores weird because of whitelist, third_party because of blacklist, |
| 737 # binary isn't a text file and beingdeleted doesn't exist. The rest is | 779 # binary isn't a text file and beingdeleted doesn't exist. The rest is |
| 738 # outside foo/. | 780 # outside foo/. |
| 739 rhs_lines = [x for x in input_api.RightHandSideLines(None)] | 781 rhs_lines = [x for x in input_api.RightHandSideLines(None)] |
| 740 self.assertEquals(len(rhs_lines), 4) | 782 self.assertEquals(len(rhs_lines), 8) |
| 741 self.assertEqual(rhs_lines[0][0].LocalPath(), | 783 self.assertEqual(rhs_lines[0][0].LocalPath(), |
| 742 presubmit.normpath(files[0][1])) | 784 presubmit.normpath(files[0][1])) |
| 743 self.assertEqual(rhs_lines[1][0].LocalPath(), | 785 self.assertEqual(rhs_lines[3][0].LocalPath(), |
| 744 presubmit.normpath(files[0][1])) | 786 presubmit.normpath(files[0][1])) |
| 745 self.assertEqual(rhs_lines[2][0].LocalPath(), | 787 self.assertEqual(rhs_lines[4][0].LocalPath(), |
| 746 presubmit.normpath(files[4][1])) | 788 presubmit.normpath(files[4][1])) |
| 747 self.assertEqual(rhs_lines[3][0].LocalPath(), | 789 self.assertEqual(rhs_lines[7][0].LocalPath(), |
| 748 presubmit.normpath(files[4][1])) | 790 presubmit.normpath(files[4][1])) |
| 749 | 791 |
| 750 def testDefaultWhiteListBlackListFilters(self): | 792 def testDefaultWhiteListBlackListFilters(self): |
| 751 def f(x): | 793 def f(x): |
| 752 return presubmit.AffectedFile(x, 'M') | 794 return presubmit.AffectedFile(x, 'M') |
| 753 files = [ | 795 files = [ |
| 754 ( | 796 ( |
| 755 [ | 797 [ |
| 756 # To be tested. | 798 # To be tested. |
| 757 f('a/experimental/b'), | 799 f('a/experimental/b'), |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1011 input_buf = StringIO.StringIO('\n') | 1053 input_buf = StringIO.StringIO('\n') |
| 1012 warning = presubmit.OutputApi.PresubmitPromptWarning('???') | 1054 warning = presubmit.OutputApi.PresubmitPromptWarning('???') |
| 1013 self.failIf(warning._Handle(output, input_buf)) | 1055 self.failIf(warning._Handle(output, input_buf)) |
| 1014 self.failUnless(output.getvalue().count('???')) | 1056 self.failUnless(output.getvalue().count('???')) |
| 1015 | 1057 |
| 1016 | 1058 |
| 1017 class AffectedFileUnittest(PresubmitTestsBase): | 1059 class AffectedFileUnittest(PresubmitTestsBase): |
| 1018 def testMembersChanged(self): | 1060 def testMembersChanged(self): |
| 1019 self.mox.ReplayAll() | 1061 self.mox.ReplayAll() |
| 1020 members = [ | 1062 members = [ |
| 1021 'AbsoluteLocalPath', 'Action', 'IsDirectory', 'IsTextFile', 'LocalPath', | 1063 'AbsoluteLocalPath', 'Action', 'ChangedContents', 'IsDirectory', |
| 1022 'NewContents', 'OldContents', 'OldFileTempPath', 'Property', 'ServerPath', | 1064 'IsTextFile', 'LocalPath', 'NewContents', 'OldContents', |
| 1065 'OldFileTempPath', 'Property', 'ServerPath', | |
| 1023 ] | 1066 ] |
| 1024 # If this test fails, you should add the relevant test. | 1067 # If this test fails, you should add the relevant test. |
| 1025 self.compareMembers(presubmit.AffectedFile('a', 'b'), members) | 1068 self.compareMembers(presubmit.AffectedFile('a', 'b'), members) |
| 1069 members.append('GenerateScmDiff') | |
| 1026 self.compareMembers(presubmit.SvnAffectedFile('a', 'b'), members) | 1070 self.compareMembers(presubmit.SvnAffectedFile('a', 'b'), members) |
| 1027 | 1071 |
| 1028 def testAffectedFile(self): | 1072 def testAffectedFile(self): |
| 1029 path = presubmit.os.path.join('foo', 'blat.cc') | 1073 path = presubmit.os.path.join('foo', 'blat.cc') |
| 1030 presubmit.os.path.exists(path).AndReturn(True) | 1074 presubmit.os.path.exists(path).AndReturn(True) |
| 1031 presubmit.os.path.isdir(path).AndReturn(False) | 1075 presubmit.os.path.isdir(path).AndReturn(False) |
| 1032 presubmit.gclient_utils.FileRead(path, 'rU').AndReturn('whatever\ncookie') | 1076 presubmit.gclient_utils.FileRead(path, 'rU').AndReturn('whatever\ncookie') |
| 1033 presubmit.scm.SVN.CaptureInfo(path).AndReturn( | 1077 presubmit.scm.SVN.CaptureInfo(path).AndReturn( |
| 1034 {'URL': 'svn:/foo/foo/blat.cc'}) | 1078 {'URL': 'svn:/foo/foo/blat.cc'}) |
| 1035 self.mox.ReplayAll() | 1079 self.mox.ReplayAll() |
| (...skipping 726 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1762 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( | 1806 results = presubmit_canned_checks.CheckBuildbotPendingBuilds( |
| 1763 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) | 1807 input_api, presubmit.OutputApi, 'uurl', 2, ('foo')) |
| 1764 self.assertEquals(len(results), 1) | 1808 self.assertEquals(len(results), 1) |
| 1765 self.assertEquals(results[0].__class__, | 1809 self.assertEquals(results[0].__class__, |
| 1766 presubmit.OutputApi.PresubmitNotifyResult) | 1810 presubmit.OutputApi.PresubmitNotifyResult) |
| 1767 | 1811 |
| 1768 | 1812 |
| 1769 if __name__ == '__main__': | 1813 if __name__ == '__main__': |
| 1770 import unittest | 1814 import unittest |
| 1771 unittest.main() | 1815 unittest.main() |
| OLD | NEW |