| OLD | NEW |
| (Empty) |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Unit tests for the source_control module.""" | |
| 6 | |
| 7 import unittest | |
| 8 import mock | |
| 9 | |
| 10 import source_control | |
| 11 | |
| 12 | |
| 13 class SourceControlTest(unittest.TestCase): | |
| 14 | |
| 15 @mock.patch('source_control.bisect_utils.CheckRunGit') | |
| 16 def testQueryRevisionInfo(self, mock_run_git): | |
| 17 # The QueryRevisionInfo function should run a sequence of git commands, | |
| 18 # then returns a dict with the results. | |
| 19 command_output_map = [ | |
| 20 (['log', '--format=%aN', '-1', 'abcd1234'], 'Some Name\n'), | |
| 21 (['log', '--format=%aE', '-1', 'abcd1234'], 'somename@x.com'), | |
| 22 (['log', '--format=%s', '-1', 'abcd1234'], 'Commit subject '), | |
| 23 (['log', '--format=%cD', '-1', 'abcd1234'], 'Fri, 10 Oct 2014'), | |
| 24 (['log', '--format=%b', '-1', 'abcd1234'], 'Commit body\n'), | |
| 25 ] | |
| 26 _SetMockCheckRunGitBehavior(mock_run_git, command_output_map) | |
| 27 # The result of calling QueryRevisionInfo is a dictionary like that below. | |
| 28 # Trailing whitespace is stripped. | |
| 29 expected = { | |
| 30 'author': 'Some Name', | |
| 31 'email': 'somename@x.com', | |
| 32 'date': 'Fri, 10 Oct 2014', | |
| 33 'subject': 'Commit subject', | |
| 34 'body': 'Commit body', | |
| 35 } | |
| 36 self.assertEqual(expected, source_control.QueryRevisionInfo('abcd1234')) | |
| 37 self.assertEqual(5, mock_run_git.call_count) | |
| 38 | |
| 39 def testResolveToRevision_InputGitHash(self): | |
| 40 # The ResolveToRevision function returns a git commit hash corresponding | |
| 41 # to the input, so if the input can't be parsed as an int, it is returned. | |
| 42 self.assertEqual( | |
| 43 'abcd1234', | |
| 44 source_control.ResolveToRevision('abcd1234', 'chromium', {}, 5)) | |
| 45 | |
| 46 # Note: It actually does this for any junk that isn't an int. This isn't | |
| 47 # necessarily desired behavior. | |
| 48 self.assertEqual( | |
| 49 'foo bar', | |
| 50 source_control.ResolveToRevision('foo bar', 'chromium', {}, 5)) | |
| 51 | |
| 52 @mock.patch('source_control.bisect_utils.CheckRunGit') | |
| 53 def testResolveToRevision_NotFound(self, mock_run_git): | |
| 54 # If no corresponding git hash was found, then None is returned. | |
| 55 mock_run_git.return_value = '' | |
| 56 self.assertIsNone( | |
| 57 source_control.ResolveToRevision('12345', 'chromium', {}, 5)) | |
| 58 | |
| 59 @mock.patch('source_control.bisect_utils.CheckRunGit') | |
| 60 def testResolveToRevision_Found(self, mock_run_git): | |
| 61 # In general, ResolveToRevision finds a git commit hash by repeatedly | |
| 62 # calling "git log --grep ..." with different numbers until something | |
| 63 # matches. | |
| 64 mock_run_git.return_value = 'abcd1234' | |
| 65 self.assertEqual( | |
| 66 'abcd1234', | |
| 67 source_control.ResolveToRevision('12345', 'chromium', {}, 5)) | |
| 68 self.assertEqual(1, mock_run_git.call_count) | |
| 69 | |
| 70 | |
| 71 def _SetMockCheckRunGitBehavior(mock_obj, command_output_map): | |
| 72 """Sets the behavior of a mock function according to the given mapping.""" | |
| 73 # Unused argument 'cwd', expected in args list but not needed. | |
| 74 # pylint: disable=W0613 | |
| 75 def FakeCheckRunGit(in_command, cwd=None): | |
| 76 for command, output in command_output_map: | |
| 77 if command == in_command: | |
| 78 return output | |
| 79 mock_obj.side_effect = FakeCheckRunGit | |
| 80 | |
| 81 | |
| 82 if __name__ == '__main__': | |
| 83 unittest.main() | |
| 84 | |
| OLD | NEW |