summaryrefslogtreecommitdiffstats
path: root/gitlint-core/gitlint/tests/rules
diff options
context:
space:
mode:
Diffstat (limited to 'gitlint-core/gitlint/tests/rules')
-rw-r--r--gitlint-core/gitlint/tests/rules/test_body_rules.py27
-rw-r--r--gitlint-core/gitlint/tests/rules/test_configuration_rules.py66
-rw-r--r--gitlint-core/gitlint/tests/rules/test_meta_rules.py48
-rw-r--r--gitlint-core/gitlint/tests/rules/test_rules.py2
-rw-r--r--gitlint-core/gitlint/tests/rules/test_title_rules.py56
-rw-r--r--gitlint-core/gitlint/tests/rules/test_user_rules.py69
6 files changed, 162 insertions, 106 deletions
diff --git a/gitlint-core/gitlint/tests/rules/test_body_rules.py b/gitlint-core/gitlint/tests/rules/test_body_rules.py
index 812c74a..94b1edf 100644
--- a/gitlint-core/gitlint/tests/rules/test_body_rules.py
+++ b/gitlint-core/gitlint/tests/rules/test_body_rules.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
from gitlint.tests.base import BaseTestCase
from gitlint import rules
@@ -17,7 +16,7 @@ class BodyRuleTests(BaseTestCase):
self.assertListEqual(violations, [expected_violation])
# set line length to 120, and check no violation on length 73
- rule = rules.BodyMaxLineLength({'line-length': 120})
+ rule = rules.BodyMaxLineLength({"line-length": 120})
violations = rule.validate("å" * 73, None)
self.assertIsNone(violations)
@@ -100,14 +99,14 @@ class BodyRuleTests(BaseTestCase):
# set line length to 120, and check violation on length 21
expected_violation = rules.RuleViolation("B5", "Body message is too short (21<120)", "å" * 21, 3)
- rule = rules.BodyMinLength({'min-length': 120})
- commit = self.gitcommit("Title\n\n{0}\n".format("å" * 21)) # pylint: disable=consider-using-f-string
+ rule = rules.BodyMinLength({"min-length": 120})
+ commit = self.gitcommit("Title\n\n{}\n".format("å" * 21)) # pylint: disable=consider-using-f-string
violations = rule.validate(commit)
self.assertListEqual(violations, [expected_violation])
# Make sure we don't get the error if the body-length is exactly the min-length
- rule = rules.BodyMinLength({'min-length': 8})
- commit = self.gitcommit("Tïtle\n\n{0}\n".format("å" * 8)) # pylint: disable=consider-using-f-string
+ rule = rules.BodyMinLength({"min-length": 8})
+ commit = self.gitcommit("Tïtle\n\n{}\n".format("å" * 8)) # pylint: disable=consider-using-f-string
violations = rule.validate(commit)
self.assertIsNone(violations)
@@ -145,7 +144,7 @@ class BodyRuleTests(BaseTestCase):
self.assertIsNone(violations)
# assert error for merge commits if ignore-merge-commits is disabled
- rule = rules.BodyMissing({'ignore-merge-commits': False})
+ rule = rules.BodyMissing({"ignore-merge-commits": False})
violations = rule.validate(commit)
expected_violation = rules.RuleViolation("B6", "Body message is missing", None, 3)
self.assertListEqual(violations, [expected_violation])
@@ -159,7 +158,7 @@ class BodyRuleTests(BaseTestCase):
self.assertIsNone(violations)
# assert no error when no files have changed but certain files need to be mentioned on change
- rule = rules.BodyChangedFileMention({'files': "bar.txt,föo/test.py"})
+ rule = rules.BodyChangedFileMention({"files": "bar.txt,föo/test.py"})
commit = self.gitcommit("This is a test\n\nHere is a mention of föo/test.py")
violations = rule.validate(commit)
self.assertIsNone(violations)
@@ -201,29 +200,29 @@ class BodyRuleTests(BaseTestCase):
# 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': "^Bödy(.*)"})
+ rule = rules.BodyRegexMatches({"regex": "^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': "My-Commit-Tag: föo$"})
+ rule = rules.BodyRegexMatches({"regex": "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': "(.*)Föo(.*)"})
+ rule = rules.BodyRegexMatches({"regex": "(.*)Föo(.*)"})
violations = rule.validate(commit)
self.assertIsNone(violations)
# assert violation on non-matching body
- rule = rules.BodyRegexMatches({'regex': "^Tëst(.*)Foo"})
+ rule = rules.BodyRegexMatches({"regex": "^Tëst(.*)Foo"})
violations = rule.validate(commit)
expected_violation = rules.RuleViolation("B8", "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})
+ rule = rules.BodyRegexMatches({"regex": None})
violations = rule.validate(commit)
self.assertIsNone(violations)
@@ -231,6 +230,6 @@ class BodyRuleTests(BaseTestCase):
bodies = ["åbc", "åbc\n", "åbc\nföo\n", "åbc\n\n", "åbc\nföo\nblå", "åbc\nföo\nblå\n"]
for body in bodies:
commit = self.gitcommit(body)
- rule = rules.BodyRegexMatches({'regex': ".*"})
+ rule = rules.BodyRegexMatches({"regex": ".*"})
violations = rule.validate(commit)
self.assertIsNone(violations)
diff --git a/gitlint-core/gitlint/tests/rules/test_configuration_rules.py b/gitlint-core/gitlint/tests/rules/test_configuration_rules.py
index 9302da5..9e3b07c 100644
--- a/gitlint-core/gitlint/tests/rules/test_configuration_rules.py
+++ b/gitlint-core/gitlint/tests/rules/test_configuration_rules.py
@@ -1,5 +1,4 @@
-# -*- coding: utf-8 -*-
-from gitlint.tests.base import BaseTestCase
+from gitlint.tests.base import BaseTestCase, EXPECTED_REGEX_STYLE_SEARCH_DEPRECATION_WARNING
from gitlint import rules
from gitlint.config import LintConfig
@@ -22,20 +21,25 @@ class ConfigurationRuleTests(BaseTestCase):
rule.apply(config, commit)
self.assertEqual(config, expected_config)
- expected_log_message = "DEBUG: gitlint.rules Ignoring commit because of rule 'I1': " + \
- "Commit title 'Releäse' matches the regex '^Releäse(.*)', ignoring rules: all"
- self.assert_log_contains(expected_log_message)
+ expected_log_messages = [
+ EXPECTED_REGEX_STYLE_SEARCH_DEPRECATION_WARNING.format("I1", "ignore-by-title"),
+ "DEBUG: gitlint.rules Ignoring commit because of rule 'I1': "
+ "Commit title 'Releäse' matches the regex '^Releäse(.*)', ignoring rules: all",
+ ]
+ self.assert_logged(expected_log_messages)
# Matching regex with specific ignore
- rule = rules.IgnoreByTitle({"regex": "^Releäse(.*)",
- "ignore": "T1,B2"})
+ rule = rules.IgnoreByTitle({"regex": "^Releäse(.*)", "ignore": "T1,B2"})
expected_config = LintConfig()
expected_config.ignore = "T1,B2"
rule.apply(config, commit)
self.assertEqual(config, expected_config)
- expected_log_message = "DEBUG: gitlint.rules Ignoring commit because of rule 'I1': " + \
+ expected_log_messages += [
+ "DEBUG: gitlint.rules Ignoring commit because of rule 'I1': "
"Commit title 'Releäse' matches the regex '^Releäse(.*)', ignoring rules: T1,B2"
+ ]
+ self.assert_logged(expected_log_messages)
def test_ignore_by_body(self):
commit = self.gitcommit("Tïtle\n\nThis is\n a relëase body\n line")
@@ -54,22 +58,26 @@ class ConfigurationRuleTests(BaseTestCase):
rule.apply(config, commit)
self.assertEqual(config, expected_config)
- expected_log_message = "DEBUG: gitlint.rules Ignoring commit because of rule 'I2': " + \
- "Commit message line ' a relëase body' matches the regex '(.*)relëase(.*)'," + \
- " ignoring rules: all"
- self.assert_log_contains(expected_log_message)
+ expected_log_messages = [
+ EXPECTED_REGEX_STYLE_SEARCH_DEPRECATION_WARNING.format("I2", "ignore-by-body"),
+ "DEBUG: gitlint.rules Ignoring commit because of rule 'I2': "
+ "Commit message line ' a relëase body' matches the regex '(.*)relëase(.*)',"
+ " ignoring rules: all",
+ ]
+ self.assert_logged(expected_log_messages)
# Matching regex with specific ignore
- rule = rules.IgnoreByBody({"regex": "(.*)relëase(.*)",
- "ignore": "T1,B2"})
+ rule = rules.IgnoreByBody({"regex": "(.*)relëase(.*)", "ignore": "T1,B2"})
expected_config = LintConfig()
expected_config.ignore = "T1,B2"
rule.apply(config, commit)
self.assertEqual(config, expected_config)
- expected_log_message = "DEBUG: gitlint.rules Ignoring commit because of rule 'I2': " + \
+ expected_log_messages += [
+ "DEBUG: gitlint.rules Ignoring commit because of rule 'I2': "
"Commit message line ' a relëase body' matches the regex '(.*)relëase(.*)', ignoring rules: T1,B2"
- self.assert_log_contains(expected_log_message)
+ ]
+ self.assert_logged(expected_log_messages)
def test_ignore_by_author_name(self):
commit = self.gitcommit("Tïtle\n\nThis is\n a relëase body\n line", author_name="Tëst nåme")
@@ -88,10 +96,13 @@ class ConfigurationRuleTests(BaseTestCase):
rule.apply(config, commit)
self.assertEqual(config, expected_config)
- expected_log_message = ("DEBUG: gitlint.rules Ignoring commit because of rule 'I4': "
- "Commit Author Name 'Tëst nåme' matches the regex '(.*)ëst(.*)',"
- " ignoring rules: all")
- self.assert_log_contains(expected_log_message)
+ expected_log_messages = [
+ EXPECTED_REGEX_STYLE_SEARCH_DEPRECATION_WARNING.format("I4", "ignore-by-author-name"),
+ "DEBUG: gitlint.rules Ignoring commit because of rule 'I4': "
+ "Commit Author Name 'Tëst nåme' matches the regex '(.*)ëst(.*)',"
+ " ignoring rules: all",
+ ]
+ self.assert_logged(expected_log_messages)
# Matching regex with specific ignore
rule = rules.IgnoreByAuthorName({"regex": "(.*)nåme", "ignore": "T1,B2"})
@@ -100,9 +111,11 @@ class ConfigurationRuleTests(BaseTestCase):
rule.apply(config, commit)
self.assertEqual(config, expected_config)
- expected_log_message = ("DEBUG: gitlint.rules Ignoring commit because of rule 'I4': "
- "Commit Author Name 'Tëst nåme' matches the regex '(.*)nåme', ignoring rules: T1,B2")
- self.assert_log_contains(expected_log_message)
+ expected_log_messages += [
+ "DEBUG: gitlint.rules Ignoring commit because of rule 'I4': "
+ "Commit Author Name 'Tëst nåme' matches the regex '(.*)nåme', ignoring rules: T1,B2"
+ ]
+ self.assert_logged(expected_log_messages)
def test_ignore_body_lines(self):
commit1 = self.gitcommit("Tïtle\n\nThis is\n a relëase body\n line")
@@ -128,8 +141,11 @@ class ConfigurationRuleTests(BaseTestCase):
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("DEBUG: gitlint.rules Ignoring line ' a relëase body' because it " +
- "matches '(.*)relëase(.*)'")
+ expected_log_messages = [
+ EXPECTED_REGEX_STYLE_SEARCH_DEPRECATION_WARNING.format("I3", "ignore-body-lines"),
+ "DEBUG: gitlint.rules Ignoring line ' a relëase body' because it " + "matches '(.*)relëase(.*)'",
+ ]
+ self.assert_logged(expected_log_messages)
# Non-Matching regex: no changes expected
commit1 = self.gitcommit("Tïtle\n\nThis is\n a relëase body\n line")
diff --git a/gitlint-core/gitlint/tests/rules/test_meta_rules.py b/gitlint-core/gitlint/tests/rules/test_meta_rules.py
index 568ca3f..0b8a10a 100644
--- a/gitlint-core/gitlint/tests/rules/test_meta_rules.py
+++ b/gitlint-core/gitlint/tests/rules/test_meta_rules.py
@@ -1,5 +1,4 @@
-# -*- coding: utf-8 -*-
-from gitlint.tests.base import BaseTestCase
+from gitlint.tests.base import BaseTestCase, EXPECTED_REGEX_STYLE_SEARCH_DEPRECATION_WARNING
from gitlint.rules import AuthorValidEmail, RuleViolation
@@ -8,8 +7,13 @@ class MetaRuleTests(BaseTestCase):
rule = AuthorValidEmail()
# valid email addresses
- valid_email_addresses = ["föo@bar.com", "Jöhn.Doe@bar.com", "jöhn+doe@bar.com", "jöhn/doe@bar.com",
- "jöhn.doe@subdomain.bar.com"]
+ valid_email_addresses = [
+ "föo@bar.com",
+ "Jöhn.Doe@bar.com",
+ "jöhn+doe@bar.com",
+ "jöhn/doe@bar.com",
+ "jöhn.doe@subdomain.bar.com",
+ ]
for email in valid_email_addresses:
commit = self.gitcommit("", author_email=email)
violations = rule.validate(commit)
@@ -22,19 +26,32 @@ class MetaRuleTests(BaseTestCase):
self.assertIsNone(violations)
# Invalid email addresses: no TLD, no domain, no @, space anywhere (=valid but not allowed by gitlint)
- invalid_email_addresses = ["föo@bar", "JöhnDoe", "Jöhn Doe", "Jöhn Doe@foo.com", " JöhnDoe@foo.com",
- "JöhnDoe@ foo.com", "JöhnDoe@foo. com", "JöhnDoe@foo. com", "@bår.com",
- "föo@.com"]
+ invalid_email_addresses = [
+ "föo@bar",
+ "JöhnDoe",
+ "Jöhn Doe",
+ "Jöhn Doe@foo.com",
+ " JöhnDoe@foo.com",
+ "JöhnDoe@ foo.com",
+ "JöhnDoe@foo. com",
+ "JöhnDoe@foo. com",
+ "@bår.com",
+ "föo@.com",
+ ]
for email in invalid_email_addresses:
commit = self.gitcommit("", author_email=email)
violations = rule.validate(commit)
- self.assertListEqual(violations,
- [RuleViolation("M1", "Author email for commit is invalid", email)])
+ self.assertListEqual(violations, [RuleViolation("M1", "Author email for commit is invalid", email)])
+
+ # Ensure nothing is logged, this relates specifically to a deprecation warning on the use of
+ # re.match vs re.search in the rules (see issue #254)
+ # If no custom regex is used, the rule uses the default regex in combination with re.search
+ self.assert_logged([])
def test_author_valid_email_rule_custom_regex(self):
# regex=None -> the rule isn't applied
rule = AuthorValidEmail()
- rule.options['regex'].set(None)
+ rule.options["regex"].set(None)
emailadresses = ["föo", None, "hür dür"]
for email in emailadresses:
commit = self.gitcommit("", author_email=email)
@@ -42,9 +59,8 @@ class MetaRuleTests(BaseTestCase):
self.assertIsNone(violations)
# Custom domain
- rule = AuthorValidEmail({'regex': "[^@]+@bår.com"})
- valid_email_addresses = [
- "föo@bår.com", "Jöhn.Doe@bår.com", "jöhn+doe@bår.com", "jöhn/doe@bår.com"]
+ rule = AuthorValidEmail({"regex": "[^@]+@bår.com"})
+ valid_email_addresses = ["föo@bår.com", "Jöhn.Doe@bår.com", "jöhn+doe@bår.com", "jöhn/doe@bår.com"]
for email in valid_email_addresses:
commit = self.gitcommit("", author_email=email)
violations = rule.validate(commit)
@@ -55,5 +71,7 @@ class MetaRuleTests(BaseTestCase):
for email in invalid_email_addresses:
commit = self.gitcommit("", author_email=email)
violations = rule.validate(commit)
- self.assertListEqual(violations,
- [RuleViolation("M1", "Author email for commit is invalid", email)])
+ self.assertListEqual(violations, [RuleViolation("M1", "Author email for commit is invalid", email)])
+
+ # When a custom regex is used, a warning should be logged by default
+ self.assert_logged([EXPECTED_REGEX_STYLE_SEARCH_DEPRECATION_WARNING.format("M1", "author-valid-email")])
diff --git a/gitlint-core/gitlint/tests/rules/test_rules.py b/gitlint-core/gitlint/tests/rules/test_rules.py
index 6fcf9bc..199cc7e 100644
--- a/gitlint-core/gitlint/tests/rules/test_rules.py
+++ b/gitlint-core/gitlint/tests/rules/test_rules.py
@@ -1,10 +1,8 @@
-# -*- coding: utf-8 -*-
from gitlint.tests.base import BaseTestCase
from gitlint.rules import Rule, RuleViolation
class RuleTests(BaseTestCase):
-
def test_rule_equality(self):
self.assertEqual(Rule(), Rule())
# Ensure rules are not equal if they differ on their attributes
diff --git a/gitlint-core/gitlint/tests/rules/test_title_rules.py b/gitlint-core/gitlint/tests/rules/test_title_rules.py
index 10b4aab..4796e54 100644
--- a/gitlint-core/gitlint/tests/rules/test_title_rules.py
+++ b/gitlint-core/gitlint/tests/rules/test_title_rules.py
@@ -1,7 +1,15 @@
-# -*- coding: utf-8 -*-
from gitlint.tests.base import BaseTestCase
-from gitlint.rules import TitleMaxLength, TitleTrailingWhitespace, TitleHardTab, TitleMustNotContainWord, \
- TitleTrailingPunctuation, TitleLeadingWhitespace, TitleRegexMatches, RuleViolation, TitleMinLength
+from gitlint.rules import (
+ TitleMaxLength,
+ TitleTrailingWhitespace,
+ TitleHardTab,
+ TitleMustNotContainWord,
+ TitleTrailingPunctuation,
+ TitleLeadingWhitespace,
+ TitleRegexMatches,
+ RuleViolation,
+ TitleMinLength,
+)
class TitleRuleTests(BaseTestCase):
@@ -18,7 +26,7 @@ class TitleRuleTests(BaseTestCase):
self.assertListEqual(violations, [expected_violation])
# set line length to 120, and check no violation on length 73
- rule = TitleMaxLength({'line-length': 120})
+ rule = TitleMaxLength({"line-length": 120})
violations = rule.validate("å" * 73, None)
self.assertIsNone(violations)
@@ -85,31 +93,37 @@ class TitleRuleTests(BaseTestCase):
# match literally
violations = rule.validate("WIP This is å test", None)
- expected_violation = RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)",
- "WIP This is å test")
+ expected_violation = RuleViolation(
+ "T5", "Title contains the word 'WIP' (case-insensitive)", "WIP This is å test"
+ )
self.assertListEqual(violations, [expected_violation])
# match case insensitive
violations = rule.validate("wip This is å test", None)
- expected_violation = RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)",
- "wip This is å test")
+ expected_violation = RuleViolation(
+ "T5", "Title contains the word 'WIP' (case-insensitive)", "wip This is å test"
+ )
self.assertListEqual(violations, [expected_violation])
# match if there is a colon after the word
violations = rule.validate("WIP:This is å test", None)
- expected_violation = RuleViolation("T5", "Title contains the word 'WIP' (case-insensitive)",
- "WIP:This is å test")
+ expected_violation = RuleViolation(
+ "T5", "Title contains the word 'WIP' (case-insensitive)", "WIP:This is å test"
+ )
self.assertListEqual(violations, [expected_violation])
# match multiple words
- rule = TitleMustNotContainWord({'words': "wip,test,å"})
+ rule = TitleMustNotContainWord({"words": "wip,test,å"})
violations = rule.validate("WIP:This is å test", None)
- expected_violation = RuleViolation("T5", "Title contains the word 'wip' (case-insensitive)",
- "WIP:This is å test")
- expected_violation2 = RuleViolation("T5", "Title contains the word 'test' (case-insensitive)",
- "WIP:This is å test")
- expected_violation3 = RuleViolation("T5", "Title contains the word 'å' (case-insensitive)",
- "WIP:This is å test")
+ expected_violation = RuleViolation(
+ "T5", "Title contains the word 'wip' (case-insensitive)", "WIP:This is å test"
+ )
+ expected_violation2 = RuleViolation(
+ "T5", "Title contains the word 'test' (case-insensitive)", "WIP:This is å test"
+ )
+ expected_violation3 = RuleViolation(
+ "T5", "Title contains the word 'å' (case-insensitive)", "WIP:This is å test"
+ )
self.assertListEqual(violations, [expected_violation, expected_violation2, expected_violation3])
def test_leading_whitespace(self):
@@ -143,12 +157,12 @@ class TitleRuleTests(BaseTestCase):
self.assertIsNone(violations)
# assert no violation on matching regex
- rule = TitleRegexMatches({'regex': "^US[0-9]*: å"})
+ rule = TitleRegexMatches({"regex": "^US[0-9]*: å"})
violations = rule.validate(commit.message.title, commit)
self.assertIsNone(violations)
# assert violation when no matching regex
- rule = TitleRegexMatches({'regex': "^UÅ[0-9]*"})
+ rule = TitleRegexMatches({"regex": "^UÅ[0-9]*"})
violations = rule.validate(commit.message.title, commit)
expected_violation = RuleViolation("T7", "Title does not match regex (^UÅ[0-9]*)", "US1234: åbc")
self.assertListEqual(violations, [expected_violation])
@@ -166,12 +180,12 @@ class TitleRuleTests(BaseTestCase):
self.assertListEqual(violations, [expected_violation])
# set line length to 3, and check no violation on length 4
- rule = TitleMinLength({'min-length': 3})
+ rule = TitleMinLength({"min-length": 3})
violations = rule.validate("å" * 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})
+ rule = TitleMinLength({"min-length": 3})
violations = rule.validate("å" * 3, None)
self.assertIsNone(violations)
diff --git a/gitlint-core/gitlint/tests/rules/test_user_rules.py b/gitlint-core/gitlint/tests/rules/test_user_rules.py
index d66a7cc..fc8d423 100644
--- a/gitlint-core/gitlint/tests/rules/test_user_rules.py
+++ b/gitlint-core/gitlint/tests/rules/test_user_rules.py
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-
import os
import sys
@@ -33,7 +31,7 @@ class UserRuleTests(BaseTestCase):
# Do some basic asserts on our user rule
self.assertEqual(classes[0].id, "UC1")
self.assertEqual(classes[0].name, "my-üser-commit-rule")
- expected_option = options.IntOption('violation-count', 1, "Number of violåtions to return")
+ expected_option = options.IntOption("violation-count", 1, "Number of violåtions to return")
self.assertListEqual(classes[0].options_spec, [expected_option])
self.assertTrue(hasattr(classes[0], "validate"))
@@ -44,10 +42,15 @@ class UserRuleTests(BaseTestCase):
self.assertListEqual(violations, [rules.RuleViolation("UC1", "Commit violåtion 1", "Contënt 1", 1)])
# Have it return more violations
- rule_class.options['violation-count'].value = 2
+ rule_class.options["violation-count"].value = 2
violations = rule_class.validate("false-commit-object (ignored)")
- self.assertListEqual(violations, [rules.RuleViolation("UC1", "Commit violåtion 1", "Contënt 1", 1),
- rules.RuleViolation("UC1", "Commit violåtion 2", "Contënt 2", 2)])
+ self.assertListEqual(
+ violations,
+ [
+ rules.RuleViolation("UC1", "Commit violåtion 1", "Contënt 1", 1),
+ rules.RuleViolation("UC1", "Commit violåtion 2", "Contënt 2", 2),
+ ],
+ )
def test_extra_path_specified_by_file(self):
# Test that find_rule_classes can handle an extra path given as a file name instead of a directory
@@ -67,7 +70,7 @@ class UserRuleTests(BaseTestCase):
classes = find_rule_classes(user_rule_path)
# convert classes to strings and sort them so we can compare them
- class_strings = sorted([str(clazz) for clazz in classes])
+ class_strings = sorted(str(clazz) for clazz in classes)
expected = ["<class 'my_commit_rules.MyUserCommitRule'>", "<class 'parent_package.InitFileRule'>"]
self.assertListEqual(class_strings, expected)
@@ -96,23 +99,23 @@ class UserRuleTests(BaseTestCase):
def test_assert_valid_rule_class(self):
class MyLineRuleClass(rules.LineRule):
- id = 'UC1'
- name = 'my-lïne-rule'
+ id = "UC1"
+ name = "my-lïne-rule"
target = rules.CommitMessageTitle
def validate(self):
pass
class MyCommitRuleClass(rules.CommitRule):
- id = 'UC2'
- name = 'my-cömmit-rule'
+ id = "UC2"
+ name = "my-cömmit-rule"
def validate(self):
pass
class MyConfigurationRuleClass(rules.ConfigurationRule):
- id = 'UC3'
- name = 'my-cönfiguration-rule'
+ id = "UC3"
+ name = "my-cönfiguration-rule"
def apply(self):
pass
@@ -125,8 +128,9 @@ class UserRuleTests(BaseTestCase):
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.assertRaisesMessage(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):
@@ -134,13 +138,14 @@ class UserRuleTests(BaseTestCase):
class MyRuleClass:
pass
- expected_msg = "User-defined rule class 'MyRuleClass' must extend from gitlint.rules.LineRule, " + \
- "gitlint.rules.CommitRule or gitlint.rules.ConfigurationRule"
+ 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):
-
for parent_class in [rules.LineRule, rules.CommitRule]:
class MyRuleClass(parent_class):
@@ -159,8 +164,9 @@ class UserRuleTests(BaseTestCase):
# 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 = f"The id '{letter}' of 'MyRuleClass' is invalid. " + \
- "Gitlint reserves ids starting with R,T,B,M,I"
+ expected_msg = (
+ f"The id '{letter}' of 'MyRuleClass' is invalid. Gitlint reserves ids starting with R,T,B,M,I"
+ )
with self.assertRaisesMessage(UserRuleError, expected_msg):
assert_valid_rule_class(MyRuleClass)
@@ -181,7 +187,6 @@ class UserRuleTests(BaseTestCase):
assert_valid_rule_class(MyRuleClass)
def test_assert_valid_rule_class_negative_option_spec(self):
-
for parent_class in [rules.LineRule, rules.CommitRule]:
class MyRuleClass(parent_class):
@@ -190,8 +195,10 @@ class UserRuleTests(BaseTestCase):
# if set, option_spec must be a list of gitlint options
MyRuleClass.options_spec = "föo"
- expected_msg = "The options_spec attribute of user-defined rule class 'MyRuleClass' must be a list " + \
+ 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)
@@ -201,21 +208,23 @@ class UserRuleTests(BaseTestCase):
assert_valid_rule_class(MyRuleClass)
def test_assert_valid_rule_class_negative_validate(self):
-
baseclasses = [rules.LineRule, rules.CommitRule]
for clazz in baseclasses:
+
class MyRuleClass(clazz):
id = "UC1"
name = "my-rüle-class"
- with self.assertRaisesMessage(UserRuleError,
- "User-defined rule class 'MyRuleClass' must have a 'validate' method"):
+ 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 = "föo"
- with self.assertRaisesMessage(UserRuleError,
- "User-defined rule class 'MyRuleClass' must have a 'validate' method"):
+ 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):
@@ -241,8 +250,10 @@ class UserRuleTests(BaseTestCase):
pass
# no target
- expected_msg = "The target attribute of the user-defined LineRule class 'MyRuleClass' must be either " + \
- "gitlint.rules.CommitMessageTitle or gitlint.rules.CommitMessageBody"
+ expected_msg = (
+ "The target attribute of the user-defined LineRule class 'MyRuleClass' must be either "
+ "gitlint.rules.CommitMessageTitle or gitlint.rules.CommitMessageBody"
+ )
with self.assertRaisesMessage(UserRuleError, expected_msg):
assert_valid_rule_class(MyRuleClass)