1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright 2020 The Chromium OS Authors. All rights reserved. 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 9from __future__ import print_function 10 11import os 12import subprocess 13import tempfile 14import unittest 15import unittest.mock as mock 16 17import git 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 mock_isdir.assert_called_once() 40 41 @mock.patch.object(os.path, 'isdir', return_value=True) 42 @mock.patch.object(subprocess, 'check_output', return_value=None) 43 def testSuccessfullyCreatedBranch(self, mock_command_output, mock_isdir): 44 path_to_repo = '/path/to/repo' 45 branch = 'branch-name' 46 47 git.CreateBranch(path_to_repo, branch) 48 49 mock_isdir.assert_called_once_with(path_to_repo) 50 51 self.assertEqual(mock_command_output.call_count, 2) 52 53 @mock.patch.object(os.path, 'isdir', return_value=False) 54 def testFailedToDeleteBranchForInvalidDirectoryPath(self, mock_isdir): 55 path_to_repo = '/invalid/path/to/repo' 56 branch = 'branch-name' 57 58 # Verify the exception is raised on an invalid repo path. 59 with self.assertRaises(ValueError) as err: 60 git.DeleteBranch(path_to_repo, branch) 61 62 self.assertEqual( 63 str(err.exception), 64 'Invalid directory path provided: %s' % path_to_repo) 65 66 mock_isdir.assert_called_once() 67 68 @mock.patch.object(os.path, 'isdir', return_value=True) 69 @mock.patch.object(subprocess, 'check_output', return_value=None) 70 def testSuccessfullyDeletedBranch(self, mock_command_output, mock_isdir): 71 path_to_repo = '/valid/path/to/repo' 72 branch = 'branch-name' 73 74 git.DeleteBranch(path_to_repo, branch) 75 76 mock_isdir.assert_called_once_with(path_to_repo) 77 78 self.assertEqual(mock_command_output.call_count, 3) 79 80 @mock.patch.object(os.path, 'isdir', return_value=False) 81 def testFailedToUploadChangesForInvalidDirectoryPath(self, mock_isdir): 82 path_to_repo = '/some/path/to/repo' 83 branch = 'update-LLVM_NEXT_HASH-a123testhash3' 84 commit_messages = ['Test message'] 85 86 # Verify exception is raised when on an invalid repo path. 87 with self.assertRaises(ValueError) as err: 88 git.UploadChanges(path_to_repo, branch, commit_messages) 89 90 self.assertEqual( 91 str(err.exception), 'Invalid path provided: %s' % path_to_repo) 92 93 mock_isdir.assert_called_once() 94 95 @mock.patch.object(os.path, 'isdir', return_value=True) 96 @mock.patch.object(subprocess, 'check_output') 97 @mock.patch.object(tempfile, 'NamedTemporaryFile') 98 def testSuccessfullyUploadedChangesForReview(self, mock_tempfile, 99 mock_commands, mock_isdir): 100 101 path_to_repo = '/some/path/to/repo' 102 branch = 'branch-name' 103 commit_messages = ['Test message'] 104 mock_tempfile.return_value.__enter__.return_value.name = 'tmp' 105 106 # A test CL generated by `repo upload`. 107 mock_commands.side_effect = [ 108 None, 109 ('remote: https://chromium-review.googlesource.' 110 'com/c/chromiumos/overlays/chromiumos-overlay/' 111 '+/193147 Fix stdout') 112 ] 113 change_list = git.UploadChanges(path_to_repo, branch, commit_messages) 114 115 self.assertEqual(change_list.cl_number, 193147) 116 117 mock_isdir.assert_called_once_with(path_to_repo) 118 119 expected_command = [ 120 'git', 'commit', '-F', 121 mock_tempfile.return_value.__enter__.return_value.name 122 ] 123 self.assertEqual(mock_commands.call_args_list[0], 124 mock.call(expected_command, cwd=path_to_repo)) 125 126 expected_cmd = [ 127 'repo', 'upload', '--yes', '--ne', '--no-verify', 128 '--br=%s' % branch 129 ] 130 self.assertEqual( 131 mock_commands.call_args_list[1], 132 mock.call( 133 expected_cmd, 134 stderr=subprocess.STDOUT, 135 cwd=path_to_repo, 136 encoding='utf-8')) 137 138 self.assertEqual( 139 change_list.url, 140 'https://chromium-review.googlesource.com/c/chromiumos/overlays/' 141 'chromiumos-overlay/+/193147') 142 143 144if __name__ == '__main__': 145 unittest.main() 146