OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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: disable=E1101,E1103 | 8 # pylint: disable=E1101,E1103 |
9 | 9 |
10 import logging | 10 import logging |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 # SuperMoxTestBase already mock these but simplify our life. | 140 # SuperMoxTestBase already mock these but simplify our life. |
141 presubmit.os.path.abspath = MockAbsPath | 141 presubmit.os.path.abspath = MockAbsPath |
142 presubmit.os.getcwd = self.RootDir | 142 presubmit.os.getcwd = self.RootDir |
143 presubmit.os.chdir = MockChdir | 143 presubmit.os.chdir = MockChdir |
144 self.mox.StubOutWithMock(presubmit.scm, 'determine_scm') | 144 self.mox.StubOutWithMock(presubmit.scm, 'determine_scm') |
145 self.mox.StubOutWithMock(presubmit.scm.SVN, '_CaptureInfo') | 145 self.mox.StubOutWithMock(presubmit.scm.SVN, '_CaptureInfo') |
146 self.mox.StubOutWithMock(presubmit.scm.SVN, 'GetFileProperty') | 146 self.mox.StubOutWithMock(presubmit.scm.SVN, 'GetFileProperty') |
147 self.mox.StubOutWithMock(presubmit.gclient_utils, 'FileRead') | 147 self.mox.StubOutWithMock(presubmit.gclient_utils, 'FileRead') |
148 self.mox.StubOutWithMock(presubmit.gclient_utils, 'FileWrite') | 148 self.mox.StubOutWithMock(presubmit.gclient_utils, 'FileWrite') |
149 self.mox.StubOutWithMock(presubmit.scm.SVN, 'GenerateDiff') | 149 self.mox.StubOutWithMock(presubmit.scm.SVN, 'GenerateDiff') |
150 self.mox.StubOutWithMock(presubmit.scm.GIT, 'GenerateDiff') | |
150 | 151 |
151 | 152 |
152 class PresubmitUnittest(PresubmitTestsBase): | 153 class PresubmitUnittest(PresubmitTestsBase): |
153 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" | 154 """General presubmit_support.py tests (excluding InputApi and OutputApi).""" |
154 | 155 |
155 _INHERIT_SETTINGS = 'inherit-review-settings-ok' | 156 _INHERIT_SETTINGS = 'inherit-review-settings-ok' |
156 | 157 |
157 def testMembersChanged(self): | 158 def testMembersChanged(self): |
158 self.mox.ReplayAll() | 159 self.mox.ReplayAll() |
159 members = [ | 160 members = [ |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
378 self.assertEquals(rhs_lines[8][2], 'this is line number 23.1') | 379 self.assertEquals(rhs_lines[8][2], 'this is line number 23.1') |
379 | 380 |
380 self.assertEquals(rhs_lines[12][0].LocalPath(), files[3][1]) | 381 self.assertEquals(rhs_lines[12][0].LocalPath(), files[3][1]) |
381 self.assertEquals(rhs_lines[12][1], 46) | 382 self.assertEquals(rhs_lines[12][1], 46) |
382 self.assertEquals(rhs_lines[12][2], '') | 383 self.assertEquals(rhs_lines[12][2], '') |
383 | 384 |
384 self.assertEquals(rhs_lines[13][0].LocalPath(), files[3][1]) | 385 self.assertEquals(rhs_lines[13][0].LocalPath(), files[3][1]) |
385 self.assertEquals(rhs_lines[13][1], 49) | 386 self.assertEquals(rhs_lines[13][1], 49) |
386 self.assertEquals(rhs_lines[13][2], 'this is line number 48.1') | 387 self.assertEquals(rhs_lines[13][2], 'this is line number 48.1') |
387 | 388 |
389 def testGitChange(self): | |
390 description_lines = ('Hello there', | |
391 'this is a change', | |
392 'BUG=123', | |
393 ' STORY =http://foo/ \t', | |
394 'and some more regular text \t') | |
395 unified_diff = ['diff --git binary_a.png binary_a.png', | |
396 'new file mode 100644', | |
397 'index 0000000..6fbdd6d', | |
398 'Binary files /dev/null and binary_a.png differ', | |
399 'diff --git binary_d.png binary_d.png', | |
400 'deleted file mode 100644', | |
401 'index 6fbdd6d..0000000', | |
402 'Binary files binary_d.png and /dev/null differ', | |
403 'diff --git binary_m.png binary_m.png', | |
404 'index 6fbdd6d..be3d5d8 100644', | |
405 'Binary files binary_m.png and binary_m.png differ', | |
iannucci
2013/06/07 02:17:36
May also want to test support the git diff --binar
ncarter (slow)
2013/06/07 19:51:15
Done, added a new file with a binary diff.
| |
406 'diff --git boo/blat.cc boo/blat.cc', | |
407 'new file mode 100644', | |
408 'index 0000000..37d18ad', | |
409 '--- boo/blat.cc', | |
410 '+++ boo/blat.cc', | |
411 '@@ -0,0 +1,5 @@', | |
412 '+This is some text', | |
413 '+which lacks a copyright warning', | |
414 '+but it is nonetheless interesting', | |
415 '+and worthy of your attention.', | |
416 '+Its freshness factor is through the roof.', | |
417 'diff --git floo/delburt.cc floo/delburt.cc', | |
418 'deleted file mode 100644', | |
419 'index e06377a..0000000', | |
420 '--- floo/delburt.cc', | |
421 '+++ /dev/null', | |
422 '@@ -1,14 +0,0 @@', | |
423 '-This text used to be here', | |
424 '-but someone, probably you,', | |
425 '-having consumed the text', | |
426 '- (absorbed its meaning)', | |
427 '-decided that it should be made to not exist', | |
428 '-that others would not read it.', | |
429 '- (What happened here?', | |
430 '-was the author incompetent?', | |
431 '-or is the world today so different from the world', | |
432 '- the author foresaw', | |
433 '-and past imaginination', | |
434 '- amounts to rubble, insignificant,', | |
435 '-something to be tripped over', | |
436 '-and frustrated by)', | |
437 'diff --git foo/TestExpectations foo/TestExpectations', | |
438 'index c6e12ab..d1c5f23 100644', | |
439 '--- foo/TestExpectations', | |
440 '+++ foo/TestExpectations', | |
441 '@@ -1,12 +1,24 @@', | |
442 '-Stranger, behold:', | |
443 '+Strange to behold:', | |
444 ' This is a text', | |
445 ' Its contents existed before.', | |
446 '', | |
447 '-It is written:', | |
448 '+Weasel words suggest:', | |
449 ' its contents shall exist after', | |
450 ' and its contents', | |
451 ' with the progress of time', | |
452 ' will evolve,', | |
453 '- snaillike,', | |
454 '+ erratically,', | |
455 ' into still different texts', | |
456 '-from this.', | |
457 '\ No newline at end of file', | |
458 '+from this.', | |
459 '+', | |
460 '+For the most part,', | |
461 '+I really think unified diffs', | |
462 '+are elegant: the way you can type', | |
463 '+diff --git inside/a/text inside/a/text', | |
464 '+or something silly like', | |
465 '+@@ -278,6 +278,10 @@', | |
466 '+and have this not be interpreted', | |
467 '+as the start of a new file', | |
468 '+or anything messed up like that,', | |
469 '+because you parsed the header', | |
470 '+correctly.', | |
471 '\ No newline at end of file', | |
472 ''] | |
473 files = [('A ', 'binary_a.png'), | |
474 ('D ', 'binary_d.png'), | |
475 ('M ', 'binary_m.png'), | |
476 ('A ', 'boo/blat.cc'), | |
477 ('D ', 'floo/delburt.cc'), | |
478 ('M ', 'foo/TestExpectations')] | |
479 | |
480 for op, path in files: | |
481 full_path = presubmit.os.path.join(self.fake_root_dir, *path.split('/')) | |
482 if op.startswith('D'): | |
483 os.path.exists(full_path).AndReturn(False) | |
484 else: | |
485 os.path.exists(full_path).AndReturn(False) | |
486 os.path.isfile(full_path).AndReturn(True) | |
487 | |
488 presubmit.scm.GIT.GenerateDiff(self.fake_root_dir, files=[], full_move=True | |
489 ).AndReturn('\n'.join(unified_diff)) | |
490 | |
491 self.mox.ReplayAll() | |
492 | |
493 change = presubmit.GitChange( | |
494 'mychange', | |
495 '\n'.join(description_lines), | |
496 self.fake_root_dir, | |
497 files, | |
498 0, | |
499 0, | |
500 None) | |
501 self.failUnless(change.Name() == 'mychange') | |
502 self.failUnless(change.DescriptionText() == | |
503 'Hello there\nthis is a change\nand some more regular text') | |
504 self.failUnless(change.FullDescriptionText() == | |
505 '\n'.join(description_lines)) | |
506 | |
507 self.failUnless(change.BUG == '123') | |
508 self.failUnless(change.STORY == 'http://foo/') | |
509 self.failUnless(change.BLEH == None) | |
510 | |
511 self.failUnless(len(change.AffectedFiles()) == 6) | |
512 self.failUnless(len(change.AffectedFiles(include_dirs=True)) == 6) | |
513 self.failUnless(len(change.AffectedFiles(include_deletes=False)) == 4) | |
514 self.failUnless(len(change.AffectedFiles(include_dirs=True, | |
515 include_deletes=False)) == 4) | |
516 | |
517 # Note that on git, there's no distinction between binary files and text | |
518 # files; everything that's not a delete is a text file. | |
519 affected_text_files = change.AffectedTextFiles() | |
520 self.failUnless(len(affected_text_files) == 4) | |
521 | |
522 local_paths = change.LocalPaths() | |
523 expected_paths = [os.path.normpath(f) for op, f in files] | |
524 self.assertEqual(local_paths, expected_paths) | |
525 | |
526 try: | |
527 _ = change.ServerPaths() | |
528 self.fail("ServerPaths implemented.") | |
529 except NotImplementedError: | |
530 pass | |
531 | |
532 actual_rhs_lines = [] | |
533 for f, linenum, line in change.RightHandSideLines(): | |
534 actual_rhs_lines.append((f.LocalPath(), linenum, line)) | |
535 | |
536 f_blat = os.path.normpath('boo/blat.cc') | |
537 f_test_expectations = os.path.normpath('foo/TestExpectations') | |
538 expected_rhs_lines = [ | |
539 (f_blat, 1, 'This is some text'), | |
540 (f_blat, 2, 'which lacks a copyright warning'), | |
541 (f_blat, 3, 'but it is nonetheless interesting'), | |
542 (f_blat, 4, 'and worthy of your attention.'), | |
543 (f_blat, 5, 'Its freshness factor is through the roof.'), | |
544 (f_test_expectations, 1, 'Strange to behold:'), | |
545 (f_test_expectations, 5, 'Weasel words suggest:'), | |
546 (f_test_expectations, 10, ' erratically,'), | |
547 (f_test_expectations, 13, 'from this.'), | |
548 (f_test_expectations, 14, ''), | |
549 (f_test_expectations, 15, 'For the most part,'), | |
550 (f_test_expectations, 16, 'I really think unified diffs'), | |
551 (f_test_expectations, 17, 'are elegant: the way you can type'), | |
552 (f_test_expectations, 18, 'diff --git inside/a/text inside/a/text'), | |
553 (f_test_expectations, 19, 'or something silly like'), | |
554 (f_test_expectations, 20, '@@ -278,6 +278,10 @@'), | |
555 (f_test_expectations, 21, 'and have this not be interpreted'), | |
556 (f_test_expectations, 22, 'as the start of a new file'), | |
557 (f_test_expectations, 23, 'or anything messed up like that,'), | |
558 (f_test_expectations, 24, 'because you parsed the header'), | |
559 (f_test_expectations, 25, 'correctly.')] | |
iannucci
2013/06/07 02:17:36
:)
ncarter (slow)
2013/06/07 19:51:15
Done.
| |
560 | |
561 self.assertEquals(expected_rhs_lines, actual_rhs_lines) | |
562 | |
388 def testInvalidChange(self): | 563 def testInvalidChange(self): |
389 try: | 564 try: |
390 presubmit.SvnChange( | 565 presubmit.SvnChange( |
391 'mychange', | 566 'mychange', |
392 'description', | 567 'description', |
393 self.fake_root_dir, | 568 self.fake_root_dir, |
394 ['foo/blat.cc', 'bar'], | 569 ['foo/blat.cc', 'bar'], |
395 0, | 570 0, |
396 0, | 571 0, |
397 None) | 572 None) |
(...skipping 846 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1244 self.mox.ReplayAll() | 1419 self.mox.ReplayAll() |
1245 | 1420 |
1246 change = presubmit.Change( | 1421 change = presubmit.Change( |
1247 'foo', 'foo', self.fake_root_dir, [('M', 'AA')], 0, 0, None) | 1422 'foo', 'foo', self.fake_root_dir, [('M', 'AA')], 0, 0, None) |
1248 input_api = presubmit.InputApi( | 1423 input_api = presubmit.InputApi( |
1249 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, | 1424 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
1250 None, False) | 1425 None, False) |
1251 input_api.ReadFile(path, 'x') | 1426 input_api.ReadFile(path, 'x') |
1252 | 1427 |
1253 def testReadFileAffectedFileDenied(self): | 1428 def testReadFileAffectedFileDenied(self): |
1254 fileobj = presubmit.AffectedFile('boo', 'M', 'Unrelated') | 1429 fileobj = presubmit.AffectedFile('boo', 'M', 'Unrelated', |
1430 diff_cache=mox.IsA(presubmit._DiffCache)) | |
1255 self.mox.ReplayAll() | 1431 self.mox.ReplayAll() |
1256 | 1432 |
1257 change = presubmit.Change( | 1433 change = presubmit.Change( |
1258 'foo', 'foo', self.fake_root_dir, [('M', 'AA')], 0, 0, None) | 1434 'foo', 'foo', self.fake_root_dir, [('M', 'AA')], 0, 0, None) |
1259 input_api = presubmit.InputApi( | 1435 input_api = presubmit.InputApi( |
1260 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, | 1436 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
1261 None, False) | 1437 None, False) |
1262 self.assertRaises(IOError, input_api.ReadFile, fileobj, 'x') | 1438 self.assertRaises(IOError, input_api.ReadFile, fileobj, 'x') |
1263 | 1439 |
1264 def testReadFileAffectedFileAccepted(self): | 1440 def testReadFileAffectedFileAccepted(self): |
1265 fileobj = presubmit.AffectedFile('AA/boo', 'M', self.fake_root_dir) | 1441 fileobj = presubmit.AffectedFile('AA/boo', 'M', self.fake_root_dir, |
1442 diff_cache=mox.IsA(presubmit._DiffCache)) | |
1266 presubmit.gclient_utils.FileRead(fileobj.AbsoluteLocalPath(), 'x' | 1443 presubmit.gclient_utils.FileRead(fileobj.AbsoluteLocalPath(), 'x' |
1267 ).AndReturn(None) | 1444 ).AndReturn(None) |
1268 self.mox.ReplayAll() | 1445 self.mox.ReplayAll() |
1269 | 1446 |
1270 change = presubmit.Change( | 1447 change = presubmit.Change( |
1271 'foo', 'foo', self.fake_root_dir, [('M', 'AA')], 0, 0, None) | 1448 'foo', 'foo', self.fake_root_dir, [('M', 'AA')], 0, 0, None) |
1272 input_api = presubmit.InputApi( | 1449 input_api = presubmit.InputApi( |
1273 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, | 1450 change, presubmit.os.path.join(self.fake_root_dir, '/p'), False, |
1274 None, False) | 1451 None, False) |
1275 input_api.ReadFile(fileobj, 'x') | 1452 input_api.ReadFile(fileobj, 'x') |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1351 self.failUnless(output.should_continue()) | 1528 self.failUnless(output.should_continue()) |
1352 self.failUnless(output.getvalue().count('???')) | 1529 self.failUnless(output.getvalue().count('???')) |
1353 | 1530 |
1354 output_api = presubmit.OutputApi(True) | 1531 output_api = presubmit.OutputApi(True) |
1355 output = presubmit.PresubmitOutput(input_stream=StringIO.StringIO('\n')) | 1532 output = presubmit.PresubmitOutput(input_stream=StringIO.StringIO('\n')) |
1356 output_api.PresubmitPromptOrNotify('???').handle(output) | 1533 output_api.PresubmitPromptOrNotify('???').handle(output) |
1357 output.prompt_yes_no('prompt: ') | 1534 output.prompt_yes_no('prompt: ') |
1358 self.failIf(output.should_continue()) | 1535 self.failIf(output.should_continue()) |
1359 self.failUnless(output.getvalue().count('???')) | 1536 self.failUnless(output.getvalue().count('???')) |
1360 | 1537 |
1538 | |
1361 class AffectedFileUnittest(PresubmitTestsBase): | 1539 class AffectedFileUnittest(PresubmitTestsBase): |
1362 def testMembersChanged(self): | 1540 def testMembersChanged(self): |
1363 self.mox.ReplayAll() | 1541 self.mox.ReplayAll() |
1364 members = [ | 1542 members = [ |
1365 'AbsoluteLocalPath', 'Action', 'ChangedContents', 'GenerateScmDiff', | 1543 'AbsoluteLocalPath', 'Action', 'ChangedContents', 'DIFF_CACHE', |
1366 'IsDirectory', 'IsTextFile', 'LocalPath', 'NewContents', 'OldContents', | 1544 'GenerateScmDiff', 'IsDirectory', 'IsTextFile', 'LocalPath', |
1367 'OldFileTempPath', 'Property', 'ServerPath', | 1545 'NewContents', 'Property', 'ServerPath', |
1368 ] | 1546 ] |
1369 # If this test fails, you should add the relevant test. | 1547 # If this test fails, you should add the relevant test. |
1370 self.compareMembers( | 1548 self.compareMembers( |
1371 presubmit.AffectedFile('a', 'b', self.fake_root_dir), members) | 1549 presubmit.AffectedFile('a', 'b', self.fake_root_dir), members) |
1372 self.compareMembers( | 1550 self.compareMembers( |
1373 presubmit.SvnAffectedFile('a', 'b', self.fake_root_dir), members) | 1551 presubmit.SvnAffectedFile('a', 'b', self.fake_root_dir), members) |
1552 self.compareMembers( | |
1553 presubmit.GitAffectedFile('a', 'b', self.fake_root_dir), members) | |
1374 | 1554 |
1375 def testAffectedFile(self): | 1555 def testAffectedFile(self): |
1376 path = presubmit.os.path.join('foo', 'blat.cc') | 1556 path = presubmit.os.path.join('foo', 'blat.cc') |
1377 f_path = presubmit.os.path.join(self.fake_root_dir, path) | 1557 f_path = presubmit.os.path.join(self.fake_root_dir, path) |
1378 presubmit.os.path.exists(f_path).AndReturn(True) | 1558 presubmit.os.path.exists(f_path).AndReturn(True) |
1379 presubmit.os.path.isdir(f_path).AndReturn(False) | 1559 presubmit.os.path.isdir(f_path).AndReturn(False) |
1380 presubmit.gclient_utils.FileRead(f_path, 'rU').AndReturn('whatever\ncookie') | 1560 presubmit.gclient_utils.FileRead(f_path, 'rU').AndReturn('whatever\ncookie') |
1381 presubmit.scm.SVN._CaptureInfo([path], self.fake_root_dir).AndReturn( | 1561 presubmit.scm.SVN._CaptureInfo([path], self.fake_root_dir).AndReturn( |
1382 {'URL': 'svn:/foo/foo/blat.cc'}) | 1562 {'URL': 'svn:/foo/foo/blat.cc'}) |
1383 self.mox.ReplayAll() | 1563 self.mox.ReplayAll() |
(...skipping 1147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2531 owners_check=False) | 2711 owners_check=False) |
2532 self.assertEqual(1, len(results)) | 2712 self.assertEqual(1, len(results)) |
2533 self.assertEqual( | 2713 self.assertEqual( |
2534 'Found line ending with white spaces in:', results[0]._message) | 2714 'Found line ending with white spaces in:', results[0]._message) |
2535 self.checkstdout('') | 2715 self.checkstdout('') |
2536 | 2716 |
2537 | 2717 |
2538 if __name__ == '__main__': | 2718 if __name__ == '__main__': |
2539 import unittest | 2719 import unittest |
2540 unittest.main() | 2720 unittest.main() |
OLD | NEW |