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 random | 9 import random |
10 import string | 10 import string |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 self.mox.ReplayAll() | 343 self.mox.ReplayAll() |
344 | 344 |
345 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0, | 345 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0, |
346 description='\n'.join(description_lines), | 346 description='\n'.join(description_lines), |
347 files=files) | 347 files=files) |
348 | 348 |
349 output = StringIO.StringIO() | 349 output = StringIO.StringIO() |
350 input = StringIO.StringIO('y\n') | 350 input = StringIO.StringIO('y\n') |
351 | 351 |
352 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, | 352 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, |
353 None)) | 353 None, False)) |
354 self.assertEqual(output.getvalue().count('!!'), 2) | 354 self.assertEqual(output.getvalue().count('!!'), 2) |
355 | 355 |
356 def testDoPresubmitChecksPromptsAfterWarnings(self): | 356 def testDoPresubmitChecksPromptsAfterWarnings(self): |
357 join = presubmit.os.path.join | 357 join = presubmit.os.path.join |
358 description_lines = ('Hello there', | 358 description_lines = ('Hello there', |
359 'this is a change', | 359 'this is a change', |
360 'NOSUCHKEY=http://tracker/123') | 360 'NOSUCHKEY=http://tracker/123') |
361 files = [ | 361 files = [ |
362 ['A', join('haspresubmit', 'blat.cc')], | 362 ['A', join('haspresubmit', 'blat.cc')], |
363 ] | 363 ] |
364 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py') | 364 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py') |
365 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py') | 365 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', 'PRESUBMIT.py') |
366 for i in range(2): | 366 for i in range(2): |
367 presubmit.os.path.isfile(presubmit_path).AndReturn(True) | 367 presubmit.os.path.isfile(presubmit_path).AndReturn(True) |
368 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) | 368 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) |
369 presubmit.gcl.ReadFile(presubmit_path, 'rU' | 369 presubmit.gcl.ReadFile(presubmit_path, 'rU' |
370 ).AndReturn(self.presubmit_text) | 370 ).AndReturn(self.presubmit_text) |
371 presubmit.gcl.ReadFile(haspresubmit_path, 'rU' | 371 presubmit.gcl.ReadFile(haspresubmit_path, 'rU' |
372 ).AndReturn(self.presubmit_text) | 372 ).AndReturn(self.presubmit_text) |
373 self.mox.ReplayAll() | 373 self.mox.ReplayAll() |
374 | 374 |
375 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0, | 375 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0, |
376 description='\n'.join(description_lines), | 376 description='\n'.join(description_lines), |
377 files=files) | 377 files=files) |
378 | 378 |
379 output = StringIO.StringIO() | 379 output = StringIO.StringIO() |
380 input = StringIO.StringIO('n\n') # say no to the warning | 380 input = StringIO.StringIO('n\n') # say no to the warning |
381 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, | 381 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, |
382 None)) | 382 None, False)) |
383 self.assertEqual(output.getvalue().count('??'), 2) | 383 self.assertEqual(output.getvalue().count('??'), 2) |
384 | 384 |
385 output = StringIO.StringIO() | 385 output = StringIO.StringIO() |
386 input = StringIO.StringIO('y\n') # say yes to the warning | 386 input = StringIO.StringIO('y\n') # say yes to the warning |
387 self.failUnless(presubmit.DoPresubmitChecks(ci, | 387 self.failUnless(presubmit.DoPresubmitChecks(ci, |
388 False, | 388 False, |
389 True, | 389 True, |
390 output, | 390 output, |
391 input, | 391 input, |
392 None)) | 392 None, |
| 393 False)) |
393 self.assertEquals(output.getvalue().count('??'), 2) | 394 self.assertEquals(output.getvalue().count('??'), 2) |
394 | 395 |
395 def testDoPresubmitChecksNoWarningPromptIfErrors(self): | 396 def testDoPresubmitChecksNoWarningPromptIfErrors(self): |
396 join = presubmit.os.path.join | 397 join = presubmit.os.path.join |
397 description_lines = ('Hello there', | 398 description_lines = ('Hello there', |
398 'this is a change', | 399 'this is a change', |
399 'NOSUCHKEY=http://tracker/123', | 400 'NOSUCHKEY=http://tracker/123', |
400 'REALLYNOSUCHKEY=http://tracker/123') | 401 'REALLYNOSUCHKEY=http://tracker/123') |
401 files = [ | 402 files = [ |
402 ['A', join('haspresubmit', 'blat.cc')], | 403 ['A', join('haspresubmit', 'blat.cc')], |
403 ] | 404 ] |
404 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py') | 405 presubmit_path = join(self.fake_root_dir, 'PRESUBMIT.py') |
405 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', | 406 haspresubmit_path = join(self.fake_root_dir, 'haspresubmit', |
406 'PRESUBMIT.py') | 407 'PRESUBMIT.py') |
407 presubmit.os.path.isfile(presubmit_path).AndReturn(True) | 408 presubmit.os.path.isfile(presubmit_path).AndReturn(True) |
408 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) | 409 presubmit.os.path.isfile(haspresubmit_path).AndReturn(True) |
409 presubmit.gcl.ReadFile(presubmit_path, 'rU').AndReturn(self.presubmit_text) | 410 presubmit.gcl.ReadFile(presubmit_path, 'rU').AndReturn(self.presubmit_text) |
410 presubmit.gcl.ReadFile(haspresubmit_path, 'rU' | 411 presubmit.gcl.ReadFile(haspresubmit_path, 'rU' |
411 ).AndReturn(self.presubmit_text) | 412 ).AndReturn(self.presubmit_text) |
412 self.mox.ReplayAll() | 413 self.mox.ReplayAll() |
413 | 414 |
414 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0, | 415 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0, |
415 description='\n'.join(description_lines), | 416 description='\n'.join(description_lines), |
416 files=files) | 417 files=files) |
417 output = StringIO.StringIO() | 418 output = StringIO.StringIO() |
418 input = StringIO.StringIO() # should be unused | 419 input = StringIO.StringIO() # should be unused |
419 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, | 420 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, |
420 None)) | 421 None, False)) |
421 self.assertEqual(output.getvalue().count('??'), 2) | 422 self.assertEqual(output.getvalue().count('??'), 2) |
422 self.assertEqual(output.getvalue().count('XX!!XX'), 2) | 423 self.assertEqual(output.getvalue().count('XX!!XX'), 2) |
423 self.assertEqual(output.getvalue().count('(y/N)'), 0) | 424 self.assertEqual(output.getvalue().count('(y/N)'), 0) |
424 | 425 |
425 def testDoDefaultPresubmitChecks(self): | 426 def testDoDefaultPresubmitChecks(self): |
426 join = presubmit.os.path.join | 427 join = presubmit.os.path.join |
427 description_lines = ('Hello there', | 428 description_lines = ('Hello there', |
428 'this is a change', | 429 'this is a change', |
429 'STORY=http://tracker/123') | 430 'STORY=http://tracker/123') |
430 files = [ | 431 files = [ |
(...skipping 14 matching lines...) Expand all Loading... |
445 | 446 |
446 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0, | 447 ci = presubmit.gcl.ChangeInfo(name='mychange', issue=0, patchset=0, |
447 description='\n'.join(description_lines), | 448 description='\n'.join(description_lines), |
448 files=files) | 449 files=files) |
449 | 450 |
450 | 451 |
451 output = StringIO.StringIO() | 452 output = StringIO.StringIO() |
452 input = StringIO.StringIO('y\n') | 453 input = StringIO.StringIO('y\n') |
453 # Always fail. | 454 # Always fail. |
454 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, | 455 self.failIf(presubmit.DoPresubmitChecks(ci, False, True, output, input, |
455 DEFAULT_SCRIPT)) | 456 DEFAULT_SCRIPT, False)) |
456 self.assertEquals(output.getvalue().count('!!'), 1) | 457 self.assertEquals(output.getvalue().count('!!'), 1) |
457 | 458 |
458 def testDirectoryHandling(self): | 459 def testDirectoryHandling(self): |
459 files = [ | 460 files = [ |
460 ['A', 'isdir'], | 461 ['A', 'isdir'], |
461 ['A', 'isdir\\blat.cc'], | 462 ['A', 'isdir\\blat.cc'], |
462 ] | 463 ] |
463 presubmit.os.path.exists('isdir').AndReturn(True) | 464 presubmit.os.path.exists('isdir').AndReturn(True) |
464 presubmit.os.path.isdir('isdir').AndReturn(True) | 465 presubmit.os.path.isdir('isdir').AndReturn(True) |
465 presubmit.os.path.exists(presubmit.os.path.join('isdir', 'blat.cc') | 466 presubmit.os.path.exists(presubmit.os.path.join('isdir', 'blat.cc') |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 | 506 |
506 change = presubmit.gcl.ChangeInfo( | 507 change = presubmit.gcl.ChangeInfo( |
507 name='foo', | 508 name='foo', |
508 issue=0, | 509 issue=0, |
509 patchset=0, | 510 patchset=0, |
510 description="Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n", | 511 description="Blah Blah\n\nSTORY=http://tracker.com/42\nBUG=boo\n", |
511 files=None) | 512 files=None) |
512 output = StringIO.StringIO() | 513 output = StringIO.StringIO() |
513 input = StringIO.StringIO('y\n') | 514 input = StringIO.StringIO('y\n') |
514 self.failUnless(presubmit.DoPresubmitChecks(change, False, True, output, | 515 self.failUnless(presubmit.DoPresubmitChecks(change, False, True, output, |
515 input, DEFAULT_SCRIPT)) | 516 input, DEFAULT_SCRIPT, False)) |
516 self.assertEquals(output.getvalue(), | 517 self.assertEquals(output.getvalue(), |
517 ('Warning, no presubmit.py found.\n' | 518 ('Warning, no presubmit.py found.\n' |
518 'Running default presubmit script.\n\n' | 519 'Running default presubmit script.\n' |
519 '** Presubmit Messages **\n\n' | 520 '** Presubmit Messages **\n' |
520 'http://tracker.com/42\n\n')) | 521 'http://tracker.com/42\n\n')) |
521 | 522 |
522 | 523 |
523 class InputApiUnittest(PresubmitTestsBase): | 524 class InputApiUnittest(PresubmitTestsBase): |
524 """Tests presubmit.InputApi.""" | 525 """Tests presubmit.InputApi.""" |
525 def testMembersChanged(self): | 526 def testMembersChanged(self): |
526 self.mox.ReplayAll() | 527 self.mox.ReplayAll() |
527 members = [ | 528 members = [ |
528 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', | 529 'AbsoluteLocalPaths', 'AffectedFiles', 'AffectedSourceFiles', |
529 'AffectedTextFiles', | 530 'AffectedTextFiles', |
(...skipping 667 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1197 test_result.errors = 0 | 1198 test_result.errors = 0 |
1198 self.mox.ReplayAll() | 1199 self.mox.ReplayAll() |
1199 | 1200 |
1200 results = presubmit_canned_checks.RunPythonUnitTests( | 1201 results = presubmit_canned_checks.RunPythonUnitTests( |
1201 input_api, presubmit.OutputApi, ['test_module']) | 1202 input_api, presubmit.OutputApi, ['test_module']) |
1202 self.assertEquals(len(results), 0) | 1203 self.assertEquals(len(results), 0) |
1203 | 1204 |
1204 | 1205 |
1205 if __name__ == '__main__': | 1206 if __name__ == '__main__': |
1206 unittest.main() | 1207 unittest.main() |
OLD | NEW |