OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 """Tests for git_drover.""" |
| 6 |
| 7 import os |
| 8 import shutil |
| 9 import subprocess |
| 10 import sys |
| 11 import tempfile |
| 12 import unittest |
| 13 |
| 14 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 15 |
| 16 from testing_support import auto_stub |
| 17 import git_drover |
| 18 |
| 19 |
| 20 class GitDroverTest(auto_stub.TestCase): |
| 21 |
| 22 REPO_CHECK_COMMANDS = [ |
| 23 ['git', '--help'], |
| 24 ['git', 'status'], |
| 25 ['git', 'fetch', 'origin'], |
| 26 ['git', 'rev-parse', 'branch-heads/branch'], |
| 27 ['git', 'rev-parse', 'cl'], |
| 28 ['git', 'show', '-s', 'cl'], |
| 29 ] |
| 30 LOCAL_REPO_COMMANDS = [ |
| 31 ['git', 'config', 'core.sparsecheckout', 'true'], |
| 32 ['git', 'checkout', '-b', 'drover_branch_123', 'branch-heads/branch'], |
| 33 ['git', 'cherry-pick', '-x', 'cl'], |
| 34 ['git', 'reset', '--hard'], |
| 35 ] |
| 36 UPLOAD_COMMAND = [['git', 'cl', 'upload']] |
| 37 LAND_COMMAND = [['git', 'cl', 'land', '--bypass-hooks']] |
| 38 ABORT_CHERRY_PICK_COMMAND = [['git', 'cherry-pick', '--abort']] |
| 39 BRANCH_CLEANUP_COMMANDS = [ |
| 40 ['git', 'checkout', '--detach'], |
| 41 ['git', 'branch', '-D', 'drover_branch_123'], |
| 42 ] |
| 43 |
| 44 def setUp(self): |
| 45 super(GitDroverTest, self).setUp() |
| 46 self._temp_directory = tempfile.mkdtemp() |
| 47 self._parent_repo = os.path.join(self._temp_directory, 'parent_repo') |
| 48 self._target_repo = os.path.join(self._temp_directory, 'drover_branch_123') |
| 49 os.makedirs(os.path.join(self._parent_repo, '.git')) |
| 50 with open(os.path.join(self._parent_repo, '.git', 'config'), 'w') as f: |
| 51 f.write('config') |
| 52 with open(os.path.join(self._parent_repo, '.git', 'HEAD'), 'w') as f: |
| 53 f.write('HEAD') |
| 54 self.mock(tempfile, 'mkdtemp', self._mkdtemp) |
| 55 self.mock(__builtins__, 'raw_input', self._get_input) |
| 56 self.mock(subprocess, 'check_call', self._check_call) |
| 57 self._commands = [] |
| 58 self._input = [] |
| 59 self._fail_on_command = None |
| 60 self._cwd = os.getcwd() |
| 61 self._has_os_symlink = hasattr(os, 'symlink') |
| 62 if not self._has_os_symlink: |
| 63 os.symlink = lambda source, dest: None |
| 64 |
| 65 def tearDown(self): |
| 66 os.chdir(self._cwd) |
| 67 shutil.rmtree(self._temp_directory) |
| 68 if not self._has_os_symlink: |
| 69 del os.symlink |
| 70 super(GitDroverTest, self).tearDown() |
| 71 |
| 72 def _mkdtemp(self, prefix='tmp'): |
| 73 self.assertEquals('drover_branch_', prefix) |
| 74 os.mkdir(self._target_repo) |
| 75 return self._target_repo |
| 76 |
| 77 def _get_input(self, message): |
| 78 result = self._input.pop(0) |
| 79 if result == 'EOF': |
| 80 raise EOFError |
| 81 return result |
| 82 |
| 83 def _check_call(self, args, stderr=None, stdout=None, shell=''): |
| 84 self.assertFalse(shell) |
| 85 self._commands.append(args) |
| 86 if (self._fail_on_command is not None and |
| 87 self._fail_on_command == len(self._commands)): |
| 88 raise subprocess.CalledProcessError(1, args[0]) |
| 89 |
| 90 def testSuccess(self): |
| 91 self._input = ['y', 'y'] |
| 92 git_drover.merge_change('branch', 'cl', self._parent_repo, False) |
| 93 self.assertEquals( |
| 94 self.REPO_CHECK_COMMANDS + self.LOCAL_REPO_COMMANDS + |
| 95 self.UPLOAD_COMMAND + self.LAND_COMMAND + self.BRANCH_CLEANUP_COMMANDS, |
| 96 self._commands) |
| 97 self.assertFalse(os.path.exists(self._target_repo)) |
| 98 self.assertFalse(self._input) |
| 99 self.assertEquals(self._cwd, os.getcwd()) |
| 100 |
| 101 def testDryRun(self): |
| 102 self._input = ['y'] |
| 103 git_drover.merge_change('branch', 'cl', self._parent_repo, True) |
| 104 self.assertEquals( |
| 105 self.REPO_CHECK_COMMANDS + self.LOCAL_REPO_COMMANDS + |
| 106 self.BRANCH_CLEANUP_COMMANDS, self._commands) |
| 107 self.assertFalse(os.path.exists(self._target_repo)) |
| 108 self.assertFalse(self._input) |
| 109 self.assertEquals(self._cwd, os.getcwd()) |
| 110 |
| 111 def testCancelEarly(self): |
| 112 self._input = ['n'] |
| 113 git_drover.merge_change('branch', 'cl', self._parent_repo, False) |
| 114 self.assertEquals(self.REPO_CHECK_COMMANDS, self._commands) |
| 115 self.assertFalse(os.path.exists(self._target_repo)) |
| 116 self.assertFalse(self._input) |
| 117 self.assertEquals(self._cwd, os.getcwd()) |
| 118 |
| 119 def testEOFOnConfirm(self): |
| 120 self._input = ['EOF'] |
| 121 git_drover.merge_change('branch', 'cl', self._parent_repo, False) |
| 122 self.assertEquals(self.REPO_CHECK_COMMANDS, self._commands) |
| 123 self.assertFalse(os.path.exists(self._target_repo)) |
| 124 self.assertFalse(self._input) |
| 125 self.assertEquals(self._cwd, os.getcwd()) |
| 126 |
| 127 def testCancelLate(self): |
| 128 self._input = ['y', 'n'] |
| 129 git_drover.merge_change('branch', 'cl', self._parent_repo, False) |
| 130 self.assertEquals(self.REPO_CHECK_COMMANDS + self.LOCAL_REPO_COMMANDS + |
| 131 self.UPLOAD_COMMAND + self.BRANCH_CLEANUP_COMMANDS, |
| 132 self._commands) |
| 133 self.assertFalse(os.path.exists(self._target_repo)) |
| 134 self.assertFalse(self._input) |
| 135 self.assertEquals(self._cwd, os.getcwd()) |
| 136 |
| 137 def testFailDuringCheck(self): |
| 138 self._input = [] |
| 139 self._fail_on_command = 1 |
| 140 self.assertRaises(git_drover.Error, git_drover.merge_change, 'branch', 'cl', |
| 141 self._parent_repo, False) |
| 142 self.assertEquals(self.REPO_CHECK_COMMANDS[:1], self._commands) |
| 143 self.assertFalse(os.path.exists(self._target_repo)) |
| 144 self.assertFalse(self._input) |
| 145 self.assertEquals(self._cwd, os.getcwd()) |
| 146 |
| 147 def testFailDuringBranchCreation(self): |
| 148 self._input = ['y'] |
| 149 self._fail_on_command = 8 |
| 150 self.assertRaises(git_drover.Error, git_drover.merge_change, 'branch', 'cl', |
| 151 self._parent_repo, False) |
| 152 self.assertEquals(self.REPO_CHECK_COMMANDS + self.LOCAL_REPO_COMMANDS[:2], |
| 153 self._commands) |
| 154 self.assertFalse(os.path.exists(self._target_repo)) |
| 155 self.assertFalse(self._input) |
| 156 self.assertEquals(self._cwd, os.getcwd()) |
| 157 |
| 158 def testFailDuringCherryPick(self): |
| 159 self._input = ['y'] |
| 160 self._fail_on_command = 9 |
| 161 self.assertRaises(git_drover.Error, git_drover.merge_change, 'branch', 'cl', |
| 162 self._parent_repo, False) |
| 163 self.assertEquals( |
| 164 self.REPO_CHECK_COMMANDS + self.LOCAL_REPO_COMMANDS[:3] + |
| 165 self.ABORT_CHERRY_PICK_COMMAND + self.BRANCH_CLEANUP_COMMANDS, |
| 166 self._commands) |
| 167 self.assertFalse(os.path.exists(self._target_repo)) |
| 168 self.assertFalse(self._input) |
| 169 self.assertEquals(self._cwd, os.getcwd()) |
| 170 |
| 171 def testFailAfterCherryPick(self): |
| 172 self._input = ['y'] |
| 173 self._fail_on_command = 10 |
| 174 self.assertRaises(git_drover.Error, git_drover.merge_change, 'branch', 'cl', |
| 175 self._parent_repo, False) |
| 176 self.assertEquals(self.REPO_CHECK_COMMANDS + self.LOCAL_REPO_COMMANDS + |
| 177 self.BRANCH_CLEANUP_COMMANDS, self._commands) |
| 178 self.assertFalse(os.path.exists(self._target_repo)) |
| 179 self.assertFalse(self._input) |
| 180 self.assertEquals(self._cwd, os.getcwd()) |
| 181 |
| 182 def testFailOnUpload(self): |
| 183 self._input = ['y'] |
| 184 self._fail_on_command = 11 |
| 185 self.assertRaises(git_drover.Error, git_drover.merge_change, 'branch', 'cl', |
| 186 self._parent_repo, False) |
| 187 self.assertEquals(self.REPO_CHECK_COMMANDS + self.LOCAL_REPO_COMMANDS + |
| 188 self.UPLOAD_COMMAND + self.BRANCH_CLEANUP_COMMANDS, |
| 189 self._commands) |
| 190 self.assertFalse(os.path.exists(self._target_repo)) |
| 191 self.assertFalse(self._input) |
| 192 self.assertEquals(self._cwd, os.getcwd()) |
| 193 |
| 194 def testInvalidParentRepoDirectory(self): |
| 195 self.assertRaises( |
| 196 git_drover.Error, git_drover.merge_change, 'branch', 'cl', |
| 197 os.path.join(self._parent_repo, 'fake'), False) |
| 198 self.assertFalse(self._commands) |
| 199 self.assertFalse(os.path.exists(self._target_repo)) |
| 200 self.assertFalse(self._input) |
| 201 self.assertEquals(self._cwd, os.getcwd()) |
| 202 |
| 203 |
| 204 if __name__ == '__main__': |
| 205 unittest.main() |
OLD | NEW |