Skip to content

Commit d08e50d

Browse files
authored
fix: #88 No file .git/COMMIT_EDITMSG (#89)
1 parent e567cf8 commit d08e50d

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

commit_check/commit.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ def check_commit_msg(checks: list) -> int:
1414
f"{YELLOW}Not found regex for commit message. skip checking.{RESET_COLOR}",
1515
)
1616
return PASS
17-
# check the message of the current commit
18-
if os.environ.get("IS_PRE_COMMIT") == "1":
17+
commit_msg = ""
18+
if os.environ.get("IS_PRE_COMMIT"):
19+
# check the message of the current commit
1920
git_dir = cmd_output(['git', 'rev-parse', '--git-dir']).strip()
2021
commit_msg_file = PurePath(git_dir, "COMMIT_EDITMSG")
21-
with open(commit_msg_file, 'r') as f:
22-
commit_msg = f.read()
23-
else: # check the message of the last commit
24-
commit_msg = str(get_commits_info("s"))
22+
try:
23+
with open(commit_msg_file, 'r') as f:
24+
commit_msg = f.read()
25+
except FileNotFoundError:
26+
# check the message of the last commit
27+
commit_msg = str(get_commits_info("s"))
2528
result = re.match(check['regex'], commit_msg)
2629
if result is None:
2730
print_error_message(

tests/commit_test.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from commit_check import PASS, FAIL
23
from commit_check.commit import check_commit_msg
34

@@ -8,7 +9,27 @@
89

910

1011
class TestCommit:
11-
def test_check_commit(self, mocker):
12+
13+
def test_check_commit_without_env(self, mocker):
14+
# Must call get_commits_info, re.match.
15+
checks = [{
16+
"check": "message",
17+
"regex": "dummy_regex"
18+
}]
19+
m_get_commits_info = mocker.patch(
20+
f"{LOCATION}.get_commits_info",
21+
return_value=FAKE_BRANCH_NAME
22+
)
23+
m_re_match = mocker.patch(
24+
"re.match",
25+
return_value="fake_rematch_resp"
26+
)
27+
retval = check_commit_msg(checks)
28+
assert retval == PASS
29+
assert m_get_commits_info.call_count == 0
30+
assert m_re_match.call_count == 1
31+
32+
def test_check_commit_with_env(self, mocker):
1233
# Must call get_commits_info, re.match.
1334
checks = [{
1435
"check": "message",
@@ -22,6 +43,7 @@ def test_check_commit(self, mocker):
2243
"re.match",
2344
return_value="fake_rematch_resp"
2445
)
46+
os.environ["IS_PRE_COMMIT"] = "1"
2547
retval = check_commit_msg(checks)
2648
assert retval == PASS
2749
assert m_get_commits_info.call_count == 1

0 commit comments

Comments
 (0)