Repository navigation
Expand file tree
/
Copy pathtest_branches.py
More file actions
537 lines (441 loc) · 18.9 KB
/
Copy pathtest_branches.py
File metadata and controls
537 lines (441 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
from unittest2 import TestCase
from git import GitCommandError
from gitflow.core import GitFlow
from gitflow.branches import BranchManager, FeatureBranchManager, \
ReleaseBranchManager, HotfixBranchManager, SupportBranchManager, \
PrefixNotUniqueError, NoSuchBranchError
from tests.helpers import copy_from_fixture
from tests.helpers.factory import create_git_repo
def fake_commit(repo, message):
f = open('newfile.py', 'a')
try:
f.write('This is a dummy change.\n')
finally:
f.close()
repo.index.add(['newfile.py'])
repo.index.commit(message)
class DummyBranchManager(BranchManager):
"""
A fictious Dummy branch, used to test Branch functionality, but we need
a concrete class, since Branch is an "abstract" class.
"""
identifier = 'abc'
prefix = 'xyz/'
class TestAbstractBranchManager(TestCase):
def test_default_prefix(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
fb = DummyBranchManager(gitflow)
self.assertEquals('xyz/', fb.prefix)
def test_explicit_prefix(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
fb = DummyBranchManager(gitflow, 'xyz-')
self.assertEquals('xyz-', fb.prefix)
def test_explicit_empty_prefix(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
fb = DummyBranchManager(gitflow, '')
self.assertEquals('', fb.prefix)
def test_shorten(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
fb = DummyBranchManager(gitflow)
self.assertEquals('foo', fb.shorten('xyz/foo'))
self.assertEquals('feature/foo', fb.shorten('feature/foo'))
class TestFeatureBranchManager(TestCase):
@copy_from_fixture('sample_repo')
def test_list(self):
gitflow = GitFlow()
mgr = FeatureBranchManager(gitflow)
expected = ['feature/even', 'feature/recursion']
self.assertItemsEqual(expected, [b.name for b in mgr.list()])
def test_list_empty_repo(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
mgr = FeatureBranchManager(gitflow)
self.assertItemsEqual([], mgr.list())
@copy_from_fixture('sample_repo')
def test_list_without_matching_prefix(self):
gitflow = GitFlow()
mgr = FeatureBranchManager(gitflow, 'fb-')
expected = []
self.assertItemsEqual(expected, [b.name for b in mgr.list()])
@copy_from_fixture('sample_repo')
def test_by_nameprefix(self):
gitflow = GitFlow()
mgr = FeatureBranchManager(gitflow)
self.assertEquals('feature/even', mgr.by_name_prefix('e').name)
self.assertEquals('feature/recursion', mgr.by_name_prefix('re').name)
@copy_from_fixture('sample_repo')
def test_by_nameprefix_not_unique_enough(self):
gitflow = GitFlow()
mgr = FeatureBranchManager(gitflow)
mgr.create('rescue')
self.assertRaises(PrefixNotUniqueError, mgr.by_name_prefix, 're')
self.assertRaises(NoSuchBranchError, mgr.by_name_prefix, 'nonexisting')
def test_create_new_feature_branch(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
gitflow.init()
mgr = FeatureBranchManager(gitflow)
self.assertEqual(0, len(mgr.list()))
new_branch = mgr.create('foo')
self.assertEqual(1, len(mgr.list()))
self.assertEqual('feature/foo', mgr.list()[0].name)
@copy_from_fixture('sample_repo')
def test_create_new_feature_from_alt_base(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
new_branch = mgr.create('foo', 'feature/even')
self.assertEqual(new_branch.commit,
gitflow.repo.branches['feature/even'].commit)
def test_feature_branch_origin(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
gitflow.init()
mgr = FeatureBranchManager(gitflow)
new_branch = mgr.create('foobar')
self.assertEqual(new_branch.commit,
gitflow.repo.branches['develop'].commit)
def test_create_existing_feature_branch_yields_raises_error(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
gitflow.init()
mgr = FeatureBranchManager(gitflow)
mgr.create('foo')
self.assertRaises(GitCommandError, mgr.create, 'foo')
@copy_from_fixture('sample_repo')
def test_create_feature_changes_active_branch(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
self.assertEquals('feature/recursion', self.repo.active_branch.name)
mgr.create('foo')
self.assertEquals('feature/foo', self.repo.active_branch.name)
@copy_from_fixture('dirty_sample_repo')
def test_create_feature_raises_error_if_local_changes_would_be_overwritten(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
self.assertRaises(GitCommandError,
mgr.create, 'foo')
@copy_from_fixture('dirty_sample_repo')
def test_create_feature_changes_active_branch_even_if_dirty_but_without_conflicts(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
# In this fixture, odd.py contains changes that would be overwritten.
# Since we don't want to test this here, we revert all local changes in
# odd.py, but leave the local changes in README.txt. These changes
# won't be overwritten by the merge, so git-flow should be able to
# create a new feature branch if Git can do this
self.repo.index.reset(index=True, working_tree=True, paths=['odd.py'])
mgr.create('foo')
self.assertIn('feature/foo', [b.name for b in mgr.iter()])
@copy_from_fixture('sample_repo')
def test_delete_feature_without_commits(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
self.assertEquals(2, len(mgr.list()))
mgr.create('foo')
gitflow.develop().checkout()
self.assertEquals(3, len(mgr.list()))
mgr.delete('foo')
self.assertEquals(2, len(mgr.list()))
self.assertNotIn('feature/foo', [b.name for b in self.repo.branches])
@copy_from_fixture('sample_repo')
def test_delete_already_merged_feature(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
self.assertEquals(2, len(mgr.list()))
mgr.create('foo')
fake_commit(self.repo, 'Dummy commit #1')
fake_commit(self.repo, 'Dummy commit #2')
mgr.merge('foo', 'develop')
self.assertEquals(3, len(mgr.list()))
mgr.delete('foo')
self.assertEquals(2, len(mgr.list()))
self.assertNotIn('feature/foo', [b.name for b in mgr.list()])
@copy_from_fixture('sample_repo')
def test_delete_feature_with_commits_raises_error(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
self.assertEquals(2, len(mgr.list()))
mgr.create('foo')
f = open('newfile.py', 'w')
f.write('This is a dummy file.\n')
f.close()
self.repo.index.add(['newfile.py'])
self.repo.index.commit('A commit on the feature branch.')
gitflow.develop().checkout()
self.assertEquals(3, len(mgr.list()))
self.assertRaisesRegexp(GitCommandError,
'The branch .* is not fully merged',
mgr.delete, 'foo')
@copy_from_fixture('sample_repo')
def test_delete_feature_with_commits_forcefully(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
self.assertEquals(2, len(mgr.list()))
mgr.create('foo')
f = open('newfile.py', 'w')
f.write('This is a dummy file.\n')
f.close()
self.repo.index.add(['newfile.py'])
self.repo.index.commit('A commit on the feature branch.')
gitflow.develop().checkout()
self.assertEquals(3, len(mgr.list()))
mgr.delete('foo', force=True)
self.assertEquals(2, len(mgr.list()))
self.assertNotIn('feature/foo', [b.name for b in self.repo.branches])
@copy_from_fixture('sample_repo')
def test_delete_current_feature_raises_error(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
mgr.create('foo').checkout()
self.assertRaisesRegexp(GitCommandError,
'Cannot delete the branch .* which you are currently on',
mgr.delete, 'foo')
@copy_from_fixture('sample_repo')
def test_delete_non_existing_feature_raises_error(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
self.assertRaisesRegexp(GitCommandError, 'branch .* not found',
mgr.delete, 'nonexisting')
@copy_from_fixture('sample_repo')
def test_merge_feature_with_multiple_commits(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
dc0 = gitflow.develop().commit
mgr.merge('even', 'develop')
dc1 = gitflow.develop().commit
# Assert merge commit has been made
self.assertEqual(2, len(dc1.parents))
self.assertEqual(
"Merge branch 'feature/even' into develop\n",
dc1.message)
# Assert develop branch advanced
self.assertNotEqual(dc0, dc1)
@copy_from_fixture('sample_repo')
def test_merge_feature_with_single_commit(self):
gitflow = GitFlow(self.repo)
gitflow.init()
mgr = FeatureBranchManager(gitflow)
dc0 = gitflow.develop().commit
mgr.merge('recursion', 'develop')
dc1 = gitflow.develop().commit
# Assert no merge commit has been made
self.assertEqual(1, len(dc1.parents))
self.assertEqual('Made the definition of odd recursive.\n',
dc1.message)
# Assert develop branch advanced
self.assertNotEqual(dc0, dc1)
def test_merge_feature_without_commits(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
gitflow.init()
mgr = FeatureBranchManager(gitflow)
dc0 = gitflow.develop().commit
mgr.create('newstuff')
mgr.merge('newstuff', 'develop')
dc1 = gitflow.develop().commit
# Assert the develop tip is unchanged by the merge
self.assertEqual(dc0, dc1)
@copy_from_fixture('sample_repo')
def test_finish_feature(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
mc0 = gitflow.master().commit
dc0 = gitflow.develop().commit
mgr.finish('even')
mc1 = gitflow.master().commit
dc1 = gitflow.develop().commit
# Feature finishes don't advance master, but develop
self.assertEqual(mc0, mc1)
self.assertNotEqual(dc0, dc1)
# Finishing removes the feature branch
self.assertNotIn('feature/even',
[b.name for b in self.repo.branches])
# Merge commit message
self.assertEquals('Finished feature even.\n', dc1.message)
class TestReleaseBranchManager(TestCase):
def test_empty_repo_has_no_releases(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
mgr = ReleaseBranchManager(gitflow)
self.assertItemsEqual([], mgr.list())
@copy_from_fixture('release')
def test_list(self):
gitflow = GitFlow()
mgr = ReleaseBranchManager(gitflow)
expected = ['release/1.0']
self.assertItemsEqual(expected, [b.name for b in mgr.list()])
@copy_from_fixture('release')
def test_list_without_matching_prefix(self):
gitflow = GitFlow()
mgr = ReleaseBranchManager(gitflow, 'rel-')
expected = []
self.assertItemsEqual(expected, [b.name for b in mgr.list()])
def test_create_new_release_branch(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
gitflow.init()
mgr = ReleaseBranchManager(gitflow)
self.assertEqual(0, len(mgr.list()))
new_branch = mgr.create('3.14-beta5')
self.assertEqual(1, len(mgr.list()))
self.assertEqual('release/3.14-beta5', mgr.list()[0].name)
self.assertEqual(new_branch.commit,
gitflow.repo.branches['develop'].commit)
@copy_from_fixture('sample_repo')
def test_create_new_release_from_alt_base(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
new_branch = mgr.create('1.0',
'c8b6deac7ef94f078a426d52c0b1fb3e1221133c') # develop~1
self.assertEqual(new_branch.commit.hexsha,
'c8b6deac7ef94f078a426d52c0b1fb3e1221133c')
def test_release_branch_origin(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
gitflow.init()
mgr = ReleaseBranchManager(gitflow)
new_branch = mgr.create('1.1')
self.assertEqual(new_branch.commit,
gitflow.repo.branches['develop'].commit)
def test_create_existing_release_branch_yields_raises_error(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
gitflow.init()
mgr = ReleaseBranchManager(gitflow)
mgr.create('1.0')
self.assertRaises(GitCommandError, mgr.create, '1.0')
def test_create_release_changes_active_branch(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
gitflow.init()
mgr = ReleaseBranchManager(gitflow)
self.assertEquals('master', repo.active_branch.name)
mgr.create('1.0')
self.assertEquals('release/1.0', repo.active_branch.name)
@copy_from_fixture('dirty_sample_repo')
def test_create_release_raises_error_if_local_changes_would_be_overwritten(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
self.assertRaises(GitCommandError,
mgr.create, '1.0')
@copy_from_fixture('dirty_sample_repo')
def test_create_release_changes_active_branch_even_if_dirty_but_without_conflicts(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
# In this fixture, odd.py contains changes that would be overwritten.
# Since we don't want to test this here, we revert all local changes in
# odd.py, but leave the local changes in README.txt. These changes
# won't be overwritten by the merge, so git-flow should be able to
# create a new release branch if Git can do this
self.repo.index.reset(index=True, working_tree=True, paths=['odd.py'])
mgr.create('1.0')
self.assertIn('release/1.0', [b.name for b in mgr.iter()])
@copy_from_fixture('sample_repo')
def test_delete_release(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
self.assertEquals(0, len(mgr.list()))
mgr.create('1.0')
gitflow.develop().checkout()
self.assertEquals(1, len(mgr.list()))
mgr.delete('1.0')
self.assertEquals(0, len(mgr.list()))
self.assertNotIn('release/1.0', [b.name for b in mgr.list()])
@copy_from_fixture('sample_repo')
def test_cannot_delete_current_release(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
mgr.create('1.0').checkout()
self.assertRaisesRegexp(GitCommandError,
'Cannot delete the branch .* which you are currently on',
mgr.delete, '1.0')
@copy_from_fixture('sample_repo')
def test_cannot_delete_non_existing_release(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
self.assertRaisesRegexp(GitCommandError, 'branch .* not found',
mgr.delete, 'nonexisting')
@copy_from_fixture('release')
def test_finish_release(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
mc0 = gitflow.master().commit
dc0 = gitflow.develop().commit
mgr.finish('1.0')
mc1 = gitflow.master().commit
dc1 = gitflow.develop().commit
# Release finishes advance both master and develop
self.assertNotEqual(mc0, mc1)
self.assertNotEqual(dc0, dc1)
# Finishing removes the release branch
self.assertNotIn('release/1.0',
[b.name for b in self.repo.branches])
# Merge commit message
self.assertEquals('Finished release 1.0.\n', dc1.message)
class TestHotfixBranchManager(TestCase):
def test_create_new_hotfix_branch(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
gitflow.init()
mgr = HotfixBranchManager(gitflow)
self.assertEqual(0, len(mgr.list()))
new_branch = mgr.create('1.2.3')
self.assertEqual(1, len(mgr.list()))
self.assertEqual('hotfix/1.2.3', mgr.list()[0].name)
@copy_from_fixture('sample_repo')
def test_hotfix_branch_origin(self):
gitflow = GitFlow()
mgr = HotfixBranchManager(gitflow)
new_branch = mgr.create('3.14-beta5')
self.assertEqual(new_branch.commit,
gitflow.repo.branches['master'].commit)
@copy_from_fixture('sample_repo')
def test_finish_release(self):
gitflow = GitFlow(self.repo)
mgr = HotfixBranchManager(gitflow)
mgr.create('1.2.3')
fake_commit(self.repo, 'Bogus commit')
fake_commit(self.repo, 'Foo commit')
fake_commit(self.repo, 'Fake commit')
fake_commit(self.repo, 'Dummy commit')
mc0 = gitflow.master().commit
dc0 = gitflow.develop().commit
mgr.finish('1.2.3')
mc1 = gitflow.master().commit
dc1 = gitflow.develop().commit
# Hotfix finishes advance both master and develop
self.assertNotEqual(mc0, mc1)
self.assertNotEqual(dc0, dc1)
# Finishing removes the hotfix branch
self.assertNotIn('hotfix/1.2.3',
[b.name for b in self.repo.branches])
# Merge commit message
self.assertEquals('Finished hotfix 1.2.3.\n', dc1.message)
class TestSupportBranchManager(TestCase):
def test_create_new_support_branch(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
gitflow.init()
mgr = SupportBranchManager(gitflow)
self.assertEqual(0, len(mgr.list()))
new_branch = mgr.create('1.x')
self.assertEqual(1, len(mgr.list()))
self.assertEqual('support/1.x', mgr.list()[0].name)
@copy_from_fixture('sample_repo')
def test_support_branch_origin(self):
gitflow = GitFlow()
mgr = SupportBranchManager(gitflow)
new_branch = mgr.create('legacy')
self.assertEqual(new_branch.commit,
gitflow.repo.branches['master'].commit)
def test_support_branches_cannot_be_finished(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
gitflow.init()
mgr = SupportBranchManager(gitflow)
mgr.create('1.x')
self.assertRaises(NotImplementedError, mgr.finish, '1.x')