File tree Expand file tree Collapse file tree 2 files changed +32
-7
lines changed Expand file tree Collapse file tree 2 files changed +32
-7
lines changed Original file line number Diff line number Diff line change @@ -14,14 +14,17 @@ def check_commit_msg(checks: list) -> int:
14
14
f"{ YELLOW } Not found regex for commit message. skip checking.{ RESET_COLOR } " ,
15
15
)
16
16
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
19
20
git_dir = cmd_output (['git' , 'rev-parse' , '--git-dir' ]).strip ()
20
21
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" ))
25
28
result = re .match (check ['regex' ], commit_msg )
26
29
if result is None :
27
30
print_error_message (
Original file line number Diff line number Diff line change
1
+ import os
1
2
from commit_check import PASS , FAIL
2
3
from commit_check .commit import check_commit_msg
3
4
8
9
9
10
10
11
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 ):
12
33
# Must call get_commits_info, re.match.
13
34
checks = [{
14
35
"check" : "message" ,
@@ -22,6 +43,7 @@ def test_check_commit(self, mocker):
22
43
"re.match" ,
23
44
return_value = "fake_rematch_resp"
24
45
)
46
+ os .environ ["IS_PRE_COMMIT" ] = "1"
25
47
retval = check_commit_msg (checks )
26
48
assert retval == PASS
27
49
assert m_get_commits_info .call_count == 1
You can’t perform that action at this time.
0 commit comments