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