summaryrefslogtreecommitdiffstats
path: root/gitlint-core/gitlint/tests/contrib/rules/test_disallow_cleanup_commits.py
blob: 19833672e2a83e6a1e1378c89ec23652ebc55e10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from gitlint.config import LintConfig
from gitlint.contrib.rules.disallow_cleanup_commits import DisallowCleanupCommits
from gitlint.rules import RuleViolation
from gitlint.tests.base import BaseTestCase


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])