OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2013 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 |
| 6 """Unit tests for git_rebase_update.py""" |
| 7 |
| 8 import os |
| 9 import sys |
| 10 |
| 11 DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 12 sys.path.insert(0, DEPOT_TOOLS_ROOT) |
| 13 |
| 14 from testing_support import coverage_utils |
| 15 from testing_support import git_test_utils |
| 16 |
| 17 class GitRebaseUpdateTest(git_test_utils.GitRepoReadWriteTestBase): |
| 18 REPO_SCHEMA = """ |
| 19 A B C D E F G |
| 20 B H I J K |
| 21 J L |
| 22 """ |
| 23 |
| 24 @classmethod |
| 25 def getRepoContent(cls, commit): |
| 26 # Every commit X gets a file X with the content X |
| 27 return {commit: {'data': commit}} |
| 28 |
| 29 @classmethod |
| 30 def setUpClass(cls): |
| 31 super(GitRebaseUpdateTest, cls).setUpClass() |
| 32 import git_rebase_update, git_new_branch, git_reparent_branch, git_common |
| 33 import git_rename_branch |
| 34 cls.reup = git_rebase_update |
| 35 cls.rp = git_reparent_branch |
| 36 cls.nb = git_new_branch |
| 37 cls.mv = git_rename_branch |
| 38 cls.gc = git_common |
| 39 cls.gc.TEST_MODE = True |
| 40 |
| 41 def setUp(self): |
| 42 super(GitRebaseUpdateTest, self).setUp() |
| 43 # Include branch_K, branch_L to make sure that ABCDEFG all get the |
| 44 # same commit hashes as self.repo. Otherwise they get committed with the |
| 45 # wrong timestamps, due to commit ordering. |
| 46 # TODO(iannucci): Make commit timestamps deterministic in left to right, top |
| 47 # to bottom order, not in lexi-topographical order. |
| 48 origin_schema = git_test_utils.GitRepoSchema(""" |
| 49 A B C D E F G M N O |
| 50 B H I J K |
| 51 J L |
| 52 """, self.getRepoContent) |
| 53 self.origin = origin_schema.reify() |
| 54 self.origin.git('checkout', 'master') |
| 55 self.origin.git('branch', '-d', *['branch_'+l for l in 'KLG']) |
| 56 |
| 57 self.repo.git('remote', 'add', 'origin', self.origin.repo_path) |
| 58 self.repo.git('config', '--add', 'remote.origin.fetch', |
| 59 '+refs/tags/*:refs/tags/*') |
| 60 self.repo.git('update-ref', 'refs/remotes/origin/master', 'tag_E') |
| 61 self.repo.git('branch', '--set-upstream-to', 'branch_G', 'branch_K') |
| 62 self.repo.git('branch', '--set-upstream-to', 'branch_K', 'branch_L') |
| 63 self.repo.git('branch', '--set-upstream-to', 'origin/master', 'branch_G') |
| 64 |
| 65 self.repo.to_schema_refs += ['origin/master'] |
| 66 |
| 67 def tearDown(self): |
| 68 self.origin.nuke() |
| 69 super(GitRebaseUpdateTest, self).tearDown() |
| 70 |
| 71 def testRebaseUpdate(self): |
| 72 self.repo.git('checkout', 'branch_K') |
| 73 |
| 74 self.repo.run(self.nb.main, ['foobar']) |
| 75 self.assertEqual(self.repo.git('rev-parse', 'HEAD').stdout, |
| 76 self.repo.git('rev-parse', 'origin/master').stdout) |
| 77 |
| 78 with self.repo.open('foobar', 'w') as f: |
| 79 f.write('this is the foobar file') |
| 80 self.repo.git('add', 'foobar') |
| 81 self.repo.git_commit('foobar1') |
| 82 |
| 83 with self.repo.open('foobar', 'w') as f: |
| 84 f.write('totes the Foobar file') |
| 85 self.repo.git_commit('foobar2') |
| 86 |
| 87 self.repo.git('checkout', 'branch_K') |
| 88 self.repo.run(self.nb.main, ['--upstream_current', 'sub_K']) |
| 89 with self.repo.open('K', 'w') as f: |
| 90 f.write('This depends on K') |
| 91 self.repo.git_commit('sub_K') |
| 92 |
| 93 self.repo.run(self.nb.main, ['old_branch']) |
| 94 self.repo.git('reset', '--hard', self.repo['A']) |
| 95 with self.repo.open('old_file', 'w') as f: |
| 96 f.write('old_files we want to keep around') |
| 97 self.repo.git('add', 'old_file') |
| 98 self.repo.git_commit('old_file') |
| 99 self.repo.git('config', 'branch.old_branch.dormant', 'true') |
| 100 |
| 101 self.repo.git('checkout', 'origin/master') |
| 102 |
| 103 self.assertSchema(""" |
| 104 A B H I J K sub_K |
| 105 J L |
| 106 B C D E foobar1 foobar2 |
| 107 E F G |
| 108 A old_file |
| 109 """) |
| 110 self.assertEquals(self.repo['A'], self.origin['A']) |
| 111 self.assertEquals(self.repo['E'], self.origin['E']) |
| 112 |
| 113 with self.repo.open('bob', 'wb') as f: |
| 114 f.write('testing auto-freeze/thaw') |
| 115 |
| 116 output, _ = self.repo.capture_stdio(self.reup.main) |
| 117 self.assertIn('Cannot rebase-update', output) |
| 118 |
| 119 self.repo.git('checkout', 'branch_K') |
| 120 |
| 121 output, _ = self.repo.capture_stdio(self.reup.main) |
| 122 |
| 123 self.assertIn('Rebasing: branch_G', output) |
| 124 self.assertIn('Rebasing: branch_K', output) |
| 125 self.assertIn('Rebasing: branch_L', output) |
| 126 self.assertIn('Rebasing: foobar', output) |
| 127 self.assertIn('Rebasing: sub_K', output) |
| 128 self.assertIn('Deleted branch branch_G', output) |
| 129 self.assertIn('Reparented branch_K to track origin/master', output) |
| 130 |
| 131 self.assertSchema(""" |
| 132 A B C D E F G M N O H I J K sub_K |
| 133 K L |
| 134 O foobar1 foobar2 |
| 135 A old_file |
| 136 """) |
| 137 |
| 138 output, _ = self.repo.capture_stdio(self.reup.main) |
| 139 self.assertIn('branch_K up-to-date', output) |
| 140 self.assertIn('branch_L up-to-date', output) |
| 141 self.assertIn('foobar up-to-date', output) |
| 142 self.assertIn('sub_K up-to-date', output) |
| 143 |
| 144 with self.repo.open('bob') as f: |
| 145 self.assertEquals('testing auto-freeze/thaw', f.read()) |
| 146 |
| 147 self.assertEqual(self.repo.git('status', '--porcelain').stdout, '?? bob\n') |
| 148 |
| 149 self.repo.git('checkout', 'origin/master') |
| 150 _, err = self.repo.capture_stdio(self.rp.main, ['foobar']) |
| 151 self.assertIn('Must be on the branch', err) |
| 152 |
| 153 self.repo.git('checkout', 'branch_K') |
| 154 _, err = self.repo.capture_stdio(self.rp.main, ['origin/master']) |
| 155 self.assertIn('Cannot reparent a branch to its existing parent', err) |
| 156 output, _ = self.repo.capture_stdio(self.rp.main, ['foobar']) |
| 157 self.assertIn('Rebasing: branch_K', output) |
| 158 self.assertIn('Rebasing: sub_K', output) |
| 159 self.assertIn('Rebasing: branch_L', output) |
| 160 |
| 161 self.assertSchema(""" |
| 162 A B C D E F G M N O foobar1 foobar2 H I J K L |
| 163 K sub_K |
| 164 A old_file |
| 165 """) |
| 166 |
| 167 self.repo.git('checkout', 'sub_K') |
| 168 output, _ = self.repo.capture_stdio(self.rp.main, ['foobar']) |
| 169 self.assertIn('Squashing failed', output) |
| 170 |
| 171 self.assertTrue(self.repo.run(self.gc.in_rebase)) |
| 172 |
| 173 self.repo.git('rebase', '--abort') |
| 174 self.assertIsNone(self.repo.run(self.gc.thaw)) |
| 175 |
| 176 self.assertSchema(""" |
| 177 A B C D E F G M N O foobar1 foobar2 H I J K L |
| 178 A old_file |
| 179 K sub_K |
| 180 """) |
| 181 |
| 182 self.assertEqual(self.repo.git('status', '--porcelain').stdout, '?? bob\n') |
| 183 |
| 184 branches = self.repo.run(set, self.gc.branches()) |
| 185 self.assertEqual(branches, {'branch_K', 'master', 'sub_K', 'root_A', |
| 186 'branch_L', 'old_branch', 'foobar'}) |
| 187 |
| 188 self.repo.git('checkout', 'branch_K') |
| 189 self.repo.run(self.mv.main, ['special_K']) |
| 190 |
| 191 branches = self.repo.run(set, self.gc.branches()) |
| 192 self.assertEqual(branches, {'special_K', 'master', 'sub_K', 'root_A', |
| 193 'branch_L', 'old_branch', 'foobar'}) |
| 194 |
| 195 self.repo.git('checkout', 'origin/master') |
| 196 _, err = self.repo.capture_stdio(self.mv.main, ['special_K', 'cool branch']) |
| 197 self.assertIn('fatal: \'cool branch\' is not a valid branch name.', err) |
| 198 |
| 199 self.repo.run(self.mv.main, ['special_K', 'cool_branch']) |
| 200 branches = self.repo.run(set, self.gc.branches()) |
| 201 self.assertEqual(branches, {'cool_branch', 'master', 'sub_K', 'root_A', |
| 202 'branch_L', 'old_branch', 'foobar'}) |
| 203 |
| 204 _, branch_tree = self.repo.run(self.gc.get_branch_tree) |
| 205 self.assertEqual(branch_tree['sub_K'], 'foobar') |
| 206 |
| 207 |
| 208 def testRebaseConflicts(self): |
| 209 # Pretend that branch_L landed |
| 210 self.origin.git('checkout', 'master') |
| 211 with self.origin.open('L', 'w') as f: |
| 212 f.write('L') |
| 213 self.origin.git('add', 'L') |
| 214 self.origin.git_commit('L') |
| 215 |
| 216 # Add a commit to branch_K so that things fail |
| 217 self.repo.git('checkout', 'branch_K') |
| 218 with self.repo.open('M', 'w') as f: |
| 219 f.write('NOPE') |
| 220 self.repo.git('add', 'M') |
| 221 self.repo.git_commit('K NOPE') |
| 222 |
| 223 # Add a commits to branch_L which will work when squashed |
| 224 self.repo.git('checkout', 'branch_L') |
| 225 self.repo.git('reset', 'branch_L~') |
| 226 with self.repo.open('L', 'w') as f: |
| 227 f.write('NOPE') |
| 228 self.repo.git('add', 'L') |
| 229 self.repo.git_commit('L NOPE') |
| 230 with self.repo.open('L', 'w') as f: |
| 231 f.write('L') |
| 232 self.repo.git('add', 'L') |
| 233 self.repo.git_commit('L YUP') |
| 234 |
| 235 # start on a branch which will be deleted |
| 236 self.repo.git('checkout', 'branch_G') |
| 237 |
| 238 output, _ = self.repo.capture_stdio(self.reup.main) |
| 239 self.assertIn('branch.branch_K.dormant true', output) |
| 240 |
| 241 output, _ = self.repo.capture_stdio(self.reup.main) |
| 242 self.assertIn('Rebase in progress', output) |
| 243 |
| 244 self.repo.git('checkout', '--theirs', 'M') |
| 245 self.repo.git('rebase', '--skip') |
| 246 |
| 247 output, _ = self.repo.capture_stdio(self.reup.main) |
| 248 self.assertIn('Failed! Attempting to squash', output) |
| 249 self.assertIn('Deleted branch branch_G', output) |
| 250 self.assertIn('Deleted branch branch_L', output) |
| 251 self.assertIn('\'branch_G\' was merged', output) |
| 252 self.assertIn('checking out \'origin/master\'', output) |
| 253 |
| 254 |
| 255 def testTrackTag(self): |
| 256 self.origin.git('tag', 'lkgr', self.origin['M']) |
| 257 self.repo.git('tag', 'lkgr', self.repo['D']) |
| 258 |
| 259 self.repo.git('config', 'branch.branch_G.remote', '.') |
| 260 self.repo.git('config', 'branch.branch_G.merge', 'refs/tags/lkgr') |
| 261 |
| 262 self.assertIn( |
| 263 'fatal: \'foo bar\' is not a valid branch name', |
| 264 self.repo.capture_stdio(self.nb.main, ['--lkgr', 'foo bar'])[1]) |
| 265 |
| 266 self.repo.run(self.nb.main, ['--lkgr', 'foobar']) |
| 267 |
| 268 with self.repo.open('foobar', 'w') as f: |
| 269 f.write('this is the foobar file') |
| 270 self.repo.git('add', 'foobar') |
| 271 self.repo.git_commit('foobar1') |
| 272 |
| 273 with self.repo.open('foobar', 'w') as f: |
| 274 f.write('totes the Foobar file') |
| 275 self.repo.git_commit('foobar2') |
| 276 |
| 277 self.assertSchema(""" |
| 278 A B H I J K |
| 279 J L |
| 280 B C D E F G |
| 281 D foobar1 foobar2 |
| 282 """) |
| 283 self.assertEquals(self.repo['A'], self.origin['A']) |
| 284 self.assertEquals(self.repo['G'], self.origin['G']) |
| 285 |
| 286 output, _ = self.repo.capture_stdio(self.reup.main) |
| 287 self.assertIn('Fetching', output) |
| 288 self.assertIn('Rebasing: branch_G', output) |
| 289 self.assertIn('Rebasing: branch_K', output) |
| 290 self.assertIn('Rebasing: branch_L', output) |
| 291 self.assertIn('Rebasing: foobar', output) |
| 292 self.assertEquals(self.repo.git('rev-parse', 'lkgr').stdout.strip(), |
| 293 self.origin['M']) |
| 294 |
| 295 self.assertSchema(""" |
| 296 A B C D E F G M N O |
| 297 M H I J K L |
| 298 M foobar1 foobar2 |
| 299 """) |
| 300 |
| 301 _, err = self.repo.capture_stdio(self.rp.main, ['tag F']) |
| 302 self.assertIn('fatal: invalid reference', err) |
| 303 |
| 304 output, _ = self.repo.capture_stdio(self.rp.main, ['tag_F']) |
| 305 self.assertIn('to track tag_F [tag] (was lkgr [tag])', output) |
| 306 |
| 307 self.assertSchema(""" |
| 308 A B C D E F G M N O |
| 309 M H I J K L |
| 310 F foobar1 foobar2 |
| 311 """) |
| 312 |
| 313 output, _ = self.repo.capture_stdio(self.rp.main, ['--lkgr']) |
| 314 self.assertIn('to track lkgr [tag] (was tag_F [tag])', output) |
| 315 |
| 316 self.assertSchema(""" |
| 317 A B C D E F G M N O |
| 318 M H I J K L |
| 319 M foobar1 foobar2 |
| 320 """) |
| 321 |
| 322 output, _ = self.repo.capture_stdio(self.rp.main, ['--root']) |
| 323 self.assertIn('to track origin/master (was lkgr [tag])', output) |
| 324 |
| 325 self.assertSchema(""" |
| 326 A B C D E F G M N O foobar1 foobar2 |
| 327 M H I J K L |
| 328 """) |
| 329 |
| 330 |
| 331 if __name__ == '__main__': |
| 332 sys.exit(coverage_utils.covered_main(( |
| 333 os.path.join(DEPOT_TOOLS_ROOT, 'git_rebase_update.py'), |
| 334 os.path.join(DEPOT_TOOLS_ROOT, 'git_new_branch.py'), |
| 335 os.path.join(DEPOT_TOOLS_ROOT, 'git_reparent_branch.py'), |
| 336 os.path.join(DEPOT_TOOLS_ROOT, 'git_rename_branch.py') |
| 337 ))) |
OLD | NEW |