summaryrefslogtreecommitdiffstats
path: root/tools/auto_bisect/source_control_test.py
blob: e8f05d30720c0d4d8458bda52172c05ec780be78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Unit tests for the source_control module."""

import unittest
import mock

import source_control


class SourceControlTest(unittest.TestCase):

  @mock.patch('source_control.bisect_utils.CheckRunGit')
  def testQueryRevisionInfo(self, mock_run_git):
    # The QueryRevisionInfo function should run a sequence of git commands,
    # then returns a dict with the results.
    command_output_map = [
        (['log', '--format=%aN', '-1', 'abcd1234'], 'Some Name\n'),
        (['log', '--format=%aE', '-1', 'abcd1234'], 'somename@x.com'),
        (['log', '--format=%s', '-1', 'abcd1234'], 'Commit subject '),
        (['log', '--format=%cD', '-1', 'abcd1234'], 'Fri, 10 Oct 2014'),
        (['log', '--format=%b', '-1', 'abcd1234'], 'Commit body\n'),
    ]
    _SetMockCheckRunGitBehavior(mock_run_git, command_output_map)
    # The result of calling QueryRevisionInfo is a dictionary like that below.
    # Trailing whitespace is stripped.
    expected = {
        'author': 'Some Name',
        'email': 'somename@x.com',
        'date': 'Fri, 10 Oct 2014',
        'subject': 'Commit subject',
        'body': 'Commit body',
    }
    self.assertEqual(expected, source_control.QueryRevisionInfo('abcd1234'))
    self.assertEqual(5, mock_run_git.call_count)

  def testResolveToRevision_InputGitHash(self):
    # The ResolveToRevision function returns a git commit hash corresponding
    # to the input, so if the input can't be parsed as an int, it is returned.
    self.assertEqual(
        'abcd1234',
        source_control.ResolveToRevision('abcd1234', 'chromium', {}, 5))

    # Note: It actually does this for any junk that isn't an int. This isn't
    # necessarily desired behavior.
    self.assertEqual(
        'foo bar',
        source_control.ResolveToRevision('foo bar', 'chromium', {}, 5))

  @mock.patch('source_control.bisect_utils.CheckRunGit')
  def testResolveToRevision_NotFound(self, mock_run_git):
    # If no corresponding git hash was found, then None is returned.
    mock_run_git.return_value = ''
    self.assertIsNone(
        source_control.ResolveToRevision('12345', 'chromium', {}, 5))

  @mock.patch('source_control.bisect_utils.CheckRunGit')
  def testResolveToRevision_Found(self, mock_run_git):
    # In general, ResolveToRevision finds a git commit hash by repeatedly
    # calling "git log --grep ..." with different numbers until something
    # matches.
    mock_run_git.return_value = 'abcd1234'
    self.assertEqual(
        'abcd1234',
        source_control.ResolveToRevision('12345', 'chromium', {}, 5))
    self.assertEqual(1, mock_run_git.call_count)


def _SetMockCheckRunGitBehavior(mock_obj, command_output_map):
  """Sets the behavior of a mock function according to the given mapping."""
  # Unused argument 'cwd', expected in args list but not needed.
  # pylint: disable=W0613
  def FakeCheckRunGit(in_command, cwd=None):
    for command, output in command_output_map:
      if command == in_command:
        return output
  mock_obj.side_effect = FakeCheckRunGit


if __name__ == '__main__':
  unittest.main()