OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2009 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 import StringIO | 8 import StringIO |
9 | 9 |
10 import presubmit_support as presubmit | 10 import presubmit_support as presubmit |
(...skipping 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1062 self.mox.ReplayAll() | 1062 self.mox.ReplayAll() |
1063 members = [ | 1063 members = [ |
1064 'CheckChangeHasBugField', 'CheckChangeHasDescription', | 1064 'CheckChangeHasBugField', 'CheckChangeHasDescription', |
1065 'CheckChangeHasNoStrayWhitespace', | 1065 'CheckChangeHasNoStrayWhitespace', |
1066 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', | 1066 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', |
1067 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', | 1067 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', |
1068 'CheckChangeHasQaField', 'CheckChangeHasTestedField', | 1068 'CheckChangeHasQaField', 'CheckChangeHasTestedField', |
1069 'CheckChangeHasTestField', | 1069 'CheckChangeHasTestField', |
1070 'CheckChangeLintsClean', | 1070 'CheckChangeLintsClean', |
1071 'CheckChangeSvnEolStyle', | 1071 'CheckChangeSvnEolStyle', |
| 1072 'CheckLicense', |
1072 'CheckSvnModifiedDirectories', | 1073 'CheckSvnModifiedDirectories', |
1073 'CheckSvnForCommonMimeTypes', 'CheckSvnProperty', | 1074 'CheckSvnForCommonMimeTypes', 'CheckSvnProperty', |
1074 'CheckDoNotSubmit', | 1075 'CheckDoNotSubmit', |
1075 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', | 1076 'CheckDoNotSubmitInDescription', 'CheckDoNotSubmitInFiles', |
1076 'CheckLongLines', 'CheckTreeIsOpen', 'RunPythonUnitTests', | 1077 'CheckLongLines', 'CheckTreeIsOpen', 'RunPythonUnitTests', |
1077 ] | 1078 ] |
1078 # If this test fails, you should add the relevant test. | 1079 # If this test fails, you should add the relevant test. |
1079 self.compareMembers(presubmit_canned_checks, members) | 1080 self.compareMembers(presubmit_canned_checks, members) |
1080 | 1081 |
1081 def DescriptionTest(self, check, description1, description2, error_type, | 1082 def DescriptionTest(self, check, description1, description2, error_type, |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1275 # Test CheckSvnProperty at the same time. | 1276 # Test CheckSvnProperty at the same time. |
1276 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle, | 1277 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle, |
1277 'svn:eol-style', 'LF', '', True, | 1278 'svn:eol-style', 'LF', '', True, |
1278 presubmit.OutputApi.PresubmitError, True) | 1279 presubmit.OutputApi.PresubmitError, True) |
1279 | 1280 |
1280 def testCheckChangeSvnEolStyleUpload(self): | 1281 def testCheckChangeSvnEolStyleUpload(self): |
1281 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle, | 1282 self.SvnPropertyTest(presubmit_canned_checks.CheckChangeSvnEolStyle, |
1282 'svn:eol-style', 'LF', '', False, | 1283 'svn:eol-style', 'LF', '', False, |
1283 presubmit.OutputApi.PresubmitNotifyResult, True) | 1284 presubmit.OutputApi.PresubmitNotifyResult, True) |
1284 | 1285 |
| 1286 def _LicenseCheck(self, text, license, committing, expected_result): |
| 1287 change = self.mox.CreateMock(presubmit.SvnChange) |
| 1288 change.scm = 'svn' |
| 1289 input_api = self.MockInputApi(change, committing) |
| 1290 affected_file = self.mox.CreateMock(presubmit.SvnAffectedFile) |
| 1291 input_api.AffectedSourceFiles(42).AndReturn([affected_file]) |
| 1292 input_api.ReadFile(affected_file, 'rb').AndReturn(text) |
| 1293 if expected_result: |
| 1294 affected_file.LocalPath().AndReturn('bleh') |
| 1295 |
| 1296 self.mox.ReplayAll() |
| 1297 result = presubmit_canned_checks.CheckLicense( |
| 1298 input_api, presubmit.OutputApi, license, 42) |
| 1299 if expected_result: |
| 1300 self.assertEqual(len(result), 1) |
| 1301 self.assertEqual(result[0].__class__, expected_result) |
| 1302 else: |
| 1303 self.assertEqual(result, []) |
| 1304 |
| 1305 def testCheckLicenseSuccess(self): |
| 1306 text = ( |
| 1307 "#!/bin/python\n" |
| 1308 "# Copyright (c) 2037 Nobody.\n" |
| 1309 "# All Rights Reserved.\n" |
| 1310 "print 'foo'\n" |
| 1311 ) |
| 1312 license = ( |
| 1313 r".*? Copyright \(c\) 2037 Nobody." "\n" |
| 1314 r".*? All Rights Reserved\." "\n" |
| 1315 ) |
| 1316 self._LicenseCheck(text, license, True, None) |
| 1317 |
| 1318 def testCheckLicenseFailCommit(self): |
| 1319 text = ( |
| 1320 "#!/bin/python\n" |
| 1321 "# Copyright (c) 2037 Nobody.\n" |
| 1322 "# All Rights Reserved.\n" |
| 1323 "print 'foo'\n" |
| 1324 ) |
| 1325 license = ( |
| 1326 r".*? Copyright \(c\) 0007 Nobody." "\n" |
| 1327 r".*? All Rights Reserved\." "\n" |
| 1328 ) |
| 1329 self._LicenseCheck(text, license, True, |
| 1330 presubmit.OutputApi.PresubmitPromptWarning) |
| 1331 |
| 1332 def testCheckLicenseFailUpload(self): |
| 1333 text = ( |
| 1334 "#!/bin/python\n" |
| 1335 "# Copyright (c) 2037 Nobody.\n" |
| 1336 "# All Rights Reserved.\n" |
| 1337 "print 'foo'\n" |
| 1338 ) |
| 1339 license = ( |
| 1340 r".*? Copyright \(c\) 0007 Nobody." "\n" |
| 1341 r".*? All Rights Reserved\." "\n" |
| 1342 ) |
| 1343 self._LicenseCheck(text, license, False, |
| 1344 presubmit.OutputApi.PresubmitNotifyResult) |
| 1345 |
1285 def testCannedCheckSvnAccidentalSubmission(self): | 1346 def testCannedCheckSvnAccidentalSubmission(self): |
1286 modified_dir_file = 'foo/' | 1347 modified_dir_file = 'foo/' |
1287 accidental_submssion_file = 'foo/bar.cc' | 1348 accidental_submssion_file = 'foo/bar.cc' |
1288 | 1349 |
1289 change = self.mox.CreateMock(presubmit.SvnChange) | 1350 change = self.mox.CreateMock(presubmit.SvnChange) |
1290 change.scm = 'svn' | 1351 change.scm = 'svn' |
1291 change.GetModifiedFiles().AndReturn([modified_dir_file]) | 1352 change.GetModifiedFiles().AndReturn([modified_dir_file]) |
1292 change.GetAllModifiedFiles().AndReturn([modified_dir_file, | 1353 change.GetAllModifiedFiles().AndReturn([modified_dir_file, |
1293 accidental_submssion_file]) | 1354 accidental_submssion_file]) |
1294 input_api = self.MockInputApi(change, True) | 1355 input_api = self.MockInputApi(change, True) |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1461 self.mox.ReplayAll() | 1522 self.mox.ReplayAll() |
1462 | 1523 |
1463 results = presubmit_canned_checks.RunPythonUnitTests( | 1524 results = presubmit_canned_checks.RunPythonUnitTests( |
1464 input_api, presubmit.OutputApi, ['test_module']) | 1525 input_api, presubmit.OutputApi, ['test_module']) |
1465 self.assertEquals(len(results), 0) | 1526 self.assertEquals(len(results), 0) |
1466 | 1527 |
1467 | 1528 |
1468 if __name__ == '__main__': | 1529 if __name__ == '__main__': |
1469 import unittest | 1530 import unittest |
1470 unittest.main() | 1531 unittest.main() |
OLD | NEW |