Skip to content

Commit

Permalink
fix: Handle comments by stripping (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
thekaveman authored Jan 12, 2024
2 parents 0304553 + c9d99e3 commit 07fd005
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions conventional_pre_commit/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ def r_autosquash_prefixes():
return "|".join(AUTOSQUASH_PREFIXES)


def r_comment():
"""Regex str for comment"""
return r"^#.*\r?\n?"


def strip_comments(input):
return re.sub(r_comment(), "", input, flags=re.MULTILINE)


def conventional_types(types=[]):
"""Return a list of Conventional Commits types merged with the given types."""
if set(types) & set(CONVENTIONAL_TYPES) == set():
Expand All @@ -68,6 +77,7 @@ def is_conventional(input, types=DEFAULT_TYPES, optional_scope=True):
Optionally provide a list of additional custom types.
"""
input = strip_comments(input)
types = conventional_types(types)
pattern = f"^({r_types(types)}){r_scope(optional_scope)}{r_delim()}{r_subject()}{r_body()}"
regex = re.compile(pattern, re.MULTILINE)
Expand Down
36 changes: 36 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,34 @@ def test_conventional_types__custom():
assert set(["custom", *format.CONVENTIONAL_TYPES]) == set(result)


def test_r_comment_single():
regex = re.compile(format.r_comment())
assert regex.match("# Some comment")
assert not regex.match("Some comment")
assert not regex.match(" # Some comment")


def test_strip_comments__consecutive():
input = """feat(scope): message
# Please enter the commit message for your changes.
# These are comments usually added by editors, f.ex. with export EDITOR=vim
"""
result = format.strip_comments(input)
assert result.count("\n") == 1
assert result.strip() == "feat(scope): message"


def test_strip_comments__spaced():
input = """feat(scope): message
# Please enter the commit message for your changes.
# These are comments usually added by editors, f.ex. with export EDITOR=vim
"""
result = format.strip_comments(input)
assert result.count("\n") == 2
assert result.strip() == "feat(scope): message"


@pytest.mark.parametrize("type", format.DEFAULT_TYPES)
def test_is_conventional__default_type(type):
input = f"{type}: message"
Expand Down Expand Up @@ -199,6 +227,14 @@ def test_is_conventional__bad_body_multiline_paragraphs():
assert not format.is_conventional(input)


def test_is_conventional__comment():
input = """feat(scope): message
# Please enter the commit message for your changes.
# These are comments usually added by editors, f.ex. with export EDITOR=vim
"""
assert format.is_conventional(input)


@pytest.mark.parametrize("char", ['"', "'", "`", "#", "&"])
def test_is_conventional__body_special_char(char):
input = f"feat: message with {char}"
Expand Down

0 comments on commit 07fd005

Please sign in to comment.