summaryrefslogtreecommitdiffstats
path: root/gitlint-core/gitlint/tests/contrib
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2022-11-19 14:52:46 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2022-11-19 14:52:46 +0000
commita2aa51f5702b18016c25d943499941323952704d (patch)
tree7ee43f79639ee53903e7ca389e548974e1497c3a /gitlint-core/gitlint/tests/contrib
parentAdding upstream version 0.17.0. (diff)
downloadgitlint-a2aa51f5702b18016c25d943499941323952704d.tar.xz
gitlint-a2aa51f5702b18016c25d943499941323952704d.zip
Adding upstream version 0.18.0.upstream/0.18.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gitlint-core/gitlint/tests/contrib')
-rw-r--r--gitlint-core/gitlint/tests/contrib/rules/test_authors_commit.py106
-rw-r--r--gitlint-core/gitlint/tests/contrib/rules/test_conventional_commit.py39
-rw-r--r--gitlint-core/gitlint/tests/contrib/rules/test_disallow_cleanup_commits.py35
-rw-r--r--gitlint-core/gitlint/tests/contrib/rules/test_signedoff_by.py5
-rw-r--r--gitlint-core/gitlint/tests/contrib/test_contrib_rules.py34
5 files changed, 182 insertions, 37 deletions
diff --git a/gitlint-core/gitlint/tests/contrib/rules/test_authors_commit.py b/gitlint-core/gitlint/tests/contrib/rules/test_authors_commit.py
new file mode 100644
index 0000000..5ea9d8f
--- /dev/null
+++ b/gitlint-core/gitlint/tests/contrib/rules/test_authors_commit.py
@@ -0,0 +1,106 @@
+from collections import namedtuple
+from unittest.mock import patch
+from gitlint.tests.base import BaseTestCase
+from gitlint.rules import RuleViolation
+from gitlint.config import LintConfig
+
+from gitlint.contrib.rules.authors_commit import AllowedAuthors
+
+
+class ContribAuthorsCommitTests(BaseTestCase):
+ def setUp(self):
+ author = namedtuple("Author", "name, email")
+ self.author_1 = author("John Doe", "john.doe@mail.com")
+ self.author_2 = author("Bob Smith", "bob.smith@mail.com")
+ self.rule = AllowedAuthors()
+ self.gitcontext = self.get_gitcontext()
+
+ def get_gitcontext(self):
+ gitcontext = self.gitcontext(self.get_sample("commit_message/sample1"))
+ gitcontext.repository_path = self.get_sample_path("config")
+ return gitcontext
+
+ def get_commit(self, name, email):
+ commit = self.gitcommit("commit_message/sample1", author_name=name, author_email=email)
+ commit.message.context = self.gitcontext
+ return commit
+
+ def test_enable(self):
+ for rule_ref in ["CC3", "contrib-allowed-authors"]:
+ config = LintConfig()
+ config.contrib = [rule_ref]
+ self.assertIn(AllowedAuthors(), config.rules)
+
+ def test_authors_succeeds(self):
+ for author in [self.author_1, self.author_2]:
+ commit = self.get_commit(author.name, author.email)
+ violations = self.rule.validate(commit)
+ self.assertListEqual([], violations)
+
+ def test_authors_email_is_case_insensitive(self):
+ for email in [
+ self.author_2.email.capitalize(),
+ self.author_2.email.lower(),
+ self.author_2.email.upper(),
+ ]:
+ commit = self.get_commit(self.author_2.name, email)
+ violations = self.rule.validate(commit)
+ self.assertListEqual([], violations)
+
+ def test_authors_name_is_case_sensitive(self):
+ for name in [self.author_2.name.lower(), self.author_2.name.upper()]:
+ commit = self.get_commit(name, self.author_2.email)
+ violations = self.rule.validate(commit)
+ expected_violation = RuleViolation(
+ "CC3",
+ f"Author not in 'AUTHORS' file: " f'"{name} <{self.author_2.email}>"',
+ )
+ self.assertListEqual([expected_violation], violations)
+
+ def test_authors_bad_name_fails(self):
+ for name in ["", "root"]:
+ commit = self.get_commit(name, self.author_2.email)
+ violations = self.rule.validate(commit)
+ expected_violation = RuleViolation(
+ "CC3",
+ f"Author not in 'AUTHORS' file: " f'"{name} <{self.author_2.email}>"',
+ )
+ self.assertListEqual([expected_violation], violations)
+
+ def test_authors_bad_email_fails(self):
+ for email in ["", "root@example.com"]:
+ commit = self.get_commit(self.author_2.name, email)
+ violations = self.rule.validate(commit)
+ expected_violation = RuleViolation(
+ "CC3",
+ f"Author not in 'AUTHORS' file: " f'"{self.author_2.name} <{email}>"',
+ )
+ self.assertListEqual([expected_violation], violations)
+
+ def test_authors_invalid_combination_fails(self):
+ commit = self.get_commit(self.author_1.name, self.author_2.email)
+ violations = self.rule.validate(commit)
+ expected_violation = RuleViolation(
+ "CC3",
+ f"Author not in 'AUTHORS' file: " f'"{self.author_1.name} <{self.author_2.email}>"',
+ )
+ self.assertListEqual([expected_violation], violations)
+
+ @patch(
+ "gitlint.contrib.rules.authors_commit.Path.read_text",
+ return_value="John Doe <john.doe@mail.com>",
+ )
+ def test_read_authors_file(self, _mock_read_text):
+ authors, authors_file_name = AllowedAuthors._read_authors_from_file(self.gitcontext)
+ self.assertEqual(authors_file_name, "AUTHORS")
+ self.assertEqual(len(authors), 1)
+ self.assertEqual(authors, {self.author_1})
+
+ @patch(
+ "gitlint.contrib.rules.authors_commit.Path.exists",
+ return_value=False,
+ )
+ def test_read_authors_file_missing_file(self, _mock_iterdir):
+ with self.assertRaises(FileNotFoundError) as err:
+ AllowedAuthors._read_authors_from_file(self.gitcontext)
+ self.assertEqual(err.exception.args[0], "AUTHORS file not found")
diff --git a/gitlint-core/gitlint/tests/contrib/rules/test_conventional_commit.py b/gitlint-core/gitlint/tests/contrib/rules/test_conventional_commit.py
index 5da5cd5..7ce9c89 100644
--- a/gitlint-core/gitlint/tests/contrib/rules/test_conventional_commit.py
+++ b/gitlint-core/gitlint/tests/contrib/rules/test_conventional_commit.py
@@ -1,5 +1,3 @@
-
-# -*- coding: utf-8 -*-
from gitlint.tests.base import BaseTestCase
from gitlint.rules import RuleViolation
from gitlint.contrib.rules.conventional_commit import ConventionalCommit
@@ -7,10 +5,9 @@ from gitlint.config import LintConfig
class ContribConventionalCommitTests(BaseTestCase):
-
def test_enable(self):
# Test that rule can be enabled in config
- for rule_ref in ['CT1', 'contrib-title-conventional-commits']:
+ for rule_ref in ["CT1", "contrib-title-conventional-commits"]:
config = LintConfig()
config.contrib = [rule_ref]
self.assertIn(ConventionalCommit(), config.rules)
@@ -24,28 +21,38 @@ class ContribConventionalCommitTests(BaseTestCase):
self.assertListEqual([], violations)
# assert violation on wrong type
- expected_violation = RuleViolation("CT1", "Title does not start with one of fix, feat, chore, docs,"
- " style, refactor, perf, test, revert, ci, build", "bår: foo")
+ expected_violation = RuleViolation(
+ "CT1",
+ "Title does not start with one of fix, feat, chore, docs, style, refactor, perf, test, revert, ci, build",
+ "bår: foo",
+ )
violations = rule.validate("bår: foo", None)
self.assertListEqual([expected_violation], violations)
# assert violation when use strange chars after correct type
- expected_violation = RuleViolation("CT1", "Title does not start with one of fix, feat, chore, docs,"
- " style, refactor, perf, test, revert, ci, build",
- "feat_wrong_chars: föo")
+ expected_violation = RuleViolation(
+ "CT1",
+ "Title does not start with one of fix, feat, chore, docs, style, refactor, perf, test, revert, ci, build",
+ "feat_wrong_chars: föo",
+ )
violations = rule.validate("feat_wrong_chars: föo", None)
self.assertListEqual([expected_violation], violations)
# assert violation when use strange chars after correct type
- expected_violation = RuleViolation("CT1", "Title does not start with one of fix, feat, chore, docs,"
- " style, refactor, perf, test, revert, ci, build",
- "feat_wrong_chars(scope): föo")
+ expected_violation = RuleViolation(
+ "CT1",
+ "Title does not start with one of fix, feat, chore, docs, style, refactor, perf, test, revert, ci, build",
+ "feat_wrong_chars(scope): föo",
+ )
violations = rule.validate("feat_wrong_chars(scope): föo", None)
self.assertListEqual([expected_violation], violations)
# assert violation on wrong format
- expected_violation = RuleViolation("CT1", "Title does not follow ConventionalCommits.org format "
- "'type(optional-scope): description'", "fix föo")
+ expected_violation = RuleViolation(
+ "CT1",
+ "Title does not follow ConventionalCommits.org format 'type(optional-scope): description'",
+ "fix föo",
+ )
violations = rule.validate("fix föo", None)
self.assertListEqual([expected_violation], violations)
@@ -58,7 +65,7 @@ class ContribConventionalCommitTests(BaseTestCase):
self.assertListEqual([], violations)
# assert no violation when adding new type
- rule = ConventionalCommit({'types': ["föo", "bär"]})
+ rule = ConventionalCommit({"types": ["föo", "bär"]})
for typ in ["föo", "bär"]:
violations = rule.validate(typ + ": hür dur", None)
self.assertListEqual([], violations)
@@ -69,7 +76,7 @@ class ContribConventionalCommitTests(BaseTestCase):
self.assertListEqual([expected_violation], violations)
# assert no violation when adding new type named with numbers
- rule = ConventionalCommit({'types': ["föo123", "123bär"]})
+ rule = ConventionalCommit({"types": ["föo123", "123bär"]})
for typ in ["föo123", "123bär"]:
violations = rule.validate(typ + ": hür dur", None)
self.assertListEqual([], violations)
diff --git a/gitlint-core/gitlint/tests/contrib/rules/test_disallow_cleanup_commits.py b/gitlint-core/gitlint/tests/contrib/rules/test_disallow_cleanup_commits.py
new file mode 100644
index 0000000..841640a
--- /dev/null
+++ b/gitlint-core/gitlint/tests/contrib/rules/test_disallow_cleanup_commits.py
@@ -0,0 +1,35 @@
+from gitlint.tests.base import BaseTestCase
+from gitlint.rules import RuleViolation
+from gitlint.contrib.rules.disallow_cleanup_commits import DisallowCleanupCommits
+
+from gitlint.config import LintConfig
+
+
+class ContribDisallowCleanupCommitsTest(BaseTestCase):
+ def test_enable(self):
+ # Test that rule can be enabled in config
+ for rule_ref in ["CC2", "contrib-disallow-cleanup-commits"]:
+ config = LintConfig()
+ config.contrib = [rule_ref]
+ self.assertIn(DisallowCleanupCommits(), config.rules)
+
+ def test_disallow_fixup_squash_commit(self):
+ # No violations when no 'fixup!' line and no 'squash!' line is present
+ rule = DisallowCleanupCommits()
+ violations = rule.validate(self.gitcommit("Föobar\n\nMy Body"))
+ self.assertListEqual(violations, [])
+
+ # Assert violation when 'fixup!' in title
+ violations = rule.validate(self.gitcommit("fixup! Föobar\n\nMy Body"))
+ expected_violation = RuleViolation("CC2", "Fixup commits are not allowed", line_nr=1)
+ self.assertListEqual(violations, [expected_violation])
+
+ # Assert violation when 'squash!' in title
+ violations = rule.validate(self.gitcommit("squash! Föobar\n\nMy Body"))
+ expected_violation = RuleViolation("CC2", "Squash commits are not allowed", line_nr=1)
+ self.assertListEqual(violations, [expected_violation])
+
+ # Assert violation when 'amend!' in title
+ violations = rule.validate(self.gitcommit("amend! Föobar\n\nMy Body"))
+ expected_violation = RuleViolation("CC2", "Amend commits are not allowed", line_nr=1)
+ self.assertListEqual(violations, [expected_violation])
diff --git a/gitlint-core/gitlint/tests/contrib/rules/test_signedoff_by.py b/gitlint-core/gitlint/tests/contrib/rules/test_signedoff_by.py
index 0369cdc..88ff1db 100644
--- a/gitlint-core/gitlint/tests/contrib/rules/test_signedoff_by.py
+++ b/gitlint-core/gitlint/tests/contrib/rules/test_signedoff_by.py
@@ -1,5 +1,3 @@
-
-# -*- coding: utf-8 -*-
from gitlint.tests.base import BaseTestCase
from gitlint.rules import RuleViolation
from gitlint.contrib.rules.signedoff_by import SignedOffBy
@@ -8,10 +6,9 @@ from gitlint.config import LintConfig
class ContribSignedOffByTests(BaseTestCase):
-
def test_enable(self):
# Test that rule can be enabled in config
- for rule_ref in ['CC1', 'contrib-body-requires-signed-off-by']:
+ for rule_ref in ["CC1", "contrib-body-requires-signed-off-by"]:
config = LintConfig()
config.contrib = [rule_ref]
self.assertIn(SignedOffBy(), config.rules)
diff --git a/gitlint-core/gitlint/tests/contrib/test_contrib_rules.py b/gitlint-core/gitlint/tests/contrib/test_contrib_rules.py
index 8ab6539..bd098c6 100644
--- a/gitlint-core/gitlint/tests/contrib/test_contrib_rules.py
+++ b/gitlint-core/gitlint/tests/contrib/test_contrib_rules.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
import os
from gitlint.tests.base import BaseTestCase
@@ -8,13 +7,12 @@ from gitlint import rule_finder, rules
class ContribRuleTests(BaseTestCase):
-
CONTRIB_DIR = os.path.dirname(os.path.realpath(contrib_rules.__file__))
def test_contrib_tests_exist(self):
- """ Tests that every contrib rule file has an associated test file.
- While this doesn't guarantee that every contrib rule has associated tests (as we don't check the content
- of the tests file), it's a good leading indicator. """
+ """Tests that every contrib rule file has an associated test file.
+ While this doesn't guarantee that every contrib rule has associated tests (as we don't check the content
+ of the tests file), it's a good leading indicator."""
contrib_tests_dir = os.path.dirname(os.path.realpath(contrib_tests.__file__))
contrib_test_files = os.listdir(contrib_tests_dir)
@@ -22,16 +20,18 @@ class ContribRuleTests(BaseTestCase):
# Find all python files in the contrib dir and assert there's a corresponding test file
for filename in os.listdir(self.CONTRIB_DIR):
if filename.endswith(".py") and filename not in ["__init__.py"]:
- expected_test_file = "test_" + filename
- error_msg = "Every Contrib Rule must have associated tests. " + \
- f"Expected test file {os.path.join(contrib_tests_dir, expected_test_file)} not found."
+ expected_test_file = f"test_{filename}"
+ error_msg = (
+ "Every Contrib Rule must have associated tests. "
+ f"Expected test file {os.path.join(contrib_tests_dir, expected_test_file)} not found."
+ )
self.assertIn(expected_test_file, contrib_test_files, error_msg)
def test_contrib_rule_naming_conventions(self):
- """ Tests that contrib rules follow certain naming conventions.
- We can test for this at test time (and not during runtime like rule_finder.assert_valid_rule_class does)
- because these are contrib rules: once they're part of gitlint they can't change unless they pass this test
- again.
+ """Tests that contrib rules follow certain naming conventions.
+ We can test for this at test time (and not during runtime like rule_finder.assert_valid_rule_class does)
+ because these are contrib rules: once they're part of gitlint they can't change unless they pass this test
+ again.
"""
rule_classes = rule_finder.find_rule_classes(self.CONTRIB_DIR)
@@ -47,10 +47,10 @@ class ContribRuleTests(BaseTestCase):
self.assertTrue(clazz.id.startswith("CB"))
def test_contrib_rule_uniqueness(self):
- """ Tests that all contrib rules have unique identifiers.
- We can test for this at test time (and not during runtime like rule_finder.assert_valid_rule_class does)
- because these are contrib rules: once they're part of gitlint they can't change unless they pass this test
- again.
+ """Tests that all contrib rules have unique identifiers.
+ We can test for this at test time (and not during runtime like rule_finder.assert_valid_rule_class does)
+ because these are contrib rules: once they're part of gitlint they can't change unless they pass this test
+ again.
"""
rule_classes = rule_finder.find_rule_classes(self.CONTRIB_DIR)
@@ -61,7 +61,7 @@ class ContribRuleTests(BaseTestCase):
self.assertEqual(len(set(class_ids)), len(class_ids))
def test_contrib_rule_instantiated(self):
- """ Tests that all contrib rules can be instantiated without errors. """
+ """Tests that all contrib rules can be instantiated without errors."""
rule_classes = rule_finder.find_rule_classes(self.CONTRIB_DIR)
# No exceptions = what we want :-)