Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(285)

Unified Diff: tools/auto_bisect/source_control_test.py

Issue 650223005: Refactor source_control.py and add a test. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/auto_bisect/source_control.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/auto_bisect/source_control_test.py
diff --git a/tools/auto_bisect/source_control_test.py b/tools/auto_bisect/source_control_test.py
new file mode 100644
index 0000000000000000000000000000000000000000..ceba3e00f6804e5e52efe9e46c52fcae0cf7ccce
--- /dev/null
+++ b/tools/auto_bisect/source_control_test.py
@@ -0,0 +1,84 @@
+# 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()
+
« no previous file with comments | « tools/auto_bisect/source_control.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698