OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 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,R0201,W0212,W0403 | 9 # pylint: disable=E1101,E1103,R0201,W0212,W0403 |
10 | 10 |
(...skipping 1356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1367 input_api2 = self.MockInputApi(change2, committing) | 1367 input_api2 = self.MockInputApi(change2, committing) |
1368 self.mox.ReplayAll() | 1368 self.mox.ReplayAll() |
1369 | 1369 |
1370 results1 = check(input_api1, presubmit.OutputApi) | 1370 results1 = check(input_api1, presubmit.OutputApi) |
1371 self.assertEquals(results1, []) | 1371 self.assertEquals(results1, []) |
1372 results2 = check(input_api2, presubmit.OutputApi) | 1372 results2 = check(input_api2, presubmit.OutputApi) |
1373 self.assertEquals(len(results2), 1) | 1373 self.assertEquals(len(results2), 1) |
1374 self.assertEquals(results2[0].__class__, error_type) | 1374 self.assertEquals(results2[0].__class__, error_type) |
1375 | 1375 |
1376 def ContentTest(self, check, content1, content2, error_type): | 1376 def ContentTest(self, check, content1, content2, error_type): |
1377 """Runs a test of a content-checking rule. | |
1378 | |
1379 Args: | |
1380 check: the check to run. | |
1381 content1: content which is expected to pass the check. | |
1382 content2: content which is expected to fail the check. | |
1383 error_type: the type of the error expected for content2. | |
1384 """ | |
1385 change1 = presubmit.Change( | 1377 change1 = presubmit.Change( |
1386 'foo1', 'foo1\n', self.fake_root_dir, None, 0, 0, None) | 1378 'foo1', 'foo1\n', self.fake_root_dir, None, 0, 0, None) |
1387 input_api1 = self.MockInputApi(change1, False) | 1379 input_api1 = self.MockInputApi(change1, False) |
1388 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1380 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) |
1389 input_api1.AffectedFiles(mox.IgnoreArg()).AndReturn([affected_file]) | 1381 affected_file.LocalPath().AndReturn('foo.cc') |
1390 affected_file.NewContents().AndReturn([ | 1382 # Format is (file, line number, line content) |
1391 'ahoy', | 1383 output1 = [ |
1392 'yo' + content1, | 1384 (affected_file, 42, 'yo, ' + content1), |
1393 'hay', | 1385 (affected_file, 43, 'yer'), |
1394 'yer', | 1386 (affected_file, 23, 'ya'), |
1395 'ya']) | 1387 ] |
1396 | 1388 input_api1.RightHandSideLines(mox.IgnoreArg()).AndReturn(output1) |
1397 change2 = presubmit.Change( | 1389 change2 = presubmit.Change( |
1398 'foo2', 'foo2\n', self.fake_root_dir, None, 0, 0, None) | 1390 'foo2', 'foo2\n', self.fake_root_dir, None, 0, 0, None) |
1399 input_api2 = self.MockInputApi(change2, False) | 1391 input_api2 = self.MockInputApi(change2, False) |
1400 | 1392 output2 = [ |
1401 input_api2.AffectedFiles(mox.IgnoreArg()).AndReturn([affected_file]) | 1393 (affected_file, 42, 'yo, ' + content2), |
1402 affected_file.NewContents().AndReturn([ | 1394 (affected_file, 43, 'yer'), |
1403 'ahoy', | 1395 (affected_file, 23, 'ya'), |
1404 'yo' + content2, | 1396 ] |
1405 'hay', | 1397 input_api2.RightHandSideLines(mox.IgnoreArg()).AndReturn(output2) |
1406 'yer', | |
1407 'ya']) | |
1408 affected_file.ChangedContents().AndReturn([ | |
1409 (42, 'yo, ' + content2), | |
1410 (43, 'yer'), | |
1411 (23, 'ya')]) | |
1412 affected_file.LocalPath().AndReturn('foo.cc') | |
1413 | |
1414 self.mox.ReplayAll() | 1398 self.mox.ReplayAll() |
1415 | 1399 |
1416 results1 = check(input_api1, presubmit.OutputApi, None) | 1400 results1 = check(input_api1, presubmit.OutputApi, None) |
1417 self.assertEquals(results1, []) | 1401 self.assertEquals(results1, []) |
1418 results2 = check(input_api2, presubmit.OutputApi, None) | 1402 results2 = check(input_api2, presubmit.OutputApi, None) |
1419 self.assertEquals(len(results2), 1) | 1403 self.assertEquals(len(results2), 1) |
1420 self.assertEquals(results2[0].__class__, error_type) | 1404 self.assertEquals(results2[0].__class__, error_type) |
1421 | 1405 |
1422 def ReadFileTest(self, check, content1, content2, error_type): | 1406 def ReadFileTest(self, check, content1, content2, error_type): |
1423 self.mox.StubOutWithMock(presubmit.InputApi, 'ReadFile') | 1407 self.mox.StubOutWithMock(presubmit.InputApi, 'ReadFile') |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1574 input_api1 = self.MockInputApi(change1, False) | 1558 input_api1 = self.MockInputApi(change1, False) |
1575 affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1559 affected_file1 = self.mox.CreateMock(presubmit.SvnAffectedFile) |
1576 affected_file1.LocalPath().AndReturn('foo.cc') | 1560 affected_file1.LocalPath().AndReturn('foo.cc') |
1577 affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1561 affected_file2 = self.mox.CreateMock(presubmit.SvnAffectedFile) |
1578 affected_file2.LocalPath().AndReturn('foo/Makefile') | 1562 affected_file2.LocalPath().AndReturn('foo/Makefile') |
1579 affected_file3 = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1563 affected_file3 = self.mox.CreateMock(presubmit.SvnAffectedFile) |
1580 affected_file3.LocalPath().AndReturn('makefile') | 1564 affected_file3.LocalPath().AndReturn('makefile') |
1581 # Only this one will trigger. | 1565 # Only this one will trigger. |
1582 affected_file4 = self.mox.CreateMock(presubmit.SvnAffectedFile) | 1566 affected_file4 = self.mox.CreateMock(presubmit.SvnAffectedFile) |
1583 affected_file4.LocalPath().AndReturn('makefile.foo') | 1567 affected_file4.LocalPath().AndReturn('makefile.foo') |
1584 affected_file1.NewContents().AndReturn(['yo, ']) | |
1585 affected_file4.NewContents().AndReturn(['ye\t']) | |
1586 affected_file4.ChangedContents().AndReturn([(46, 'ye\t')]) | |
1587 affected_file4.LocalPath().AndReturn('makefile.foo') | 1568 affected_file4.LocalPath().AndReturn('makefile.foo') |
1588 affected_files = (affected_file1, affected_file2, | 1569 output1 = [ |
1589 affected_file3, affected_file4) | 1570 (affected_file1, 42, 'yo, '), |
1590 | 1571 (affected_file2, 43, 'yer\t'), |
| 1572 (affected_file3, 45, 'yr\t'), |
| 1573 (affected_file4, 46, 'ye\t'), |
| 1574 ] |
1591 def test(source_filter): | 1575 def test(source_filter): |
1592 for x in affected_files: | 1576 for i in output1: |
1593 if source_filter(x): | 1577 if source_filter(i[0]): |
1594 yield x | 1578 yield i |
1595 # Override the mock of these functions. | 1579 # Override the mock of these functions. |
1596 input_api1.FilterSourceFile = lambda x: x | 1580 input_api1.FilterSourceFile = lambda x: x |
1597 input_api1.AffectedFiles = test | 1581 input_api1.RightHandSideLines = test |
1598 self.mox.ReplayAll() | 1582 self.mox.ReplayAll() |
1599 | 1583 |
1600 results1 = presubmit_canned_checks.CheckChangeHasNoTabs(input_api1, | 1584 results1 = presubmit_canned_checks.CheckChangeHasNoTabs(input_api1, |
1601 presubmit.OutputApi, None) | 1585 presubmit.OutputApi, None) |
1602 self.assertEquals(len(results1), 1) | 1586 self.assertEquals(len(results1), 1) |
1603 self.assertEquals(results1[0].__class__, | 1587 self.assertEquals(results1[0].__class__, |
1604 presubmit.OutputApi.PresubmitPromptWarning) | 1588 presubmit.OutputApi.PresubmitPromptWarning) |
1605 self.assertEquals(results1[0]._long_text, | 1589 self.assertEquals(results1[0]._long_text, |
1606 'makefile.foo, line 46') | 1590 'makefile.foo, line 46') |
1607 | 1591 |
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2135 whitelist=['^a$', '^b$'], | 2119 whitelist=['^a$', '^b$'], |
2136 blacklist=['a']) | 2120 blacklist=['a']) |
2137 self.assertEqual(results, []) | 2121 self.assertEqual(results, []) |
2138 self.checkstdout( | 2122 self.checkstdout( |
2139 'Running %s\n' % presubmit.os.path.join('random_directory', 'b')) | 2123 'Running %s\n' % presubmit.os.path.join('random_directory', 'b')) |
2140 | 2124 |
2141 | 2125 |
2142 if __name__ == '__main__': | 2126 if __name__ == '__main__': |
2143 import unittest | 2127 import unittest |
2144 unittest.main() | 2128 unittest.main() |
OLD | NEW |