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 def setUp(self): |
| 23 super(GitDroverTest, self).setUp() |
| 24 self._temp_directory = tempfile.mkdtemp() |
| 25 self._parent_repo = os.path.join(self._temp_directory, 'parent_repo') |
| 26 self._target_repo = os.path.join(self._temp_directory, 'drover_branch_123') |
| 27 os.makedirs(os.path.join(self._parent_repo, '.git')) |
| 28 with open(os.path.join(self._parent_repo, '.git', 'config'), 'w') as f: |
| 29 f.write('config') |
| 30 with open(os.path.join(self._parent_repo, '.git', 'HEAD'), 'w') as f: |
| 31 f.write('HEAD') |
| 32 os.mkdir(os.path.join(self._parent_repo, '.git', 'info')) |
| 33 with open(os.path.join(self._parent_repo, '.git', 'info', 'refs'), |
| 34 'w') as f: |
| 35 f.write('refs') |
| 36 self.mock(tempfile, 'mkdtemp', self._mkdtemp) |
| 37 self.mock(__builtins__, 'raw_input', self._get_input) |
| 38 self.mock(subprocess, 'check_call', self._check_call) |
| 39 self.mock(subprocess, 'check_output', self._check_call) |
| 40 self._commands = [] |
| 41 self._input = [] |
| 42 self._fail_on_command = None |
| 43 self._has_os_symlink = hasattr(os, 'symlink') |
| 44 if not self._has_os_symlink: |
| 45 os.symlink = lambda source, dest: None |
| 46 |
| 47 self.REPO_CHECK_COMMANDS = [ |
| 48 (['git', '--help'], self._parent_repo), |
| 49 (['git', 'status'], self._parent_repo), |
| 50 (['git', 'fetch', 'origin'], self._parent_repo), |
| 51 (['git', 'rev-parse', 'refs/remotes/branch-heads/branch^{commit}'], |
| 52 self._parent_repo), |
| 53 (['git', 'rev-parse', 'cl^{commit}'], self._parent_repo), |
| 54 (['git', 'show', '-s', 'cl'], self._parent_repo), |
| 55 ] |
| 56 self.LOCAL_REPO_COMMANDS = [ |
| 57 (['git', 'rev-parse', '--git-dir'], self._parent_repo), |
| 58 (['git', 'config', 'core.sparsecheckout', 'true'], self._target_repo), |
| 59 (['git', 'checkout', '-b', 'drover_branch_123', |
| 60 'refs/remotes/branch-heads/branch'], self._target_repo), |
| 61 (['git', 'cherry-pick', '-x', 'cl'], self._target_repo), |
| 62 (['git', 'reset', '--hard'], self._target_repo), |
| 63 ] |
| 64 self.UPLOAD_COMMAND = [(['git', 'cl', 'upload'], self._target_repo)] |
| 65 self.LAND_COMMAND = [ |
| 66 (['git', 'cl', 'land', '--bypass-hooks'], self._target_repo), |
| 67 ] |
| 68 self.BRANCH_CLEANUP_COMMANDS = [ |
| 69 (['git', 'cherry-pick', '--abort'], self._target_repo), |
| 70 (['git', 'checkout', '--detach'], self._target_repo), |
| 71 (['git', 'branch', '-D', 'drover_branch_123'], self._target_repo), |
| 72 ] |
| 73 |
| 74 def tearDown(self): |
| 75 shutil.rmtree(self._temp_directory) |
| 76 if not self._has_os_symlink: |
| 77 del os.symlink |
| 78 super(GitDroverTest, self).tearDown() |
| 79 |
| 80 def _mkdtemp(self, prefix='tmp'): |
| 81 self.assertEqual('drover_branch_', prefix) |
| 82 os.mkdir(self._target_repo) |
| 83 return self._target_repo |
| 84 |
| 85 def _get_input(self, message): |
| 86 result = self._input.pop(0) |
| 87 if result == 'EOF': |
| 88 raise EOFError |
| 89 return result |
| 90 |
| 91 def _check_call(self, args, stderr=None, stdout=None, shell='', cwd=None): |
| 92 self.assertFalse(shell) |
| 93 self._commands.append((args, cwd)) |
| 94 if (self._fail_on_command is not None and |
| 95 self._fail_on_command == len(self._commands)): |
| 96 raise subprocess.CalledProcessError(1, args[0]) |
| 97 if args == ['git', 'rev-parse', '--git-dir']: |
| 98 return os.path.join(self._parent_repo, '.git') |
| 99 |
| 100 def testSuccess(self): |
| 101 self._input = ['y', 'y'] |
| 102 git_drover.cherry_pick_change('branch', 'cl', self._parent_repo, False) |
| 103 self.assertEqual( |
| 104 self.REPO_CHECK_COMMANDS + self.LOCAL_REPO_COMMANDS + |
| 105 self.UPLOAD_COMMAND + self.LAND_COMMAND + self.BRANCH_CLEANUP_COMMANDS, |
| 106 self._commands) |
| 107 self.assertFalse(os.path.exists(self._target_repo)) |
| 108 self.assertFalse(self._input) |
| 109 |
| 110 def testDryRun(self): |
| 111 self._input = ['y'] |
| 112 git_drover.cherry_pick_change('branch', 'cl', self._parent_repo, True) |
| 113 self.assertEqual( |
| 114 self.REPO_CHECK_COMMANDS + self.LOCAL_REPO_COMMANDS + |
| 115 self.BRANCH_CLEANUP_COMMANDS, self._commands) |
| 116 self.assertFalse(os.path.exists(self._target_repo)) |
| 117 self.assertFalse(self._input) |
| 118 |
| 119 def testCancelEarly(self): |
| 120 self._input = ['n'] |
| 121 git_drover.cherry_pick_change('branch', 'cl', self._parent_repo, False) |
| 122 self.assertEqual(self.REPO_CHECK_COMMANDS, self._commands) |
| 123 self.assertFalse(os.path.exists(self._target_repo)) |
| 124 self.assertFalse(self._input) |
| 125 |
| 126 def testEOFOnConfirm(self): |
| 127 self._input = ['EOF'] |
| 128 git_drover.cherry_pick_change('branch', 'cl', self._parent_repo, False) |
| 129 self.assertEqual(self.REPO_CHECK_COMMANDS, self._commands) |
| 130 self.assertFalse(os.path.exists(self._target_repo)) |
| 131 self.assertFalse(self._input) |
| 132 |
| 133 def testCancelLate(self): |
| 134 self._input = ['y', 'n'] |
| 135 git_drover.cherry_pick_change('branch', 'cl', self._parent_repo, False) |
| 136 self.assertEqual(self.REPO_CHECK_COMMANDS + self.LOCAL_REPO_COMMANDS + |
| 137 self.UPLOAD_COMMAND + self.BRANCH_CLEANUP_COMMANDS, |
| 138 self._commands) |
| 139 self.assertFalse(os.path.exists(self._target_repo)) |
| 140 self.assertFalse(self._input) |
| 141 |
| 142 def testFailDuringCheck(self): |
| 143 self._input = [] |
| 144 self._fail_on_command = 1 |
| 145 self.assertRaises(git_drover.Error, git_drover.cherry_pick_change, 'branch', |
| 146 'cl', self._parent_repo, False) |
| 147 self.assertEqual(self.REPO_CHECK_COMMANDS[:1], self._commands) |
| 148 self.assertFalse(os.path.exists(self._target_repo)) |
| 149 self.assertFalse(self._input) |
| 150 |
| 151 def testFailDuringBranchCreation(self): |
| 152 self._input = ['y'] |
| 153 self._fail_on_command = 8 |
| 154 self.assertRaises(git_drover.Error, git_drover.cherry_pick_change, 'branch', |
| 155 'cl', self._parent_repo, False) |
| 156 self.assertEqual(self.REPO_CHECK_COMMANDS + self.LOCAL_REPO_COMMANDS[:2], |
| 157 self._commands) |
| 158 self.assertFalse(os.path.exists(self._target_repo)) |
| 159 self.assertFalse(self._input) |
| 160 |
| 161 def testFailDuringCherryPick(self): |
| 162 self._input = ['y'] |
| 163 self._fail_on_command = 10 |
| 164 self.assertRaises(git_drover.Error, git_drover.cherry_pick_change, 'branch', |
| 165 'cl', self._parent_repo, False) |
| 166 self.assertEqual( |
| 167 self.REPO_CHECK_COMMANDS + self.LOCAL_REPO_COMMANDS[:4] + |
| 168 self.BRANCH_CLEANUP_COMMANDS, self._commands) |
| 169 self.assertFalse(os.path.exists(self._target_repo)) |
| 170 self.assertFalse(self._input) |
| 171 |
| 172 def testFailAfterCherryPick(self): |
| 173 self._input = ['y'] |
| 174 self._fail_on_command = 11 |
| 175 self.assertRaises(git_drover.Error, git_drover.cherry_pick_change, 'branch', |
| 176 'cl', self._parent_repo, False) |
| 177 self.assertEqual(self.REPO_CHECK_COMMANDS + self.LOCAL_REPO_COMMANDS + |
| 178 self.BRANCH_CLEANUP_COMMANDS, self._commands) |
| 179 self.assertFalse(os.path.exists(self._target_repo)) |
| 180 self.assertFalse(self._input) |
| 181 |
| 182 def testFailOnUpload(self): |
| 183 self._input = ['y'] |
| 184 self._fail_on_command = 12 |
| 185 self.assertRaises(git_drover.Error, git_drover.cherry_pick_change, 'branch', |
| 186 'cl', self._parent_repo, False) |
| 187 self.assertEqual(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 |
| 193 def testInvalidParentRepoDirectory(self): |
| 194 self.assertRaises( |
| 195 git_drover.Error, git_drover.cherry_pick_change, 'branch', 'cl', |
| 196 os.path.join(self._parent_repo, 'fake'), False) |
| 197 self.assertFalse(self._commands) |
| 198 self.assertFalse(os.path.exists(self._target_repo)) |
| 199 self.assertFalse(self._input) |
| 200 |
| 201 |
| 202 if __name__ == '__main__': |
| 203 unittest.main() |
OLD | NEW |