1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright 2020 The ChromiumOS Authors 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""Unit tests for git helper functions.""" 8 9 10import os 11import subprocess 12import tempfile 13import unittest 14import unittest.mock as mock 15 16import git 17 18 19# These are unittests; protected access is OK to a point. 20# pylint: disable=protected-access 21 22 23class HelperFunctionsTest(unittest.TestCase): 24 """Test class for updating LLVM hashes of packages.""" 25 26 @mock.patch.object(os.path, "isdir", return_value=False) 27 def testFailedToCreateBranchForInvalidDirectoryPath(self, mock_isdir): 28 path_to_repo = "/invalid/path/to/repo" 29 branch = "branch-name" 30 31 # Verify the exception is raised when provided an invalid directory path. 32 with self.assertRaises(ValueError) as err: 33 git.CreateBranch(path_to_repo, branch) 34 35 self.assertEqual( 36 str(err.exception), 37 "Invalid directory path provided: %s" % path_to_repo, 38 ) 39 40 mock_isdir.assert_called_once() 41 42 @mock.patch.object(os.path, "isdir", return_value=True) 43 @mock.patch.object(subprocess, "check_output", return_value=None) 44 def testSuccessfullyCreatedBranch(self, mock_command_output, mock_isdir): 45 path_to_repo = "/path/to/repo" 46 branch = "branch-name" 47 48 git.CreateBranch(path_to_repo, branch) 49 50 mock_isdir.assert_called_once_with(path_to_repo) 51 52 self.assertEqual(mock_command_output.call_count, 2) 53 54 @mock.patch.object(os.path, "isdir", return_value=False) 55 def testFailedToDeleteBranchForInvalidDirectoryPath(self, mock_isdir): 56 path_to_repo = "/invalid/path/to/repo" 57 branch = "branch-name" 58 59 # Verify the exception is raised on an invalid repo path. 60 with self.assertRaises(ValueError) as err: 61 git.DeleteBranch(path_to_repo, branch) 62 63 self.assertEqual( 64 str(err.exception), 65 "Invalid directory path provided: %s" % path_to_repo, 66 ) 67 68 mock_isdir.assert_called_once() 69 70 @mock.patch.object(os.path, "isdir", return_value=True) 71 @mock.patch.object(subprocess, "check_output", return_value=None) 72 def testSuccessfullyDeletedBranch(self, mock_command_output, mock_isdir): 73 path_to_repo = "/valid/path/to/repo" 74 branch = "branch-name" 75 76 git.DeleteBranch(path_to_repo, branch) 77 78 mock_isdir.assert_called_once_with(path_to_repo) 79 80 self.assertEqual(mock_command_output.call_count, 3) 81 82 @mock.patch.object(os.path, "isdir", return_value=False) 83 def testFailedToUploadChangesForInvalidDirectoryPath(self, mock_isdir): 84 path_to_repo = "/some/path/to/repo" 85 branch = "update-LLVM_NEXT_HASH-a123testhash3" 86 commit_messages = ["Test message"] 87 88 # Verify exception is raised when on an invalid repo path. 89 with self.assertRaises(ValueError) as err: 90 git.UploadChanges(path_to_repo, branch, commit_messages) 91 92 self.assertEqual( 93 str(err.exception), "Invalid path provided: %s" % path_to_repo 94 ) 95 96 mock_isdir.assert_called_once() 97 98 @mock.patch.object(os.path, "isdir", return_value=True) 99 @mock.patch.object(subprocess, "check_output") 100 @mock.patch.object(tempfile, "NamedTemporaryFile") 101 def testSuccessfullyUploadedChangesForReview( 102 self, mock_tempfile, mock_commands, mock_isdir 103 ): 104 105 path_to_repo = "/some/path/to/repo" 106 branch = "branch-name" 107 commit_messages = ["Test message"] 108 mock_tempfile.return_value.__enter__.return_value.name = "tmp" 109 110 # A test CL generated by `repo upload`. 111 mock_commands.side_effect = [ 112 None, 113 ( 114 "remote: https://chromium-review.googlesource." 115 "com/c/chromiumos/overlays/chromiumos-overlay/" 116 "+/193147 Fix stdout" 117 ), 118 ] 119 change_list = git.UploadChanges(path_to_repo, branch, commit_messages) 120 121 self.assertEqual(change_list.cl_number, 193147) 122 123 mock_isdir.assert_called_once_with(path_to_repo) 124 125 expected_command = [ 126 "git", 127 "commit", 128 "-F", 129 mock_tempfile.return_value.__enter__.return_value.name, 130 ] 131 self.assertEqual( 132 mock_commands.call_args_list[0], 133 mock.call(expected_command, cwd=path_to_repo), 134 ) 135 136 expected_cmd = [ 137 "repo", 138 "upload", 139 "--yes", 140 "--ne", 141 "--no-verify", 142 "--br=%s" % branch, 143 ] 144 self.assertEqual( 145 mock_commands.call_args_list[1], 146 mock.call( 147 expected_cmd, 148 stderr=subprocess.STDOUT, 149 cwd=path_to_repo, 150 encoding="utf-8", 151 ), 152 ) 153 154 self.assertEqual( 155 change_list.url, 156 "https://chromium-review.googlesource.com/c/chromiumos/overlays/" 157 "chromiumos-overlay/+/193147", 158 ) 159 160 161if __name__ == "__main__": 162 unittest.main() 163