@@ -680,6 +680,77 @@ def test_reset_reset_failure(mock_repo):
680
680
repo .branch .hard_reset (refresh = False )
681
681
682
682
683
+ def test_reset_to_ref_with_checkout (mock_repo ):
684
+ """
685
+ GIVEN GitRepo is initialized with a path and repo
686
+ WHEN reset is called with checkout
687
+ THEN repo.head.reset is called
688
+ AND repo.checkout is called once
689
+ """
690
+ repo = GitRepo (repo = mock_repo )
691
+ with patch ('git.repo.fun.name_to_object' ):
692
+ repo .branch .hard_reset_to_ref ("main" , "origin/main" , checkout = True )
693
+
694
+ assert mock_repo .head .reset .called is True
695
+ assert mock_repo .git .checkout .call_count == 1
696
+
697
+
698
+ def test_reset_to_ref_detached_head_with_checkout (mock_repo , monkeypatch ):
699
+ """
700
+ GIVEN GitRepo is initialized with a path and repo
701
+ WHEN reset is called with checkout
702
+ AND the current HEAD is detached
703
+ THEN repo.head.reset is called
704
+ AND repo.checkout is called once
705
+ """
706
+ class MockRef :
707
+ @property
708
+ def name (self ):
709
+ # Detached heads don't have a name
710
+ raise TypeError
711
+
712
+ repo = GitRepo (repo = mock_repo )
713
+ with patch ('git.repo.fun.name_to_object' ):
714
+ mock_repo .head .ref = MockRef ()
715
+ repo .branch .hard_reset_to_ref ("main" , "origin/main" , checkout = True )
716
+
717
+ assert mock_repo .head .reset .called is True
718
+ assert mock_repo .git .checkout .call_count == 1
719
+
720
+
721
+ def test_reset_to_ref_without_checkout (mock_repo ):
722
+ """
723
+ GIVEN GitRepo is initialized with a path and repo
724
+ WHEN reset_to_ref is called with checkout False
725
+ THEN repo.head.reset is called
726
+ AND repo.checkout is called twice to return to the original state
727
+ """
728
+ repo = GitRepo (repo = mock_repo )
729
+ with patch ('git.repo.fun.name_to_object' ):
730
+ repo .branch .hard_reset_to_ref ("main" , "origin/main" , checkout = False )
731
+
732
+ assert mock_repo .head .reset .called is True
733
+ assert mock_repo .git .checkout .call_count == 2
734
+
735
+
736
+ def test_reset_to_ref_without_checkout_fails (mock_repo ):
737
+ """
738
+ GIVEN GitRepo is initialized with a path and repo
739
+ WHEN reset_to_ref is called with checkout False
740
+ AND switching back fails
741
+ THEN checkoutException is raised
742
+ """
743
+ mock_repo .git .checkout .side_effect = [None , git .GitCommandError ('checkout' , '' )]
744
+
745
+ repo = GitRepo (repo = mock_repo )
746
+ with patch ('git.repo.fun.name_to_object' ):
747
+ with pytest .raises (exceptions .CheckoutException ):
748
+ repo .branch .hard_reset_to_ref ("main" , "origin/main" , checkout = False )
749
+
750
+ assert mock_repo .head .reset .called is True
751
+ assert mock_repo .git .checkout .call_count == 2
752
+
753
+
683
754
def test_local_branch_exists (mock_repo ):
684
755
"""
685
756
GIVEN GitRepo is initialized with a path and repo
@@ -751,12 +822,29 @@ def test_create_branch(mock_repo):
751
822
GIVEN GitRepo is initialized with a path and repo
752
823
WHEN branch.create is called with a valid name and start_ref
753
824
THEN git.branch is called
825
+ AND git.checkout is not called
754
826
"""
755
827
repo = GitRepo (repo = mock_repo )
756
828
757
829
with patch ('git.repo.fun.name_to_object' ):
758
830
assert repo .branch .create ("test" , "123456" ) is True
759
831
repo .git .branch .assert_called_with ("test" , "123456" )
832
+ repo .git .checkout .assert_not_called ()
833
+
834
+
835
+ def test_create_and_checkout_branch (mock_repo ):
836
+ """
837
+ GIVEN GitRepo is initialized with a path and repo
838
+ WHEN branch.create is called with valid parameters and checkout is True
839
+ THEN git.branch is called
840
+ AND git.checkout is called
841
+ """
842
+ repo = GitRepo (repo = mock_repo )
843
+
844
+ with patch ('git.repo.fun.name_to_object' ):
845
+ assert repo .branch .create ("test" , "123456" , checkout = True ) is True
846
+ repo .git .branch .assert_called_with ("test" , "123456" )
847
+ repo .git .checkout .assert_called ()
760
848
761
849
762
850
def test_create_branch_with_bad_start_ref (mock_repo ):
@@ -786,6 +874,24 @@ def test_create_branch_already_exists(mock_repo):
786
874
with patch ('git.repo.fun.name_to_object' ):
787
875
repo .branch .create ("test" , "123456" )
788
876
assert repo .git .branch .called is False
877
+ assert repo .git .checkout .called is False
878
+
879
+
880
+ def test_create_branch_already_exists_and_check_it_out (mock_repo ):
881
+ """
882
+ GIVEN GitRepo is initialized with a path and repo
883
+ WHEN branch.create is called with valid params and checkout is True
884
+ AND the branch already exists
885
+ THEN git.branch is not called
886
+ AND git.checkout is called
887
+ """
888
+ repo = GitRepo (repo = mock_repo )
889
+ mock_repo .branches = ["test" , "master" ]
890
+
891
+ with patch ('git.repo.fun.name_to_object' ):
892
+ repo .branch .create ("test" , "123456" , checkout = True )
893
+ assert repo .git .branch .called is False
894
+ assert repo .git .checkout .called is True
789
895
790
896
791
897
def test_create_branch_already_exists_and_reset_it (mock_repo ):
0 commit comments