Skip to content

Commit

Permalink
feature: Add title-min-length
Browse files Browse the repository at this point in the history
* Add a new `title-min-length` rule which mimics the `body-min-length`
  rule in its function. The default min length is set to 5 as per the
  discussion in jorisroovers#138.

* Add a few tests modeled on TitleMaxLength.

Signed-off-by: mr.Shu <mr@shu.io>
  • Loading branch information
mrshu committed Aug 8, 2020
1 parent 6a37bae commit bbf88a5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
14 changes: 14 additions & 0 deletions gitlint/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,20 @@ def validate(self, title, _commit):
return [RuleViolation(self.id, violation_msg, title)]


class TitleMinLength(LineRule):
name = "title-min-length"
id = "T8"
target = CommitMessageTitle
options_spec = [IntOption('min-length', 5, "Min line length in characters")]

def validate(self, title, _commit):
min_length = self.options['min-length'].value
actual_length = len(title)
if 0 < actual_length < min_length:
violation_message = "Title is too short ({0}<{1})".format(actual_length, min_length)
return [RuleViolation(self.id, violation_message, title, 1)]


class BodyMaxLineLength(MaxLineLength):
name = "body-max-line-length"
id = "B1"
Expand Down
24 changes: 23 additions & 1 deletion gitlint/tests/rules/test_title_rules.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from gitlint.tests.base import BaseTestCase
from gitlint.rules import TitleMaxLength, TitleTrailingWhitespace, TitleHardTab, TitleMustNotContainWord, \
TitleTrailingPunctuation, TitleLeadingWhitespace, TitleRegexMatches, RuleViolation
TitleTrailingPunctuation, TitleLeadingWhitespace, TitleRegexMatches, RuleViolation, TitleMinLength


class TitleRuleTests(BaseTestCase):
Expand Down Expand Up @@ -152,3 +152,25 @@ def test_regex_matches(self):
violations = rule.validate(commit.message.title, commit)
expected_violation = RuleViolation("T7", u"Title does not match regex (^UÅ[0-9]*)", u"US1234: åbc")
self.assertListEqual(violations, [expected_violation])

def test_min_line_length(self):
rule = TitleMinLength()

# assert no error
violation = rule.validate(u"å" * 72, None)
self.assertIsNone(violation)

# assert error on line length > 72
expected_violation = RuleViolation("T8", "Title is too short (4<5)", u"å" * 4, 1)
violations = rule.validate(u"å" * 4, None)
self.assertListEqual(violations, [expected_violation])

# set line length to 3, and check no violation on length 4
rule = TitleMaxLength({'min-length': 3})
violations = rule.validate(u"å" * 4, None)
self.assertIsNone(violations)

# assert raise on 2
expected_violation = RuleViolation("T8", "Title is too short (2<3)", u"å" * 2, 1)
violations = rule.validate(u"å" * 2, None)
self.assertListEqual(violations, [expected_violation])

0 comments on commit bbf88a5

Please sign in to comment.