Skip to content

Commit 07fd005

Browse files
authored
fix: Handle comments by stripping (#87)
2 parents 0304553 + c9d99e3 commit 07fd005

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

conventional_pre_commit/format.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ def r_autosquash_prefixes():
5454
return "|".join(AUTOSQUASH_PREFIXES)
5555

5656

57+
def r_comment():
58+
"""Regex str for comment"""
59+
return r"^#.*\r?\n?"
60+
61+
62+
def strip_comments(input):
63+
return re.sub(r_comment(), "", input, flags=re.MULTILINE)
64+
65+
5766
def conventional_types(types=[]):
5867
"""Return a list of Conventional Commits types merged with the given types."""
5968
if set(types) & set(CONVENTIONAL_TYPES) == set():
@@ -68,6 +77,7 @@ def is_conventional(input, types=DEFAULT_TYPES, optional_scope=True):
6877
6978
Optionally provide a list of additional custom types.
7079
"""
80+
input = strip_comments(input)
7181
types = conventional_types(types)
7282
pattern = f"^({r_types(types)}){r_scope(optional_scope)}{r_delim()}{r_subject()}{r_body()}"
7383
regex = re.compile(pattern, re.MULTILINE)

tests/test_format.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,34 @@ def test_conventional_types__custom():
123123
assert set(["custom", *format.CONVENTIONAL_TYPES]) == set(result)
124124

125125

126+
def test_r_comment_single():
127+
regex = re.compile(format.r_comment())
128+
assert regex.match("# Some comment")
129+
assert not regex.match("Some comment")
130+
assert not regex.match(" # Some comment")
131+
132+
133+
def test_strip_comments__consecutive():
134+
input = """feat(scope): message
135+
# Please enter the commit message for your changes.
136+
# These are comments usually added by editors, f.ex. with export EDITOR=vim
137+
"""
138+
result = format.strip_comments(input)
139+
assert result.count("\n") == 1
140+
assert result.strip() == "feat(scope): message"
141+
142+
143+
def test_strip_comments__spaced():
144+
input = """feat(scope): message
145+
# Please enter the commit message for your changes.
146+
147+
# These are comments usually added by editors, f.ex. with export EDITOR=vim
148+
"""
149+
result = format.strip_comments(input)
150+
assert result.count("\n") == 2
151+
assert result.strip() == "feat(scope): message"
152+
153+
126154
@pytest.mark.parametrize("type", format.DEFAULT_TYPES)
127155
def test_is_conventional__default_type(type):
128156
input = f"{type}: message"
@@ -199,6 +227,14 @@ def test_is_conventional__bad_body_multiline_paragraphs():
199227
assert not format.is_conventional(input)
200228

201229

230+
def test_is_conventional__comment():
231+
input = """feat(scope): message
232+
# Please enter the commit message for your changes.
233+
# These are comments usually added by editors, f.ex. with export EDITOR=vim
234+
"""
235+
assert format.is_conventional(input)
236+
237+
202238
@pytest.mark.parametrize("char", ['"', "'", "`", "#", "&"])
203239
def test_is_conventional__body_special_char(char):
204240
input = f"feat: message with {char}"

0 commit comments

Comments
 (0)