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 exceptions | 8 import exceptions |
9 import os | 9 import os |
10 import StringIO | 10 import StringIO |
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 """Tests presubmit.InputApi.""" | 487 """Tests presubmit.InputApi.""" |
488 def testMembersChanged(self): | 488 def testMembersChanged(self): |
489 self.mox.ReplayAll() | 489 self.mox.ReplayAll() |
490 members = [ | 490 members = [ |
491 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', | 491 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', |
492 'AffectedTextFiles', | 492 'AffectedTextFiles', |
493 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', | 493 'DEFAULT_BLACK_LIST', 'DEFAULT_WHITE_LIST', |
494 'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', | 494 'DepotToLocalPath', 'FilterSourceFile', 'LocalPaths', |
495 'LocalToDepotPath', | 495 'LocalToDepotPath', |
496 'PresubmitLocalPath', 'ReadFile', 'RightHandSideLines', 'ServerPaths', | 496 'PresubmitLocalPath', 'ReadFile', 'RightHandSideLines', 'ServerPaths', |
497 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', | 497 'basename', 'cPickle', 'cStringIO', 'canned_checks', 'change', 'environ', |
498 'is_committing', 'marshal', 'os_path', 'pickle', 'platform', | 498 'is_committing', 'marshal', 'os_path', 'pickle', 'platform', |
| 499 'python_executable', |
499 're', 'subprocess', 'tempfile', 'traceback', 'unittest', 'urllib2', | 500 're', 'subprocess', 'tempfile', 'traceback', 'unittest', 'urllib2', |
500 'version', | 501 'version', |
501 ] | 502 ] |
502 # If this test fails, you should add the relevant test. | 503 # If this test fails, you should add the relevant test. |
503 self.compareMembers(presubmit.InputApi(None, './.', False), members) | 504 self.compareMembers(presubmit.InputApi(None, './.', False), members) |
504 | 505 |
505 def testDepotToLocalPath(self): | 506 def testDepotToLocalPath(self): |
506 presubmit.gclient.CaptureSVNInfo('svn://foo/smurf').AndReturn( | 507 presubmit.gclient.CaptureSVNInfo('svn://foo/smurf').AndReturn( |
507 {'Path': 'prout'}) | 508 {'Path': 'prout'}) |
508 presubmit.gclient.CaptureSVNInfo('svn:/foo/notfound/burp').AndReturn({}) | 509 presubmit.gclient.CaptureSVNInfo('svn:/foo/notfound/burp').AndReturn({}) |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
968 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], | 969 change = presubmit.Change('foo', 'foo', self.fake_root_dir, [('M', 'AA')], |
969 0, 0) | 970 0, 0) |
970 self.compareMembers(change, members) | 971 self.compareMembers(change, members) |
971 | 972 |
972 | 973 |
973 class CannedChecksUnittest(PresubmitTestsBase): | 974 class CannedChecksUnittest(PresubmitTestsBase): |
974 """Tests presubmit_canned_checks.py.""" | 975 """Tests presubmit_canned_checks.py.""" |
975 | 976 |
976 def setUp(self): | 977 def setUp(self): |
977 PresubmitTestsBase.setUp(self) | 978 PresubmitTestsBase.setUp(self) |
978 self.mox.StubOutWithMock(presubmit_canned_checks, | |
979 '_RunPythonUnitTests_LoadTests') | |
980 | 979 |
981 def MockInputApi(self, change, committing): | 980 def MockInputApi(self, change, committing): |
982 input_api = self.mox.CreateMock(presubmit.InputApi) | 981 input_api = self.mox.CreateMock(presubmit.InputApi) |
983 input_api.cStringIO = presubmit.cStringIO | 982 input_api.cStringIO = presubmit.cStringIO |
984 input_api.re = presubmit.re | 983 input_api.re = presubmit.re |
985 input_api.traceback = presubmit.traceback | 984 input_api.traceback = presubmit.traceback |
986 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) | 985 input_api.urllib2 = self.mox.CreateMock(presubmit.urllib2) |
987 input_api.unittest = unittest | 986 input_api.unittest = unittest |
| 987 input_api.subprocess = self.mox.CreateMock(presubmit.subprocess) |
| 988 |
988 input_api.change = change | 989 input_api.change = change |
989 input_api.is_committing = committing | 990 input_api.is_committing = committing |
| 991 input_api.python_executable = 'pyyyyython' |
990 return input_api | 992 return input_api |
991 | 993 |
992 def testMembersChanged(self): | 994 def testMembersChanged(self): |
993 self.mox.ReplayAll() | 995 self.mox.ReplayAll() |
994 members = [ | 996 members = [ |
995 'CheckChangeHasBugField', 'CheckChangeHasDescription', | 997 'CheckChangeHasBugField', 'CheckChangeHasDescription', |
996 'CheckChangeHasNoStrayWhitespace', | 998 'CheckChangeHasNoStrayWhitespace', |
997 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', | 999 'CheckChangeHasOnlyOneEol', 'CheckChangeHasNoCR', |
998 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', | 1000 'CheckChangeHasNoCrAndHasOnlyOneEol', 'CheckChangeHasNoTabs', |
999 'CheckChangeHasQaField', 'CheckChangeHasTestedField', | 1001 'CheckChangeHasQaField', 'CheckChangeHasTestedField', |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1264 | 1266 |
1265 def testRunPythonUnitTestsNoTest(self): | 1267 def testRunPythonUnitTestsNoTest(self): |
1266 input_api = self.MockInputApi(None, False) | 1268 input_api = self.MockInputApi(None, False) |
1267 self.mox.ReplayAll() | 1269 self.mox.ReplayAll() |
1268 results = presubmit_canned_checks.RunPythonUnitTests( | 1270 results = presubmit_canned_checks.RunPythonUnitTests( |
1269 input_api, presubmit.OutputApi, []) | 1271 input_api, presubmit.OutputApi, []) |
1270 self.assertEquals(results, []) | 1272 self.assertEquals(results, []) |
1271 | 1273 |
1272 def testRunPythonUnitTestsNonExistentUpload(self): | 1274 def testRunPythonUnitTestsNonExistentUpload(self): |
1273 input_api = self.MockInputApi(None, False) | 1275 input_api = self.MockInputApi(None, False) |
1274 presubmit_canned_checks._RunPythonUnitTests_LoadTests( | 1276 process = self.mox.CreateMockAnything() |
1275 input_api, '_non_existent_module').AndRaise( | 1277 process.returncode = 2 |
1276 exceptions.ImportError('Blehh')) | 1278 input_api.subprocess.Popen( |
| 1279 ['pyyyyython', '-m', '_non_existent_module'], cwd=None, env=None, |
| 1280 stderr=presubmit.subprocess.PIPE, stdin=presubmit.subprocess.PIPE, |
| 1281 stdout=presubmit.subprocess.PIPE).AndReturn(process) |
| 1282 process.communicate().AndReturn( |
| 1283 ('', 'pyyython: module _non_existent_module not found')) |
1277 self.mox.ReplayAll() | 1284 self.mox.ReplayAll() |
1278 | 1285 |
1279 results = presubmit_canned_checks.RunPythonUnitTests( | 1286 results = presubmit_canned_checks.RunPythonUnitTests( |
1280 input_api, presubmit.OutputApi, ['_non_existent_module']) | 1287 input_api, presubmit.OutputApi, ['_non_existent_module']) |
1281 self.assertEquals(len(results), 1) | 1288 self.assertEquals(len(results), 1) |
1282 self.assertEquals(results[0].__class__, | 1289 self.assertEquals(results[0].__class__, |
1283 presubmit.OutputApi.PresubmitNotifyResult) | 1290 presubmit.OutputApi.PresubmitNotifyResult) |
1284 | 1291 |
1285 def testRunPythonUnitTestsNonExistentCommitting(self): | 1292 def testRunPythonUnitTestsNonExistentCommitting(self): |
1286 input_api = self.MockInputApi(None, True) | 1293 input_api = self.MockInputApi(None, True) |
1287 presubmit_canned_checks._RunPythonUnitTests_LoadTests( | 1294 process = self.mox.CreateMockAnything() |
1288 input_api, '_non_existent_module').AndRaise( | 1295 process.returncode = 2 |
1289 exceptions.ImportError('Blehh')) | 1296 input_api.subprocess.Popen( |
| 1297 ['pyyyyython', '-m', '_non_existent_module'], cwd=None, env=None, |
| 1298 stderr=presubmit.subprocess.PIPE, stdin=presubmit.subprocess.PIPE, |
| 1299 stdout=presubmit.subprocess.PIPE).AndReturn(process) |
| 1300 process.communicate().AndReturn( |
| 1301 ('', 'pyyython: module _non_existent_module not found')) |
1290 self.mox.ReplayAll() | 1302 self.mox.ReplayAll() |
1291 results = presubmit_canned_checks.RunPythonUnitTests( | 1303 results = presubmit_canned_checks.RunPythonUnitTests( |
1292 input_api, presubmit.OutputApi, ['_non_existent_module']) | 1304 input_api, presubmit.OutputApi, ['_non_existent_module']) |
1293 self.assertEquals(len(results), 1) | 1305 self.assertEquals(len(results), 1) |
1294 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) | 1306 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) |
1295 | 1307 |
1296 def testRunPythonUnitTestsEmptyUpload(self): | |
1297 input_api = self.MockInputApi(None, False) | |
1298 test_module = self.mox.CreateMockAnything() | |
1299 presubmit_canned_checks._RunPythonUnitTests_LoadTests( | |
1300 input_api, 'test_module').AndReturn([]) | |
1301 self.mox.ReplayAll() | |
1302 | |
1303 results = presubmit_canned_checks.RunPythonUnitTests( | |
1304 input_api, presubmit.OutputApi, ['test_module']) | |
1305 self.assertEquals(results, []) | |
1306 | |
1307 def testRunPythonUnitTestsEmptyCommitting(self): | |
1308 input_api = self.MockInputApi(None, True) | |
1309 test_module = self.mox.CreateMockAnything() | |
1310 presubmit_canned_checks._RunPythonUnitTests_LoadTests( | |
1311 input_api, 'test_module').AndReturn([]) | |
1312 self.mox.ReplayAll() | |
1313 | |
1314 results = presubmit_canned_checks.RunPythonUnitTests( | |
1315 input_api, presubmit.OutputApi, ['test_module']) | |
1316 self.assertEquals(results, []) | |
1317 | |
1318 def testRunPythonUnitTestsFailureUpload(self): | 1308 def testRunPythonUnitTestsFailureUpload(self): |
1319 input_api = self.MockInputApi(None, False) | 1309 input_api = self.MockInputApi(None, False) |
1320 input_api.unittest = self.mox.CreateMock(unittest) | 1310 input_api.unittest = self.mox.CreateMock(unittest) |
1321 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) | 1311 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) |
1322 test = self.mox.CreateMockAnything() | 1312 process = self.mox.CreateMockAnything() |
1323 presubmit_canned_checks._RunPythonUnitTests_LoadTests( | 1313 process.returncode = -1 |
1324 input_api, 'test_module').AndReturn([test]) | 1314 input_api.subprocess.Popen( |
1325 runner = self.mox.CreateMockAnything() | 1315 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, |
1326 buffer = self.mox.CreateMockAnything() | 1316 stderr=presubmit.subprocess.PIPE, stdin=presubmit.subprocess.PIPE, |
1327 input_api.cStringIO.StringIO().AndReturn(buffer) | 1317 stdout=presubmit.subprocess.PIPE).AndReturn(process) |
1328 buffer.getvalue().AndReturn('BOO HOO!') | 1318 process.communicate().AndReturn(('BOO HOO!', '')) |
1329 input_api.unittest.TextTestRunner(stream=buffer, verbosity=0 | |
1330 ).AndReturn(runner) | |
1331 suite = self.mox.CreateMockAnything() | |
1332 input_api.unittest.TestSuite([test]).AndReturn(suite) | |
1333 test_result = self.mox.CreateMockAnything() | |
1334 runner.run(suite).AndReturn(test_result) | |
1335 test_result.wasSuccessful().AndReturn(False) | |
1336 test_result.failures = [None, None] | |
1337 test_result.errors = [None, None, None] | |
1338 self.mox.ReplayAll() | 1319 self.mox.ReplayAll() |
1339 | 1320 |
1340 results = presubmit_canned_checks.RunPythonUnitTests( | 1321 results = presubmit_canned_checks.RunPythonUnitTests( |
1341 input_api, presubmit.OutputApi, ['test_module']) | 1322 input_api, presubmit.OutputApi, ['test_module']) |
1342 self.assertEquals(len(results), 1) | 1323 self.assertEquals(len(results), 1) |
1343 self.assertEquals(results[0].__class__, | 1324 self.assertEquals(results[0].__class__, |
1344 presubmit.OutputApi.PresubmitNotifyResult) | 1325 presubmit.OutputApi.PresubmitNotifyResult) |
1345 self.assertEquals(results[0]._long_text, 'BOO HOO!') | 1326 self.assertEquals(results[0]._long_text, |
| 1327 "Test 'test_module' failed with code -1\nBOO HOO!") |
1346 | 1328 |
1347 def testRunPythonUnitTestsFailureCommitting(self): | 1329 def testRunPythonUnitTestsFailureCommitting(self): |
1348 input_api = self.MockInputApi(None, True) | 1330 input_api = self.MockInputApi(None, True) |
1349 input_api.unittest = self.mox.CreateMock(unittest) | 1331 process = self.mox.CreateMockAnything() |
1350 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) | 1332 process.returncode = 1 |
1351 test = self.mox.CreateMockAnything() | 1333 input_api.subprocess.Popen( |
1352 presubmit_canned_checks._RunPythonUnitTests_LoadTests( | 1334 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, |
1353 input_api, 'test_module').AndReturn([test]) | 1335 stderr=presubmit.subprocess.PIPE, stdin=presubmit.subprocess.PIPE, |
1354 runner = self.mox.CreateMockAnything() | 1336 stdout=presubmit.subprocess.PIPE).AndReturn(process) |
1355 buffer = self.mox.CreateMockAnything() | 1337 process.communicate().AndReturn(('BOO HOO!', '')) |
1356 input_api.cStringIO.StringIO().AndReturn(buffer) | |
1357 buffer.getvalue().AndReturn('BOO HOO!') | |
1358 input_api.unittest.TextTestRunner(stream=buffer, verbosity=0 | |
1359 ).AndReturn(runner) | |
1360 suite = self.mox.CreateMockAnything() | |
1361 input_api.unittest.TestSuite([test]).AndReturn(suite) | |
1362 test_result = self.mox.CreateMockAnything() | |
1363 runner.run(suite).AndReturn(test_result) | |
1364 test_result.wasSuccessful().AndReturn(False) | |
1365 test_result.failures = [None, None] | |
1366 test_result.errors = [None, None, None] | |
1367 self.mox.ReplayAll() | 1338 self.mox.ReplayAll() |
1368 | 1339 |
1369 results = presubmit_canned_checks.RunPythonUnitTests( | 1340 results = presubmit_canned_checks.RunPythonUnitTests( |
1370 input_api, presubmit.OutputApi, ['test_module']) | 1341 input_api, presubmit.OutputApi, ['test_module']) |
1371 self.assertEquals(len(results), 1) | 1342 self.assertEquals(len(results), 1) |
1372 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) | 1343 self.assertEquals(results[0].__class__, presubmit.OutputApi.PresubmitError) |
1373 self.assertEquals(results[0]._long_text, 'BOO HOO!') | 1344 self.assertEquals(results[0]._long_text, |
| 1345 "Test 'test_module' failed with code 1\nBOO HOO!") |
1374 | 1346 |
1375 def testRunPythonUnitTestsSuccess(self): | 1347 def testRunPythonUnitTestsSuccess(self): |
1376 input_api = self.MockInputApi(None, False) | 1348 input_api = self.MockInputApi(None, False) |
1377 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) | 1349 input_api.cStringIO = self.mox.CreateMock(presubmit.cStringIO) |
1378 input_api.unittest = self.mox.CreateMock(unittest) | 1350 input_api.unittest = self.mox.CreateMock(unittest) |
1379 test = self.mox.CreateMockAnything() | 1351 process = self.mox.CreateMockAnything() |
1380 presubmit_canned_checks._RunPythonUnitTests_LoadTests( | 1352 process.returncode = 0 |
1381 input_api, 'test_module').AndReturn([test]) | 1353 input_api.subprocess.Popen( |
1382 runner = self.mox.CreateMockAnything() | 1354 ['pyyyyython', '-m', 'test_module'], cwd=None, env=None, |
1383 buffer = self.mox.CreateMockAnything() | 1355 stderr=presubmit.subprocess.PIPE, stdin=presubmit.subprocess.PIPE, |
1384 input_api.cStringIO.StringIO().AndReturn(buffer) | 1356 stdout=presubmit.subprocess.PIPE).AndReturn(process) |
1385 input_api.unittest.TextTestRunner(stream=buffer, verbosity=0 | 1357 process.communicate().AndReturn(('', '')) |
1386 ).AndReturn(runner) | |
1387 suite = self.mox.CreateMockAnything() | |
1388 input_api.unittest.TestSuite([test]).AndReturn(suite) | |
1389 test_result = self.mox.CreateMockAnything() | |
1390 runner.run(suite).AndReturn(test_result) | |
1391 test_result.wasSuccessful().AndReturn(True) | |
1392 test_result.failures = 0 | |
1393 test_result.errors = 0 | |
1394 self.mox.ReplayAll() | 1358 self.mox.ReplayAll() |
1395 | 1359 |
1396 results = presubmit_canned_checks.RunPythonUnitTests( | 1360 results = presubmit_canned_checks.RunPythonUnitTests( |
1397 input_api, presubmit.OutputApi, ['test_module']) | 1361 input_api, presubmit.OutputApi, ['test_module']) |
1398 self.assertEquals(len(results), 0) | 1362 self.assertEquals(len(results), 0) |
1399 | 1363 |
1400 | 1364 |
1401 if __name__ == '__main__': | 1365 if __name__ == '__main__': |
1402 unittest.main() | 1366 unittest.main() |
OLD | NEW |