summaryrefslogtreecommitdiffstats
path: root/gitlint-core/gitlint/tests/rules/test_configuration_rules.py
blob: 9e3b07c5015206cfdaaf966e5ea4f1239dafa67b (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from gitlint.tests.base import BaseTestCase, EXPECTED_REGEX_STYLE_SEARCH_DEPRECATION_WARNING
from gitlint import rules
from gitlint.config import LintConfig


class ConfigurationRuleTests(BaseTestCase):
    def test_ignore_by_title(self):
        commit = self.gitcommit("Releäse\n\nThis is the secōnd body line")

        # No regex specified -> Config shouldn't be changed
        rule = rules.IgnoreByTitle()
        config = LintConfig()
        rule.apply(config, commit)
        self.assertEqual(config, LintConfig())
        self.assert_logged([])  # nothing logged -> nothing ignored

        # Matching regex -> expect config to ignore all rules
        rule = rules.IgnoreByTitle({"regex": "^Releäse(.*)"})
        expected_config = LintConfig()
        expected_config.ignore = "all"
        rule.apply(config, commit)
        self.assertEqual(config, expected_config)

        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"})
        expected_config = LintConfig()
        expected_config.ignore = "T1,B2"
        rule.apply(config, commit)
        self.assertEqual(config, expected_config)

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

        # No regex specified -> Config shouldn't be changed
        rule = rules.IgnoreByBody()
        config = LintConfig()
        rule.apply(config, commit)
        self.assertEqual(config, LintConfig())
        self.assert_logged([])  # nothing logged -> nothing ignored

        # Matching regex -> expect config to ignore all rules
        rule = rules.IgnoreByBody({"regex": "(.*)relëase(.*)"})
        expected_config = LintConfig()
        expected_config.ignore = "all"
        rule.apply(config, commit)
        self.assertEqual(config, expected_config)

        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"})
        expected_config = LintConfig()
        expected_config.ignore = "T1,B2"
        rule.apply(config, commit)
        self.assertEqual(config, expected_config)

        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_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")

        # No regex specified -> Config shouldn't be changed
        rule = rules.IgnoreByAuthorName()
        config = LintConfig()
        rule.apply(config, commit)
        self.assertEqual(config, LintConfig())
        self.assert_logged([])  # nothing logged -> nothing ignored

        # Matching regex -> expect config to ignore all rules
        rule = rules.IgnoreByAuthorName({"regex": "(.*)ëst(.*)"})
        expected_config = LintConfig()
        expected_config.ignore = "all"
        rule.apply(config, commit)
        self.assertEqual(config, expected_config)

        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"})
        expected_config = LintConfig()
        expected_config.ignore = "T1,B2"
        rule.apply(config, commit)
        self.assertEqual(config, expected_config)

        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")
        commit2 = self.gitcommit("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": "(.*)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("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
        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")
        rule = rules.IgnoreBodyLines({"regex": "(.*)föobar(.*)"})
        config = LintConfig()
        rule.apply(config, commit1)
        self.assertEqual(commit1, commit2)
        self.assertEqual(config, LintConfig())  # config shouldn't have been modified