@@ -729,35 +729,6 @@ def make_paths():
729
729
assert fkey not in index .entries
730
730
731
731
index .add (files , write = True )
732
- if is_win :
733
- hp = hook_path ('pre-commit' , index .repo .git_dir )
734
- hpd = osp .dirname (hp )
735
- if not osp .isdir (hpd ):
736
- os .mkdir (hpd )
737
- with open (hp , "wt" ) as fp :
738
- fp .write ("#!/usr/bin/env sh\n echo stdout; echo stderr 1>&2; exit 1" )
739
- # end
740
- os .chmod (hp , 0o744 )
741
- try :
742
- index .commit ("This should fail" )
743
- except HookExecutionError as err :
744
- if is_win :
745
- self .assertIsInstance (err .status , OSError )
746
- self .assertEqual (err .command , [hp ])
747
- self .assertEqual (err .stdout , '' )
748
- self .assertEqual (err .stderr , '' )
749
- assert str (err )
750
- else :
751
- self .assertEqual (err .status , 1 )
752
- self .assertEqual (err .command , hp )
753
- self .assertEqual (err .stdout , 'stdout\n ' )
754
- self .assertEqual (err .stderr , 'stderr\n ' )
755
- assert str (err )
756
- else :
757
- raise AssertionError ("Should have cought a HookExecutionError" )
758
- # end exception handling
759
- os .remove (hp )
760
- # end hook testing
761
732
nc = index .commit ("2 files committed" , head = False )
762
733
763
734
for fkey in keys :
@@ -859,3 +830,88 @@ def test_add_a_file_with_wildcard_chars(self, rw_dir):
859
830
r = Repo .init (rw_dir )
860
831
r .index .add ([fp ])
861
832
r .index .commit ('Added [.exe' )
833
+
834
+ @with_rw_repo ('HEAD' , bare = True )
835
+ def test_pre_commit_hook_success (self , rw_repo ):
836
+ index = rw_repo .index
837
+ hp = hook_path ('pre-commit' , index .repo .git_dir )
838
+ hpd = osp .dirname (hp )
839
+ if not osp .isdir (hpd ):
840
+ os .mkdir (hpd )
841
+ with open (hp , "wt" ) as fp :
842
+ fp .write ("#!/usr/bin/env sh\n exit 0" )
843
+ os .chmod (hp , 0o744 )
844
+ index .commit ("This should not fail" )
845
+
846
+ @with_rw_repo ('HEAD' , bare = True )
847
+ def test_pre_commit_hook_fail (self , rw_repo ):
848
+ index = rw_repo .index
849
+ hp = hook_path ('pre-commit' , index .repo .git_dir )
850
+ hpd = osp .dirname (hp )
851
+ if not osp .isdir (hpd ):
852
+ os .mkdir (hpd )
853
+ with open (hp , "wt" ) as fp :
854
+ fp .write ("#!/usr/bin/env sh\n echo stdout; echo stderr 1>&2; exit 1" )
855
+ os .chmod (hp , 0o744 )
856
+ try :
857
+ index .commit ("This should fail" )
858
+ except HookExecutionError as err :
859
+ if is_win :
860
+ self .assertIsInstance (err .status , OSError )
861
+ self .assertEqual (err .command , [hp ])
862
+ self .assertEqual (err .stdout , '' )
863
+ self .assertEqual (err .stderr , '' )
864
+ assert str (err )
865
+ else :
866
+ self .assertEqual (err .status , 1 )
867
+ self .assertEqual (err .command , [hp ])
868
+ self .assertEqual (err .stdout , "\n stdout: 'stdout\n '" )
869
+ self .assertEqual (err .stderr , "\n stderr: 'stderr\n '" )
870
+ assert str (err )
871
+ else :
872
+ raise AssertionError ("Should have cought a HookExecutionError" )
873
+
874
+ @with_rw_repo ('HEAD' , bare = True )
875
+ def test_commit_msg_hook_success (self , rw_repo ):
876
+ index = rw_repo .index
877
+ commit_message = u"commit default head by Frèderic Çaufl€"
878
+ from_hook_message = u"from commit-msg"
879
+
880
+ hp = hook_path ('commit-msg' , index .repo .git_dir )
881
+ hpd = osp .dirname (hp )
882
+ if not osp .isdir (hpd ):
883
+ os .mkdir (hpd )
884
+ with open (hp , "wt" ) as fp :
885
+ fp .write ('#!/usr/bin/env sh\n echo -n " {}" >> "$1"' .format (from_hook_message ))
886
+ os .chmod (hp , 0o744 )
887
+
888
+ new_commit = index .commit (commit_message )
889
+ self .assertEqual (new_commit .message , u"{} {}" .format (commit_message , from_hook_message ))
890
+
891
+ @with_rw_repo ('HEAD' , bare = True )
892
+ def test_commit_msg_hook_fail (self , rw_repo ):
893
+ index = rw_repo .index
894
+ hp = hook_path ('commit-msg' , index .repo .git_dir )
895
+ hpd = osp .dirname (hp )
896
+ if not osp .isdir (hpd ):
897
+ os .mkdir (hpd )
898
+ with open (hp , "wt" ) as fp :
899
+ fp .write ("#!/usr/bin/env sh\n echo stdout; echo stderr 1>&2; exit 1" )
900
+ os .chmod (hp , 0o744 )
901
+ try :
902
+ index .commit ("This should fail" )
903
+ except HookExecutionError as err :
904
+ if is_win :
905
+ self .assertIsInstance (err .status , OSError )
906
+ self .assertEqual (err .command , [hp ])
907
+ self .assertEqual (err .stdout , '' )
908
+ self .assertEqual (err .stderr , '' )
909
+ assert str (err )
910
+ else :
911
+ self .assertEqual (err .status , 1 )
912
+ self .assertEqual (err .command , [hp ])
913
+ self .assertEqual (err .stdout , "\n stdout: 'stdout\n '" )
914
+ self .assertEqual (err .stderr , "\n stderr: 'stderr\n '" )
915
+ assert str (err )
916
+ else :
917
+ raise AssertionError ("Should have cought a HookExecutionError" )
0 commit comments