diff options
author | Jian Cai <jiancai@google.com> | 2020-04-15 17:53:41 -0700 |
---|---|---|
committer | Jian Cai <jiancai@google.com> | 2020-04-17 01:17:18 +0000 |
commit | c16daa1eb92cba9781a557798b065747128acfb1 (patch) | |
tree | 66b489e45c1227bc89be29a6fc20bee885e3256f /llvm_tools/git_unittest.py | |
parent | 9258b055847fcefb635c0aa74d5be08e699ab37c (diff) | |
download | toolchain-utils-c16daa1eb92cba9781a557798b065747128acfb1.tar.gz |
llvm_tools: move common functions into standalone modules
Move common functions into separate modules and update dependencies
accordingly.
BUG=chromium:1057428
TEST=local tests.
Change-Id: I40f1b613f0a41f1fc478c811379c851479aff7c3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/2151708
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
Reviewed-by: George Burgess <gbiv@chromium.org>
Tested-by: Jian Cai <jiancai@google.com>
Diffstat (limited to 'llvm_tools/git_unittest.py')
-rwxr-xr-x | llvm_tools/git_unittest.py | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/llvm_tools/git_unittest.py b/llvm_tools/git_unittest.py new file mode 100755 index 00000000..39316276 --- /dev/null +++ b/llvm_tools/git_unittest.py @@ -0,0 +1,173 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Copyright 2020 The Chromium OS 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 git helper functions.""" + +from __future__ import print_function + +import os +import subprocess +import unittest +import unittest.mock as mock + +import git + +# These are unittests; protected access is OK to a point. +# pylint: disable=protected-access + + +class HelperFunctionsTest(unittest.TestCase): + """Test class for updating LLVM hashes of packages.""" + + @mock.patch.object(os.path, 'isdir', return_value=False) + def testFailedToCreateBranchForInvalidDirectoryPath(self, mock_isdir): + path_to_repo = '/invalid/path/to/repo' + branch = 'branch-name' + + # Verify the exception is raised when provided an invalid directory path. + with self.assertRaises(ValueError) as err: + git.CreateBranch(path_to_repo, branch) + + self.assertEqual( + str(err.exception), + 'Invalid directory path provided: %s' % path_to_repo) + + mock_isdir.assert_called_once() + + @mock.patch.object(os.path, 'isdir', return_value=True) + @mock.patch.object(subprocess, 'check_output', return_value=None) + def testSuccessfullyCreatedBranch(self, mock_command_output, mock_isdir): + path_to_repo = '/path/to/repo' + branch = 'branch-name' + + git.CreateBranch(path_to_repo, branch) + + mock_isdir.assert_called_once_with(path_to_repo) + + self.assertEqual(mock_command_output.call_count, 2) + + @mock.patch.object(os.path, 'isdir', return_value=False) + def testFailedToDeleteBranchForInvalidDirectoryPath(self, mock_isdir): + path_to_repo = '/invalid/path/to/repo' + branch = 'branch-name' + + # Verify the exception is raised on an invalid repo path. + with self.assertRaises(ValueError) as err: + git.DeleteBranch(path_to_repo, branch) + + self.assertEqual( + str(err.exception), + 'Invalid directory path provided: %s' % path_to_repo) + + mock_isdir.assert_called_once() + + @mock.patch.object(os.path, 'isdir', return_value=True) + @mock.patch.object(subprocess, 'check_output', return_value=None) + def testSuccessfullyDeletedBranch(self, mock_command_output, mock_isdir): + path_to_repo = '/valid/path/to/repo' + branch = 'branch-name' + + git.DeleteBranch(path_to_repo, branch) + + mock_isdir.assert_called_once_with(path_to_repo) + + self.assertEqual(mock_command_output.call_count, 3) + + @mock.patch.object(os.path, 'isdir', return_value=False) + def testFailedToUploadChangesForInvalidDirectoryPath(self, mock_isdir): + path_to_repo = '/some/path/to/repo' + branch = 'update-LLVM_NEXT_HASH-a123testhash3' + commit_messages = ['Test message'] + + # Verify exception is raised when on an invalid repo path. + with self.assertRaises(ValueError) as err: + git.UploadChanges(path_to_repo, branch, commit_messages) + + self.assertEqual( + str(err.exception), 'Invalid path provided: %s' % path_to_repo) + + mock_isdir.assert_called_once() + + @mock.patch.object(os.path, 'isdir', return_value=True) + @mock.patch.object(subprocess, 'check_output', return_value=None) + @mock.patch.object(subprocess, 'Popen') + def testFailedToUploadChangesForReview(self, mock_repo_upload, + mock_command_output, mock_isdir): + + # Simulate the behavior of 'subprocess.Popen()' when uploading the changes + # for review + # + # `Popen.communicate()` returns a tuple of `stdout` and `stderr`. + mock_repo_upload.return_value.communicate.return_value = ( + None, 'Branch does not exist.') + + # Exit code of 1 means failed to upload changes for review. + mock_repo_upload.return_value.returncode = 1 + + path_to_repo = '/some/path/to/repo' + branch = 'invalid-branch-name' + commit_messages = ['Test message'] + + # Verify exception is raised when failed to upload the changes for review. + with self.assertRaises(ValueError) as err: + git.UploadChanges(path_to_repo, branch, commit_messages) + + self.assertEqual(str(err.exception), 'Failed to upload changes for review') + + mock_isdir.assert_called_once_with(path_to_repo) + + mock_command_output.assert_called_once() + mock_command_output_args = mock_command_output.call_args_list[0][0][0] + expected_mock_command_output_prefix = ['git', 'commit', '-F'] + self.assertEqual( + mock_command_output_args[:len(expected_mock_command_output_prefix)], + expected_mock_command_output_prefix) + + mock_repo_upload.assert_called_once() + + @mock.patch.object(os.path, 'isdir', return_value=True) + @mock.patch.object(subprocess, 'check_output', return_value=None) + @mock.patch.object(subprocess, 'Popen') + def testSuccessfullyUploadedChangesForReview(self, mock_repo_upload, + mock_command_output, mock_isdir): + + # A test CL generated by `repo upload`. + repo_upload_contents = ('remote: https://chromium-review.googlesource.' + 'com/c/chromiumos/overlays/chromiumos-overlay/' + '+/193147 Fix stdout') + + # Simulate the behavior of 'subprocess.Popen()' when uploading the changes + # for review + # + # `Popen.communicate()` returns a tuple of `stdout` and `stderr`. + mock_repo_upload.return_value.communicate.return_value = ( + repo_upload_contents, None) + + # Exit code of 0 means successfully uploaded changes for review. + mock_repo_upload.return_value.returncode = 0 + + path_to_repo = '/some/path/to/repo' + branch = 'branch-name' + commit_messages = ['Test message'] + + change_list = git.UploadChanges(path_to_repo, branch, commit_messages) + + self.assertEqual( + change_list.url, + 'https://chromium-review.googlesource.com/c/chromiumos/overlays/' + 'chromiumos-overlay/+/193147') + + self.assertEqual(change_list.cl_number, 193147) + + mock_isdir.assert_called_once_with(path_to_repo) + + mock_command_output.assert_called_once() + + mock_repo_upload.assert_called_once() + + +if __name__ == '__main__': + unittest.main() |