Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit42e89cc

Browse files
committed
RF(+BF?): refactor hooks creation in a test, and may be make it compat with windows
1 parentc352dba commit42e89cc

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

‎git/test/test_index.py

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@
5454
importos.pathasosp
5555
fromgit.cmdimportGit
5656

57+
HOOKS_SHEBANG= \
58+
"!C:/Program\ Files/Git/usr/bin/sh.exe\n"ifis_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+
ifnotosp.isdir(hpd):
67+
os.mkdir(hpd)
68+
withopen(hp,"wt")asfp:
69+
fp.write(HOOKS_SHEBANG+content)
70+
ifmake_exec:
71+
os.chmod(hp,0o744)
72+
returnhp
73+
5774

5875
classTestIndex(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
deftest_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-
ifnotosp.isdir(hpd):
840-
os.mkdir(hpd)
841-
withopen(hp,"wt")asfp:
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
deftest_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-
ifnotosp.isdir(hpd):
852-
os.mkdir(hpd)
853-
withopen(hp,"wt")asfp:
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
exceptHookExecutionErroraserr:
@@ -869,35 +882,29 @@ def test_pre_commit_hook_fail(self, rw_repo):
869882
self.assertEqual(err.stderr,"\n stderr: 'stderr\n'")
870883
assertstr(err)
871884
else:
872-
raiseAssertionError("Should havecought a HookExecutionError")
885+
raiseAssertionError("Should havecaught a HookExecutionError")
873886

874887
@with_rw_repo('HEAD',bare=True)
875888
deftest_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-
ifnotosp.isdir(hpd):
883-
os.mkdir(hpd)
884-
withopen(hp,"wt")asfp:
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
deftest_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-
ifnotosp.isdir(hpd):
897-
os.mkdir(hpd)
898-
withopen(hp,"wt")asfp:
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
exceptHookExecutionErroraserr:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp