Skip to content

Commit 7b0c255

Browse files
authored
Integration tests: test match regex and ignore rules (#448)
Integration tests for rules that use regexes. The targetted rules don't really do anything unless they have a custom regex set. Since a default gitlint run doesn't meaningfully tests these rules, explicit tests for these rules ensure they work properly.
1 parent 1e7bc91 commit 7b0c255

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1: T5 Title contains the word 'WIP' (case-insensitive): "WIP: Commït Tïtle"
2+
3: B3 Line contains hard tab characters (\t): "Sïmple commit body"
3+
4: B2 Line has trailing whitespace: "Anōther Line "
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1: T5 Title contains the word 'WIP' (case-insensitive): "WIP: Commït Tïtle"
2+
3: B3 Line contains hard tab characters (\t): "Sïmple commit body"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1: T7 Title does not match regex (foo): "Thåt dûr bår"
2+
4: B8 Body does not match regex (bar)

qa/test_rules.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from qa.base import BaseTestCase
2+
from qa.shell import gitlint
3+
4+
5+
class RuleTests(BaseTestCase):
6+
"""
7+
Tests for specific rules that are worth testing as integration tests.
8+
It's not a goal to test every edge case of each rule, that's what the unit tests do.
9+
"""
10+
11+
def test_match_regex_rules(self):
12+
"""
13+
Test that T7 (title-match-regex) and B8 (body-match-regex) work as expected.
14+
By default, these rules don't do anything, only when setting a custom regex will they run.
15+
"""
16+
17+
commit_msg = "Thåt dûr bår\n\nSïmple commit message body"
18+
self.create_simple_commit(commit_msg)
19+
20+
# Assert violations when T7 and B8 regexes don't match
21+
output = gitlint("-c", "T7.regex=foo", "-c", "B8.regex=bar", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[2])
22+
self.assertEqualStdout(output, self.get_expected("test_rules/test_match_regex_rules_1"))
23+
24+
# Assert no violations when T7 and B8 regexes do match
25+
output = gitlint("-c", "T7.regex=^Thåt", "-c", "B8.regex=commit message", _cwd=self.tmp_git_repo, _tty_in=True)
26+
self.assertEqualStdout(output, "")
27+
28+
def test_ignore_rules(self):
29+
"""
30+
Test that ignore rules work as expected:
31+
ignore-by-title, ignore-by-body, ignore-by-author-name, ignore-body-lines
32+
By default, these rules don't do anything, only when setting a custom regex will they run.
33+
"""
34+
commit_msg = "WIP: Commït Tïtle\n\nSïmple commit\tbody\nAnōther Line \nLåst Line"
35+
self.create_simple_commit(commit_msg)
36+
37+
# Assert violations when not ignoring anything
38+
output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3])
39+
self.assertEqualStdout(output, self.get_expected("test_rules/test_ignore_rules_1"))
40+
41+
# Simple convenience function that passes in common arguments for this test
42+
def invoke_gitlint(*args, **kwargs):
43+
return gitlint(
44+
*args, "-c", "general.regex-style-search=True", **kwargs, _cwd=self.tmp_git_repo, _tty_in=True
45+
)
46+
47+
# ignore-by-title
48+
output = invoke_gitlint("-c", "ignore-by-title.regex=Commït")
49+
self.assertEqualStdout(output, "")
50+
51+
# ignore-by-body
52+
output = invoke_gitlint("-c", "ignore-by-body.regex=Anōther Line")
53+
self.assertEqualStdout(output, "")
54+
55+
# ignore-by-author-name
56+
output = invoke_gitlint("-c", "ignore-by-author-name.regex=gitlint-test-user")
57+
self.assertEqualStdout(output, "")
58+
59+
# ignore-body-lines
60+
output = invoke_gitlint("-c", "ignore-body-lines.regex=^Anōther", _ok_code=[2])
61+
self.assertEqualStdout(output, self.get_expected("test_rules/test_ignore_rules_2"))

0 commit comments

Comments
 (0)