Skip to content

Commit 42e89cc

Browse files
committedNov 28, 2017
RF(+BF?): refactor hooks creation in a test, and may be make it compat with windows
1 parent c352dba commit 42e89cc

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed
 

‎git/test/test_index.py

+39-32
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@
5454
import os.path as osp
5555
from git.cmd import Git
5656

57+
HOOKS_SHEBANG = \
58+
"!C:/Program\ Files/Git/usr/bin/sh.exe\n" if is_win \
59+
else "#!/usr/bin/env sh\n"
60+
61+
62+
def _make_hook(git_dir, name, content, make_exec=True):
63+
"""A helper to create a hook"""
64+
hp = hook_path(name, git_dir)
65+
hpd = osp.dirname(hp)
66+
if not osp.isdir(hpd):
67+
os.mkdir(hpd)
68+
with open(hp, "wt") as fp:
69+
fp.write(HOOKS_SHEBANG + content)
70+
if make_exec:
71+
os.chmod(hp, 0o744)
72+
return hp
73+
5774

5875
class TestIndex(TestBase):
5976

@@ -834,25 +851,21 @@ def test_add_a_file_with_wildcard_chars(self, rw_dir):
834851
@with_rw_repo('HEAD', bare=True)
835852
def test_pre_commit_hook_success(self, rw_repo):
836853
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\nexit 0")
843-
os.chmod(hp, 0o744)
854+
_make_hook(
855+
index.repo.git_dir,
856+
'pre-commit',
857+
"exit 0"
858+
)
844859
index.commit("This should not fail")
845860

846861
@with_rw_repo('HEAD', bare=True)
847862
def test_pre_commit_hook_fail(self, rw_repo):
848863
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\necho stdout; echo stderr 1>&2; exit 1")
855-
os.chmod(hp, 0o744)
864+
hp = _make_hook(
865+
index.repo.git_dir,
866+
'pre-commit',
867+
"echo stdout; echo stderr 1>&2; exit 1"
868+
)
856869
try:
857870
index.commit("This should fail")
858871
except HookExecutionError as err:
@@ -869,35 +882,29 @@ def test_pre_commit_hook_fail(self, rw_repo):
869882
self.assertEqual(err.stderr, "\n stderr: 'stderr\n'")
870883
assert str(err)
871884
else:
872-
raise AssertionError("Should have cought a HookExecutionError")
885+
raise AssertionError("Should have caught a HookExecutionError")
873886

874887
@with_rw_repo('HEAD', bare=True)
875888
def test_commit_msg_hook_success(self, rw_repo):
876-
index = rw_repo.index
877889
commit_message = u"commit default head by Frèderic Çaufl€"
878890
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\necho -n " {0}" >> "$1"'.format(from_hook_message))
886-
os.chmod(hp, 0o744)
887-
891+
index = rw_repo.index
892+
_make_hook(
893+
index.repo.git_dir,
894+
'commit-msg',
895+
'echo -n " {0}" >> "$1"'.format(from_hook_message)
896+
)
888897
new_commit = index.commit(commit_message)
889898
self.assertEqual(new_commit.message, u"{0} {1}".format(commit_message, from_hook_message))
890899

891900
@with_rw_repo('HEAD', bare=True)
892901
def test_commit_msg_hook_fail(self, rw_repo):
893902
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\necho stdout; echo stderr 1>&2; exit 1")
900-
os.chmod(hp, 0o744)
903+
hp = _make_hook(
904+
index.repo.git_dir,
905+
'commit-msg',
906+
"echo stdout; echo stderr 1>&2; exit 1"
907+
)
901908
try:
902909
index.commit("This should fail")
903910
except HookExecutionError as err:

0 commit comments

Comments
 (0)
Please sign in to comment.