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.
5 """Unit tests for the source_control module."""
13 class SourceControlTest(unittest
.TestCase
):
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'),
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.
30 'author': 'Some Name',
31 'email': 'somename@x.com',
32 'date': 'Fri, 10 Oct 2014',
33 'subject': 'Commit subject',
34 'body': 'Commit body',
36 self
.assertEqual(expected
, source_control
.QueryRevisionInfo('abcd1234'))
37 self
.assertEqual(5, mock_run_git
.call_count
)
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.
44 source_control
.ResolveToRevision('abcd1234', 'chromium', {}, 5))
46 # Note: It actually does this for any junk that isn't an int. This isn't
47 # necessarily desired behavior.
50 source_control
.ResolveToRevision('foo bar', 'chromium', {}, 5))
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
= ''
57 source_control
.ResolveToRevision('12345', 'chromium', {}, 5))
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
64 mock_run_git
.return_value
= 'abcd1234'
67 source_control
.ResolveToRevision('12345', 'chromium', {}, 5))
68 self
.assertEqual(1, mock_run_git
.call_count
)
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
:
79 mock_obj
.side_effect
= FakeCheckRunGit
82 if __name__
== '__main__':