summaryrefslogtreecommitdiffstats
path: root/gitlint/tests/rules
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2020-11-03 06:07:45 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2020-11-03 06:07:45 +0000
commit5f208e04c159791e668031a7fa83f98724ec8d24 (patch)
tree4b58b42fd2a91a14871010e2dd39369a839ae383 /gitlint/tests/rules
parentAdding upstream version 0.13.1. (diff)
downloadgitlint-5f208e04c159791e668031a7fa83f98724ec8d24.tar.xz
gitlint-5f208e04c159791e668031a7fa83f98724ec8d24.zip
Adding upstream version 0.14.0.upstream/0.14.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gitlint/tests/rules')
-rw-r--r--gitlint/tests/rules/test_body_rules.py46
-rw-r--r--gitlint/tests/rules/test_configuration_rules.py38
-rw-r--r--gitlint/tests/rules/test_meta_rules.py9
-rw-r--r--gitlint/tests/rules/test_rules.py5
-rw-r--r--gitlint/tests/rules/test_title_rules.py34
-rw-r--r--gitlint/tests/rules/test_user_rules.py137
6 files changed, 215 insertions, 54 deletions
diff --git a/gitlint/tests/rules/test_body_rules.py b/gitlint/tests/rules/test_body_rules.py
index fcb1b30..f46760b 100644
--- a/gitlint/tests/rules/test_body_rules.py
+++ b/gitlint/tests/rules/test_body_rules.py
@@ -178,3 +178,49 @@ class BodyRuleTests(BaseTestCase):
violations = rule.validate(commit)
expected_violation_2 = rules.RuleViolation("B7", "Body does not mention changed file 'bar.txt'", None, 4)
self.assertEqual([expected_violation_2, expected_violation], violations)
+
+ def test_body_match_regex(self):
+ # We intentionally add 2 newlines at the end of our commit message as that's how git will pass the
+ # message. This way we also test that the rule strips off the last line.
+ commit = self.gitcommit(u"US1234: åbc\nIgnored\nBödy\nFöo\nMy-Commit-Tag: föo\n\n")
+
+ # assert no violation on default regex (=everything allowed)
+ rule = rules.BodyRegexMatches()
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # assert no violation on matching regex
+ # (also note that first body line - in between title and rest of body - is ignored)
+ rule = rules.BodyRegexMatches({'regex': u"^Bödy(.*)"})
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # assert we can do end matching (and last empty line is ignored)
+ # (also note that first body line - in between title and rest of body - is ignored)
+ rule = rules.BodyRegexMatches({'regex': u"My-Commit-Tag: föo$"})
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # common use-case: matching that a given line is present
+ rule = rules.BodyRegexMatches({'regex': u"(.*)Föo(.*)"})
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # assert violation on non-matching body
+ rule = rules.BodyRegexMatches({'regex': u"^Tëst(.*)Foo"})
+ violations = rule.validate(commit)
+ expected_violation = rules.RuleViolation("B8", u"Body does not match regex (^Tëst(.*)Foo)", None, 6)
+ self.assertListEqual(violations, [expected_violation])
+
+ # assert no violation on None regex
+ rule = rules.BodyRegexMatches({'regex': None})
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
+ # Assert no issues when there's no body or a weird body variation
+ bodies = [u"åbc", u"åbc\n", u"åbc\nföo\n", u"åbc\n\n", u"åbc\nföo\nblå", u"åbc\nföo\nblå\n"]
+ for body in bodies:
+ commit = self.gitcommit(body)
+ rule = rules.BodyRegexMatches({'regex': ".*"})
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
diff --git a/gitlint/tests/rules/test_configuration_rules.py b/gitlint/tests/rules/test_configuration_rules.py
index 73d42f3..121cb3a 100644
--- a/gitlint/tests/rules/test_configuration_rules.py
+++ b/gitlint/tests/rules/test_configuration_rules.py
@@ -67,5 +67,41 @@ class ConfigurationRuleTests(BaseTestCase):
rule.apply(config, commit)
self.assertEqual(config, expected_config)
- expected_log_message = u"DEBUG: gitlint.rules Ignoring commit because of rule 'I1': " + \
+ expected_log_message = u"DEBUG: gitlint.rules Ignoring commit because of rule 'I2': " + \
u"Commit message line ' a relëase body' matches the regex '(.*)relëase(.*)', ignoring rules: T1,B2"
+ self.assert_log_contains(expected_log_message)
+
+ def test_ignore_body_lines(self):
+ commit1 = self.gitcommit(u"Tïtle\n\nThis is\n a relëase body\n line")
+ commit2 = self.gitcommit(u"Tïtle\n\nThis is\n a relëase body\n line")
+
+ # no regex specified, nothing should have happened:
+ # commit and config should remain identical, log should be empty
+ rule = rules.IgnoreBodyLines()
+ config = LintConfig()
+ rule.apply(config, commit1)
+ self.assertEqual(commit1, commit2)
+ self.assertEqual(config, LintConfig())
+ self.assert_logged([])
+
+ # Matching regex
+ rule = rules.IgnoreBodyLines({"regex": u"(.*)relëase(.*)"})
+ config = LintConfig()
+ rule.apply(config, commit1)
+ # Our modified commit should be identical to a commit that doesn't contain the specific line
+ expected_commit = self.gitcommit(u"Tïtle\n\nThis is\n line")
+ # The original message isn't touched by this rule, this way we always have a way to reference back to it,
+ # so assert it's not modified by setting it to the same as commit1
+ expected_commit.message.original = commit1.message.original
+ self.assertEqual(commit1, expected_commit)
+ self.assertEqual(config, LintConfig()) # config shouldn't have been modified
+ self.assert_log_contains(u"DEBUG: gitlint.rules Ignoring line ' a relëase body' because it " +
+ u"matches '(.*)relëase(.*)'")
+
+ # Non-Matching regex: no changes expected
+ commit1 = self.gitcommit(u"Tïtle\n\nThis is\n a relëase body\n line")
+ rule = rules.IgnoreBodyLines({"regex": u"(.*)föobar(.*)"})
+ config = LintConfig()
+ rule.apply(config, commit1)
+ self.assertEqual(commit1, commit2)
+ self.assertEqual(config, LintConfig()) # config shouldn't have been modified
diff --git a/gitlint/tests/rules/test_meta_rules.py b/gitlint/tests/rules/test_meta_rules.py
index c94b8b3..987aa88 100644
--- a/gitlint/tests/rules/test_meta_rules.py
+++ b/gitlint/tests/rules/test_meta_rules.py
@@ -32,6 +32,15 @@ class MetaRuleTests(BaseTestCase):
[RuleViolation("M1", "Author email for commit is invalid", email)])
def test_author_valid_email_rule_custom_regex(self):
+ # regex=None -> the rule isn't applied
+ rule = AuthorValidEmail()
+ rule.options['regex'].set(None)
+ emailadresses = [u"föo", None, u"hür dür"]
+ for email in emailadresses:
+ commit = self.gitcommit(u"", author_email=email)
+ violations = rule.validate(commit)
+ self.assertIsNone(violations)
+
# Custom domain
rule = AuthorValidEmail({'regex': u"[^@]+@bår.com"})
valid_email_addresses = [
diff --git a/gitlint/tests/rules/test_rules.py b/gitlint/tests/rules/test_rules.py
index 89caa27..58ee1c3 100644
--- a/gitlint/tests/rules/test_rules.py
+++ b/gitlint/tests/rules/test_rules.py
@@ -13,6 +13,11 @@ class RuleTests(BaseTestCase):
setattr(rule, attr, u"åbc")
self.assertNotEqual(Rule(), rule)
+ def test_rule_log(self):
+ rule = Rule()
+ rule.log.debug(u"Tēst message")
+ self.assert_log_contains(u"DEBUG: gitlint.rules Tēst message")
+
def test_rule_violation_equality(self):
violation1 = RuleViolation(u"ïd1", u"My messåge", u"My cöntent", 1)
self.object_equality_test(violation1, ["rule_id", "message", "content", "line_nr"])
diff --git a/gitlint/tests/rules/test_title_rules.py b/gitlint/tests/rules/test_title_rules.py
index 07d2323..049735e 100644
--- a/gitlint/tests/rules/test_title_rules.py
+++ b/gitlint/tests/rules/test_title_rules.py
@@ -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):
@@ -152,3 +152,35 @@ class TitleRuleTests(BaseTestCase):
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 < 5
+ 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 = TitleMinLength({'min-length': 3})
+ violations = rule.validate(u"å" * 4, None)
+ self.assertIsNone(violations)
+
+ # assert no violations on length 3 (this asserts we've implemented a *strict* less than)
+ rule = TitleMinLength({'min-length': 3})
+ violations = rule.validate(u"å" * 3, 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])
+
+ # assert raise on empty title
+ expected_violation = RuleViolation("T8", "Title is too short (0<3)", "", 1)
+ violations = rule.validate("", None)
+ self.assertListEqual(violations, [expected_violation])
diff --git a/gitlint/tests/rules/test_user_rules.py b/gitlint/tests/rules/test_user_rules.py
index 57c03a0..52d0283 100644
--- a/gitlint/tests/rules/test_user_rules.py
+++ b/gitlint/tests/rules/test_user_rules.py
@@ -92,7 +92,7 @@ class UserRuleTests(BaseTestCase):
find_rule_classes(user_rule_path)
def test_find_rule_classes_nonexisting_path(self):
- with self.assertRaisesRegex(UserRuleError, u"Invalid extra-path: föo/bar"):
+ with self.assertRaisesMessage(UserRuleError, u"Invalid extra-path: föo/bar"):
find_rule_classes(u"föo/bar")
def test_assert_valid_rule_class(self):
@@ -111,15 +111,23 @@ class UserRuleTests(BaseTestCase):
def validate(self):
pass
+ class MyConfigurationRuleClass(rules.ConfigurationRule):
+ id = 'UC3'
+ name = u'my-cönfiguration-rule'
+
+ def apply(self):
+ pass
+
# Just assert that no error is raised
self.assertIsNone(assert_valid_rule_class(MyLineRuleClass))
self.assertIsNone(assert_valid_rule_class(MyCommitRuleClass))
+ self.assertIsNone(assert_valid_rule_class(MyConfigurationRuleClass))
def test_assert_valid_rule_class_negative(self):
# general test to make sure that incorrect rules will raise an exception
user_rule_path = self.get_sample_path("user_rules/incorrect_linerule")
- with self.assertRaisesRegex(UserRuleError,
- "User-defined rule class 'MyUserLineRule' must have a 'validate' method"):
+ with self.assertRaisesMessage(UserRuleError,
+ "User-defined rule class 'MyUserLineRule' must have a 'validate' method"):
find_rule_classes(user_rule_path)
def test_assert_valid_rule_class_negative_parent(self):
@@ -127,76 +135,101 @@ class UserRuleTests(BaseTestCase):
class MyRuleClass(object):
pass
- expected_msg = "User-defined rule class 'MyRuleClass' must extend from gitlint.rules.LineRule " + \
- "or gitlint.rules.CommitRule"
- with self.assertRaisesRegex(UserRuleError, expected_msg):
+ expected_msg = "User-defined rule class 'MyRuleClass' must extend from gitlint.rules.LineRule, " + \
+ "gitlint.rules.CommitRule or gitlint.rules.ConfigurationRule"
+ with self.assertRaisesMessage(UserRuleError, expected_msg):
assert_valid_rule_class(MyRuleClass)
def test_assert_valid_rule_class_negative_id(self):
- class MyRuleClass(rules.LineRule):
- pass
- # Rule class must have an id
- expected_msg = "User-defined rule class 'MyRuleClass' must have an 'id' attribute"
- with self.assertRaisesRegex(UserRuleError, expected_msg):
- assert_valid_rule_class(MyRuleClass)
+ for parent_class in [rules.LineRule, rules.CommitRule]:
- # Rule ids must be non-empty
- MyRuleClass.id = ""
- with self.assertRaisesRegex(UserRuleError, expected_msg):
- assert_valid_rule_class(MyRuleClass)
+ class MyRuleClass(parent_class):
+ pass
- # Rule ids must not start with one of the reserved id letters
- for letter in ["T", "R", "B", "M"]:
- MyRuleClass.id = letter + "1"
- expected_msg = "The id '{0}' of 'MyRuleClass' is invalid. Gitlint reserves ids starting with R,T,B,M"
- with self.assertRaisesRegex(UserRuleError, expected_msg.format(letter)):
+ # Rule class must have an id
+ expected_msg = "User-defined rule class 'MyRuleClass' must have an 'id' attribute"
+ with self.assertRaisesMessage(UserRuleError, expected_msg):
assert_valid_rule_class(MyRuleClass)
+ # Rule ids must be non-empty
+ MyRuleClass.id = ""
+ with self.assertRaisesMessage(UserRuleError, expected_msg):
+ assert_valid_rule_class(MyRuleClass)
+
+ # Rule ids must not start with one of the reserved id letters
+ for letter in ["T", "R", "B", "M", "I"]:
+ MyRuleClass.id = letter + "1"
+ expected_msg = "The id '{0}' of 'MyRuleClass' is invalid. Gitlint reserves ids starting with R,T,B,M,I"
+ with self.assertRaisesMessage(UserRuleError, expected_msg.format(letter)):
+ assert_valid_rule_class(MyRuleClass)
+
def test_assert_valid_rule_class_negative_name(self):
- class MyRuleClass(rules.LineRule):
- id = "UC1"
+ for parent_class in [rules.LineRule, rules.CommitRule]:
- # Rule class must have an name
- expected_msg = "User-defined rule class 'MyRuleClass' must have a 'name' attribute"
- with self.assertRaisesRegex(UserRuleError, expected_msg):
- assert_valid_rule_class(MyRuleClass)
+ class MyRuleClass(parent_class):
+ id = "UC1"
- # Rule names must be non-empty
- MyRuleClass.name = ""
- with self.assertRaisesRegex(UserRuleError, expected_msg):
- assert_valid_rule_class(MyRuleClass)
+ # Rule class must have an name
+ expected_msg = "User-defined rule class 'MyRuleClass' must have a 'name' attribute"
+ with self.assertRaisesMessage(UserRuleError, expected_msg):
+ assert_valid_rule_class(MyRuleClass)
+
+ # Rule names must be non-empty
+ MyRuleClass.name = ""
+ with self.assertRaisesMessage(UserRuleError, expected_msg):
+ assert_valid_rule_class(MyRuleClass)
def test_assert_valid_rule_class_negative_option_spec(self):
- class MyRuleClass(rules.LineRule):
- id = "UC1"
- name = u"my-rüle-class"
- # if set, option_spec must be a list of gitlint options
- MyRuleClass.options_spec = u"föo"
- expected_msg = "The options_spec attribute of user-defined rule class 'MyRuleClass' must be a list " + \
- "of gitlint.options.RuleOption"
- with self.assertRaisesRegex(UserRuleError, expected_msg):
- assert_valid_rule_class(MyRuleClass)
+ for parent_class in [rules.LineRule, rules.CommitRule]:
- # option_spec is a list, but not of gitlint options
- MyRuleClass.options_spec = [u"föo", 123] # pylint: disable=bad-option-value,redefined-variable-type
- with self.assertRaisesRegex(UserRuleError, expected_msg):
- assert_valid_rule_class(MyRuleClass)
+ class MyRuleClass(parent_class):
+ id = "UC1"
+ name = u"my-rüle-class"
+
+ # if set, option_spec must be a list of gitlint options
+ MyRuleClass.options_spec = u"föo"
+ expected_msg = "The options_spec attribute of user-defined rule class 'MyRuleClass' must be a list " + \
+ "of gitlint.options.RuleOption"
+ with self.assertRaisesMessage(UserRuleError, expected_msg):
+ assert_valid_rule_class(MyRuleClass)
+
+ # option_spec is a list, but not of gitlint options
+ MyRuleClass.options_spec = [u"föo", 123] # pylint: disable=bad-option-value,redefined-variable-type
+ with self.assertRaisesMessage(UserRuleError, expected_msg):
+ assert_valid_rule_class(MyRuleClass)
def test_assert_valid_rule_class_negative_validate(self):
- class MyRuleClass(rules.LineRule):
- id = "UC1"
+
+ baseclasses = [rules.LineRule, rules.CommitRule]
+ for clazz in baseclasses:
+ class MyRuleClass(clazz):
+ id = "UC1"
+ name = u"my-rüle-class"
+
+ with self.assertRaisesMessage(UserRuleError,
+ "User-defined rule class 'MyRuleClass' must have a 'validate' method"):
+ assert_valid_rule_class(MyRuleClass)
+
+ # validate attribute - not a method
+ MyRuleClass.validate = u"föo"
+ with self.assertRaisesMessage(UserRuleError,
+ "User-defined rule class 'MyRuleClass' must have a 'validate' method"):
+ assert_valid_rule_class(MyRuleClass)
+
+ def test_assert_valid_rule_class_negative_apply(self):
+ class MyRuleClass(rules.ConfigurationRule):
+ id = "UCR1"
name = u"my-rüle-class"
- with self.assertRaisesRegex(UserRuleError,
- "User-defined rule class 'MyRuleClass' must have a 'validate' method"):
+ expected_msg = "User-defined Configuration rule class 'MyRuleClass' must have an 'apply' method"
+ with self.assertRaisesMessage(UserRuleError, expected_msg):
assert_valid_rule_class(MyRuleClass)
# validate attribute - not a method
MyRuleClass.validate = u"föo"
- with self.assertRaisesRegex(UserRuleError,
- "User-defined rule class 'MyRuleClass' must have a 'validate' method"):
+ with self.assertRaisesMessage(UserRuleError, expected_msg):
assert_valid_rule_class(MyRuleClass)
def test_assert_valid_rule_class_negative_target(self):
@@ -210,12 +243,12 @@ class UserRuleTests(BaseTestCase):
# no target
expected_msg = "The target attribute of the user-defined LineRule class 'MyRuleClass' must be either " + \
"gitlint.rules.CommitMessageTitle or gitlint.rules.CommitMessageBody"
- with self.assertRaisesRegex(UserRuleError, expected_msg):
+ with self.assertRaisesMessage(UserRuleError, expected_msg):
assert_valid_rule_class(MyRuleClass)
# invalid target
MyRuleClass.target = u"föo"
- with self.assertRaisesRegex(UserRuleError, expected_msg):
+ with self.assertRaisesMessage(UserRuleError, expected_msg):
assert_valid_rule_class(MyRuleClass)
# valid target, no exception should be raised