Repository navigation
Expand file tree
/
Copy pathtest_branches.py
More file actions
1229 lines (1034 loc) · 47.2 KB
/
Copy pathtest_branches.py
File metadata and controls
1229 lines (1034 loc) · 47.2 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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# This file is part of `gitflow`.
# Copyright (c) 2010-2011 Vincent Driessen
# Copyright (c) 2012-2013 Hartmut Goebel
# Copyright (c) 2015 Christian Assing
# Distributed under a BSD-like license. For full terms see the file LICENSE.txt
#
from unittest2 import TestCase
from git import GitCommandError
from gitflow.core import GitFlow
from gitflow.exceptions import (
NoSuchRemoteError, MergeError, BranchExistsError, BaseNotOnBranch,
BranchTypeExistsError, PrefixNotUniqueError, NoSuchBranchError,
WorkdirIsDirtyError, TagExistsError)
from gitflow.branches import (
BranchManager, FeatureBranchManager, ReleaseBranchManager,
HotfixBranchManager, SupportBranchManager)
from tests.helpers import (copy_from_fixture, remote_clone_from_fixture,
fake_commit, all_commits, set_gnupg_home)
from tests.helpers.factory import create_git_repo
__copyright__ = "2010-2011 Vincent Driessen; 2012-2013 Hartmut Goebel; 2015 Christian Assing"
__license__ = "BSD"
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'
DEFAULT_PREFIX = 'xyz/'
class TestAbstractBranchManager(TestCase):
def __prep_explicit_prefix(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
fb = DummyBranchManager(gitflow, 'xyz-')
return repo, fb
def __prep_explicit_empty_prefix(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
fb = DummyBranchManager(gitflow, '')
return repo, fb
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, fb = self.__prep_explicit_prefix()
self.assertEquals('xyz-', fb.prefix)
def test_explicit_empty_prefix(self):
repo, fb = self.__prep_explicit_empty_prefix()
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'))
def test_full_name(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
fb = DummyBranchManager(gitflow)
self.assertEquals('xyz/foo', fb.full_name('foo'))
self.assertEquals('xyz/feature/foo', fb.full_name('feature/foo'))
def test_explicit_prefix_shorten(self):
repo, fb = self.__prep_explicit_prefix()
self.assertEquals('foo', fb.shorten('xyz-foo'))
self.assertEquals('xyz/foo', fb.shorten('xyz/foo'))
self.assertEquals('feature/foo', fb.shorten('feature/foo'))
def test_explicit_empty_prefix_shorten(self):
repo, fb = self.__prep_explicit_empty_prefix()
self.assertEquals('xyz-foo', fb.shorten('xyz-foo'))
self.assertEquals('xyz/foo', fb.shorten('xyz/foo'))
self.assertEquals('feature/foo', fb.shorten('feature/foo'))
def test_explicit_prefix_full_name(self):
repo, fb = self.__prep_explicit_prefix()
self.assertEquals('xyz-foo', fb.full_name('foo'))
self.assertEquals('xyz-xyz/foo', fb.full_name('xyz/foo'))
self.assertEquals('xyz-feature/foo', fb.full_name('feature/foo'))
def test_explicit_empty_prefix_full_name(self):
repo, fb = self.__prep_explicit_empty_prefix()
self.assertEquals('foo', fb.full_name('foo'))
self.assertEquals('xyz-foo', fb.full_name('xyz-foo'))
self.assertEquals('xyz/foo', fb.full_name('xyz/foo'))
self.assertEquals('feature/foo', fb.full_name('feature/foo'))
class TestFeatureBranchManager(TestCase):
def test_defined_members(self):
# must define at least these members
members = vars(FeatureBranchManager).keys()
self.assertIn('DEFAULT_PREFIX', members)
self.assertIn('identifier', members)
def test_empty_repo_has_no_features(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo)
mgr = FeatureBranchManager(gitflow)
self.assertItemsEqual([], mgr.list())
@copy_from_fixture('sample_repo')
def test_shorten(self):
gitflow = GitFlow(self.repo)
fb = FeatureBranchManager(gitflow)
self.assertEquals('foo', fb.shorten('feat/foo'))
self.assertEquals('feature/foo', fb.shorten('feature/foo'))
@copy_from_fixture('sample_repo')
def test_full_name(self):
gitflow = GitFlow(self.repo)
fb = FeatureBranchManager(gitflow)
self.assertEquals('feat/foo', fb.full_name('foo'))
self.assertEquals('feat/feature/foo', fb.full_name('feature/foo'))
@copy_from_fixture('sample_repo')
def test_list(self):
gitflow = GitFlow()
mgr = FeatureBranchManager(gitflow)
expected = ['feat/even', 'feat/recursion']
self.assertItemsEqual(expected, [b.name for b in 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('feat/even', mgr.by_name_prefix('e').name)
self.assertEquals('feat/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')
# --- create ---
def test_create_new_feature_branch(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo).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)
self.assertEqual(new_branch.commit,
gitflow.repo.branches['develop'].commit)
@copy_from_fixture('sample_repo')
def test_create_new_feature_branch_non_default_prefix(self):
gitflow = GitFlow(self.repo).init()
mgr = FeatureBranchManager(gitflow)
new_branch = mgr.create('foo')
self.assertEqual(new_branch.name, 'feat/foo')
self.assertIn('feat/foo', [b.name for b in mgr.list()])
self.assertEqual(new_branch.commit,
gitflow.repo.branches['devel'].commit)
@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', 'feat/even')
self.assertEqual(new_branch.commit,
gitflow.repo.branches['feat/even'].commit)
def test_feature_branch_origin(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo).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_raises_error(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo).init()
mgr = FeatureBranchManager(gitflow)
mgr.create('foo')
self.assertRaises(BranchExistsError, mgr.create, 'foo')
def test_create_feature_branch_fetch_without_remote_raises_error(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo).init()
mgr = FeatureBranchManager(gitflow)
self.assertRaises(NoSuchRemoteError, mgr.create, 'foo', fetch=True)
@remote_clone_from_fixture('sample_repo')
def test_create_feature_from_remote_branch(self):
remote_branch = self.remote.refs['feat/even']
rfc0 = remote_branch.commit
gitflow = GitFlow(self.repo).init()
mgr = FeatureBranchManager(gitflow)
mgr.create('even')
branch = self.repo.active_branch
self.assertEqual(branch.name, 'feat/even')
self.assertEqual(branch.commit, rfc0)
# must be a tracking branch
self.assertTrue(branch.tracking_branch())
self.assertEqual(branch.tracking_branch().name, 'my-remote/feat/even')
@remote_clone_from_fixture('sample_repo')
def test_create_feature_from_remote_branch_with_develop_behind(self):
# If BranchManager.create() uses `update`, this test-case has
# to be adopted, since since `update` change the cloned repo.
rfc0 = self.remote.refs['feat/even'].commit
rdc0 = self.remote.refs['devel'].commit
# add a commit to remote develop branch
self.remote.refs['devel'].checkout()
change = fake_commit(self.remote, "Yet another develop commit.")
gitflow = GitFlow(self.repo).init()
mgr = FeatureBranchManager(gitflow)
mgr.create('even')
# must not advance develop nor feat/even
self.assertEqual(self.repo.refs['feat/even'].commit, rfc0)
self.assertEqual(self.repo.refs['devel'].commit, rdc0)
# change must not be in local repo
self.assertNotIn(change, all_commits(self.repo))
@remote_clone_from_fixture('sample_repo')
def test_create_feature_from_remote_branch_behind(self):
# If BranchManager.create() uses `update`, this test-case has
# to be adopted, since since `update` change the cloned repo.
rfc0 = self.remote.refs['feat/even'].commit
# add a commit to remote feat/even branch
self.remote.refs['feat/even'].checkout()
change = fake_commit(self.remote, "Yet another even commit.")
gitflow = GitFlow(self.repo).init()
mgr = FeatureBranchManager(gitflow)
mgr.create('even')
# does not advance feat/even, since create() uses `fetch`, not `update`
self.assertEqual(self.repo.refs['feat/even'].commit, rfc0)
# change must not be in local repo, since create() uses `fetch`, not `update`
self.assertNotIn(change, all_commits(self.repo))
@copy_from_fixture('sample_repo')
def test_create_feature_changes_active_branch(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
self.assertEquals('feat/recursion', self.repo.active_branch.name)
mgr.create('foo')
self.assertEquals('feat/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.assertRaisesRegexp(WorkdirIsDirtyError, 'checked in.*not committed',
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()])
# :todo: test-cases for create with base not on develop
# :todo: test-cases for create with remote base not on develop
# ---- delete ---
@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('feat/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', 'devel')
self.assertEquals(3, len(mgr.list()))
mgr.delete('foo')
self.assertEquals(2, len(mgr.list()))
self.assertNotIn('feat/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')
fake_commit(self.repo, 'A commit on the feature branch.', append=False)
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')
fake_commit(self.repo, 'A commit on the feature branch.', append=False)
gitflow.develop().checkout()
self.assertEquals(3, len(mgr.list()))
mgr.delete('foo', force=True)
self.assertEquals(2, len(mgr.list()))
self.assertNotIn('feat/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')
# --- merge ---
@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', 'devel')
dc1 = gitflow.develop().commit
# Assert merge commit has been made
self.assertEqual(2, len(dc1.parents))
self.assertTrue(dc1.message.startswith("Merge branch 'feat/even' into devel\n"))
# Assert develop branch advanced
self.assertNotEqual(dc0, dc1)
# Assert the target-branch is active
self.assertEqual(gitflow.repo.active_branch.name, 'devel')
@copy_from_fixture('sample_repo')
def test_merge_feature_with_single_commit(self):
gitflow = GitFlow(self.repo).init()
mgr = FeatureBranchManager(gitflow)
dc0 = gitflow.develop().commit
mgr.merge('recursion', 'devel')
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)
# Assert the target-branch is active
self.assertEqual(gitflow.repo.active_branch.name, 'devel')
def test_merge_feature_without_commits(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo).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)
# Assert the target-branch is active
self.assertEqual(gitflow.repo.active_branch.name, 'develop')
@copy_from_fixture('sample_repo')
def test_merge_conflict(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
mgr.merge('recursion', 'devel')
self.assertRaises(MergeError,
mgr.merge, 'even', 'devel')
gitflow.git.rm('odd.py')
gitflow.git.commit('-F.git/MERGE_MSG')
# --- finish ---
@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('feat/even',
[b.name for b in self.repo.branches])
# Merge commit message
self.assertTrue(dc1.message.startswith('Finished feature even.\n'))
@copy_from_fixture('sample_repo')
def test_finish_feature_keep(self):
gitflow = GitFlow(self.repo)
mgr = FeatureBranchManager(gitflow)
mgr.finish('even', keep=True)
# feature branch still exists
self.assertIn('feat/even',
[b.name for b in self.repo.branches])
@remote_clone_from_fixture('sample_repo')
def test_finish_feature_on_unpulled_branch_raises_error(self):
# branch exists on remote but was not pulled prior to finish
gitflow = GitFlow(self.repo).init()
mgr = FeatureBranchManager(gitflow)
self.assertRaises(NoSuchBranchError, mgr.finish, 'even', push=True)
@remote_clone_from_fixture('sample_repo')
def test_finish_feature_push(self):
remote = GitFlow(self.remote).init()
gitflow = GitFlow(self.repo).init()
rmc0 = remote.master().commit
rdc0 = remote.develop().commit
mc0 = gitflow.master().commit
dc0 = gitflow.develop().commit
mgr = FeatureBranchManager(gitflow)
mgr.create('even')
mgr.finish('even', push=True)
rmc1 = remote.master().commit
rdc1 = remote.develop().commit
mc1 = gitflow.master().commit
dc1 = gitflow.develop().commit
# Feature finishes don't advance remote master, but remote develop
self.assertEqual(rmc0, rmc1)
self.assertNotEqual(rdc0, rdc1)
self.assertEqual(mc0, mc1)
self.assertNotEqual(dc0, dc1)
# local and remote heads must be the same again
self.assertEqual(rmc1, mc1)
self.assertEqual(rdc1, dc1)
# Finishing removes the local and the remote feature branch
self.assertNotIn('feat/even',
[b.name for b in self.repo.branches])
self.assertNotIn('feat/even',
[b.name for b in self.remote.branches])
# Merge commit message
self.assertTrue(rdc1.message.startswith('Finished feature even.\n'))
@remote_clone_from_fixture('sample_repo')
def test_finish_feature_push_keep(self):
gitflow = GitFlow(self.repo).init()
mgr = FeatureBranchManager(gitflow)
mgr.create('even')
mgr.finish('even', push=True, keep=True)
# Finishing removes the local and the remote feature branch
self.assertIn('feat/even',
[b.name for b in self.repo.branches])
self.assertIn('feat/even',
[b.name for b in self.remote.branches])
@copy_from_fixture('sample_repo')
def test_finish_feature_merge_conflict(self):
gitflow = GitFlow(self.repo).init()
mgr = FeatureBranchManager(gitflow)
mgr.finish('recursion')
self.assertRaises(MergeError,
mgr.finish, 'even')
# resolve the conflict
gitflow.git.rm('odd.py')
gitflow.git.commit('-F.git/MERGE_MSG')
# the feature branch is still here
self.assertIn('feat/even',
[b.name for b in self.repo.branches])
mgr.finish('even')
# now the feature branch is gone
self.assertNotIn('feat/even',
[b.name for b in self.repo.branches])
@copy_from_fixture('sample_repo')
def test_finish_feature_unresolved_merge_conflict(self):
gitflow = GitFlow(self.repo).init()
mgr = FeatureBranchManager(gitflow)
mgr.finish('recursion')
self.assertRaises(MergeError,
mgr.finish, 'even')
# do not resolve, but finish again
self.assertRaises(GitCommandError,
mgr.finish, 'even')
# :todo: test-cases for finish with rebase
# :todo: test-cases for finish with rebase-conflicts for both develop
class TestReleaseBranchManager(TestCase):
def test_defined_members(self):
# must define at least these members
members = vars(ReleaseBranchManager).keys()
self.assertIn('DEFAULT_PREFIX', members)
self.assertIn('identifier', members)
@copy_from_fixture('sample_repo')
def test_shorten(self):
gitflow = GitFlow(self.repo)
fb = ReleaseBranchManager(gitflow)
self.assertEquals('foo', fb.shorten('rel/foo'))
self.assertEquals('release/foo', fb.shorten('release/foo'))
@copy_from_fixture('sample_repo')
def test_full_name(self):
gitflow = GitFlow(self.repo)
fb = ReleaseBranchManager(gitflow)
self.assertEquals('rel/foo', fb.full_name('foo'))
self.assertEquals('rel/release/foo', fb.full_name('release/foo'))
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 = ['rel/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()])
@copy_from_fixture('release')
def test_by_nameprefix(self):
gitflow = GitFlow()
mgr = ReleaseBranchManager(gitflow)
self.assertEquals('rel/1.0', mgr.by_name_prefix('1').name)
@copy_from_fixture('release')
def test_by_nameprefix_not_unique_enough(self):
gitflow = GitFlow()
mgr = ReleaseBranchManager(gitflow)
# Create branch without manager since manager enforces there
# is a single release branch.
self.repo.create_head('rel/1.1', 'HEAD')
self.assertRaises(PrefixNotUniqueError, mgr.by_name_prefix, '1.')
self.assertRaises(NoSuchBranchError, mgr.by_name_prefix, 'nonexisting')
# --- create ---
def test_create_new_release_branch(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo).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_branch_non_default_prefix(self):
gitflow = GitFlow(self.repo).init()
mgr = ReleaseBranchManager(gitflow)
new_branch = mgr.create('3.14-beta5')
self.assertEqual(new_branch.name, 'rel/3.14-beta5')
self.assertIn('rel/3.14-beta5', [b.name for b in mgr.list()])
self.assertEqual(new_branch.commit,
gitflow.repo.branches['devel'].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') # devel~1
self.assertEqual(new_branch.commit.hexsha,
'c8b6deac7ef94f078a426d52c0b1fb3e1221133c')
@copy_from_fixture('sample_repo')
def test_create_new_release_from_wrong_alt_base_raises_error(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
self.assertRaises(BaseNotOnBranch,
mgr.create, '1.0', 'feat/even')
@copy_from_fixture('release')
def test_create_new_release_for_existing_tag_raises_error(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
mgr.finish('1.0', tagging_info={'message': 'Tagging 1.0'})
self.assertRaises(TagExistsError,
mgr.create, '1.0')
def test_release_branch_origin(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo).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_raises_error(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo).init()
mgr = ReleaseBranchManager(gitflow)
mgr.create('1.0')
self.assertRaises(BranchTypeExistsError, mgr.create, '1.0')
def test_create_release_branch_fetch_without_remote_raises_error(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo).init()
mgr = ReleaseBranchManager(gitflow)
self.assertRaises(NoSuchRemoteError, mgr.create, 'foo', fetch=True)
@remote_clone_from_fixture('release')
def test_create_release_from_remote_branch(self):
remote_branch = self.remote.refs['rel/1.0']
rfc0 = remote_branch.commit
gitflow = GitFlow(self.repo).init()
mgr = ReleaseBranchManager(gitflow)
mgr.create('1.0')
branch = self.repo.active_branch
self.assertEqual(branch.name, 'rel/1.0')
self.assertEqual(branch.commit, rfc0)
# must be a tracking branch
self.assertTrue(branch.tracking_branch())
self.assertEqual(branch.tracking_branch().name, 'my-remote/rel/1.0')
@remote_clone_from_fixture('release')
def test_create_release_from_remote_branch_with_develop_behind(self):
# If BranchManager.create() uses `update`, this test-case has
# to be adopted, since `update` changes the cloned repo.
rfc0 = self.remote.refs['rel/1.0'].commit
rdc0 = self.remote.refs['devel'].commit
# add a commit to remote develop branch
self.remote.refs['devel'].checkout()
change = fake_commit(self.remote, "Yet another develop commit.")
gitflow = GitFlow(self.repo).init()
mgr = ReleaseBranchManager(gitflow)
mgr.create('1.0')
# must not advance develop nor rel/1.0
self.assertEqual(self.repo.refs['rel/1.0'].commit, rfc0)
self.assertEqual(self.repo.refs['devel'].commit, rdc0)
# change must not be in local repo
self.assertNotIn(change, all_commits(self.repo))
@remote_clone_from_fixture('release')
def test_create_release_from_remote_branch_behind(self):
# If BranchManager.create() uses `update`, this test-case has
# to be adopted, since since `update` change the cloned repo.
rfc0 = self.remote.refs['rel/1.0'].commit
# add a commit to remote rel/1.0 branch
self.remote.refs['rel/1.0'].checkout()
change = fake_commit(self.remote, "Yet another 1.0 commit.")
gitflow = GitFlow(self.repo).init()
mgr = ReleaseBranchManager(gitflow)
mgr.create('1.0')
# does not advance rel/1.0, since create() uses `fetch`, not `update`
self.assertEqual(self.repo.refs['rel/1.0'].commit, rfc0)
# change must not be in local repo, since create() uses `fetch`, not `update`
self.assertNotIn(change, all_commits(self.repo))
def test_create_release_changes_active_branch(self):
repo = create_git_repo(self)
gitflow = GitFlow(repo).init()
mgr = ReleaseBranchManager(gitflow)
self.assertEquals('develop', 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.assertRaisesRegexp(WorkdirIsDirtyError, 'checked in.*not committed',
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()])
# :todo: test-cases for create with base not on develop
# :todo: test-cases for create with remote base not on develop
# ---- delete ---
@copy_from_fixture('sample_repo')
def test_delete_release_without_commits(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('rel/1.0', [b.name for b in mgr.list()])
@copy_from_fixture('sample_repo')
def test_delete_already_merged_release(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
self.assertEquals(0, len(mgr.list()))
mgr.create('0.7')
fake_commit(self.repo, 'Dummy commit #1')
fake_commit(self.repo, 'Dummy commit #2')
mgr.merge('0.7', 'devel')
self.assertEquals(1, len(mgr.list()))
mgr.delete('0.7')
self.assertEquals(0, len(mgr.list()))
self.assertNotIn('rel/0.7', [b.name for b in mgr.list()])
@copy_from_fixture('sample_repo')
def test_delete_release_with_commits_raises_error(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
self.assertEquals(0, len(mgr.list()))
mgr.create('0.7')
fake_commit(self.repo, 'A commit on the release branch.', append=False)
gitflow.develop().checkout()
self.assertEquals(1, len(mgr.list()))
self.assertRaisesRegexp(GitCommandError,
'The branch .* is not fully merged',
mgr.delete, '0.7')
@copy_from_fixture('sample_repo')
def test_delete_release_with_commits_forcefully(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
self.assertEquals(0, len(mgr.list()))
mgr.create('0.7')
fake_commit(self.repo, 'A commit on the release branch.', append=False)
gitflow.develop().checkout()
self.assertEquals(1, len(mgr.list()))
mgr.delete('0.7', force=True)
self.assertEquals(0, len(mgr.list()))
self.assertNotIn('rel/0.7', [b.name for b in self.repo.branches])
@copy_from_fixture('sample_repo')
def test_delete_current_release_raises_error(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_delete_non_existing_release_raises_error(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
self.assertRaisesRegexp(GitCommandError, 'branch .* not found',
mgr.delete, 'nonexisting')
# --- merge ---
# No need to test merge for ReleaseBranchManager since it is the
# same as for FeatureBranch Manager
# --- finish ---
@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)
# master is merged back to develop
self.assertIn(mc1, dc1.parents)
# Finishing removes the release branch
self.assertNotIn('rel/1.0',
[b.name for b in self.repo.branches])
# Merge commit message
self.assertTrue(dc1.message.startswith('Finished release 1.0.\n'))
self.assertTrue(mc1.message.startswith('Finished release 1.0.\n'))
@copy_from_fixture('release')
def test_finish_release_keep(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
mgr.finish('1.0', keep=True)
# release branch still exists
self.assertIn('rel/1.0',
[b.name for b in self.repo.branches])
@copy_from_fixture('release')
def test_finish_release_tag(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
taginfo = dict(
message='Tagging version 1.0'
)
mgr.finish('1.0', tagging_info=taginfo)
mc1 = gitflow.master().commit
dc1 = gitflow.develop().commit
# master is merged back to develop
self.assertIn(mc1, dc1.parents)
# tag exists
self.assertIn('v1.0', self.repo.tags)
self.assertEqual(self.repo.tags['v1.0'].commit, mc1)
# tag message
self.assertEqual(self.repo.tags['v1.0'].tag.message,
'Tagging version 1.0')
@set_gnupg_home
@copy_from_fixture('release')
def test_finish_release_tag_sign(self):
gitflow = GitFlow(self.repo)
mgr = ReleaseBranchManager(gitflow)
taginfo = dict(
message='Tagging version 1.0',
signingkey='Dummy Key for Gitflow testing',
)
mgr.finish('1.0', tagging_info=taginfo)
# tag message
tag = self.repo.tags['v1.0'].tag
self.assertIn('-----BEGIN PGP SIGNATURE-----', tag.message)
@remote_clone_from_fixture('release')
def test_finish_release_on_unpulled_branch_raises_error(self):
# branch exists on remote but was not pulled prior to finish
gitflow = GitFlow(self.repo).init()
mgr = ReleaseBranchManager(gitflow)
self.assertRaises(NoSuchBranchError, mgr.finish, '1.0', push=True)
@remote_clone_from_fixture('release')
def test_finish_release_push(self):
remote = GitFlow(self.remote).init()
# Since remote is no bare repo, checkout some branch untouched
# by this operation. :fixme: find better solution
self.remote.heads['feat/even'].checkout()
gitflow = GitFlow(self.repo).init()
rmc0 = remote.master().commit
rdc0 = remote.develop().commit
mc0 = gitflow.master().commit
dc0 = gitflow.develop().commit
mgr = ReleaseBranchManager(gitflow)
mgr.create('1.0')
mgr.finish('1.0', push=True)
rmc1 = remote.master().commit
rdc1 = remote.develop().commit
mc1 = gitflow.master().commit
dc1 = gitflow.develop().commit
# Release finishes advances master and develop both local and remote
self.assertNotEqual(rmc0, rmc1)
self.assertNotEqual(rdc0, rdc1)
self.assertNotEqual(mc0, mc1)
self.assertNotEqual(dc0, dc1)
# local and remote heads must be the same again
self.assertEqual(rmc1, mc1)
self.assertEqual(rdc1, dc1)
# Finishing removes the local and the remote release branch
self.assertNotIn('rel/1.0',
[b.name for b in self.repo.branches])
self.assertNotIn('rel/1.0',
[b.name for b in self.remote.branches])
# Merge commit message
self.assertTrue(rdc1.message.startswith('Finished release 1.0.\n'))
@remote_clone_from_fixture('release')
def test_finish_release_push_keep(self):
# Since remote is no bare repo, checkout some branch untouched
# by this operation. :fixme: find better solution
self.remote.heads['feat/even'].checkout()
gitflow = GitFlow(self.repo).init()
mgr = ReleaseBranchManager(gitflow)
mgr.create('1.0')
mgr.finish('1.0', push=True, keep=True)
# release branch still exists local and remote
self.assertIn('rel/1.0',
[b.name for b in self.repo.branches])
self.assertIn('rel/1.0',
[b.name for b in self.remote.branches])
@remote_clone_from_fixture('release')
def test_finish_release_tag_push(self):
# Since remote is no bare repo, checkout some branch untouched
# by this operation. :fixme: find better solution
self.remote.heads['feat/even'].checkout()
gitflow = GitFlow(self.repo).init()
mgr = ReleaseBranchManager(gitflow)
mgr.create('1.0')
taginfo = dict(
message='Tagging version 1.0'
)
mgr.finish('1.0', push=True, tagging_info=taginfo)
mc1 = gitflow.master().commit
# remote tag exists
self.assertIn('v1.0', self.remote.tags)