Debian Codegraph @

qa/test_config.py

raw symbols SCIP
1 import os
2 import re
3
4 from qa.base import BaseTestCase
5 from qa.shell import gitlint
6
7
8 class ConfigTests(BaseTestCase):
9 """Integration tests for gitlint configuration and configuration precedence."""
10
11 def test_ignore_by_id(self):
12 self.create_simple_commit("WIP: Thïs is a title.\nContënt on the second line")
13 output = gitlint("--ignore", "T5,B4", _tty_in=True, _cwd=self.tmp_git_repo, _ok_code=[1])
14 expected = '1: T3 Title has trailing punctuation (.): "WIP: Thïs is a title."\n'
15 self.assertEqualStdout(output, expected)
16
17 def test_ignore_by_name(self):
18 self.create_simple_commit("WIP: Thïs is a title.\nContënt on the second line")
19 output = gitlint(
20 "--ignore",
21 "title-must-not-contain-word,body-first-line-empty",
22 _cwd=self.tmp_git_repo,
23 _tty_in=True,
24 _ok_code=[1],
25 )
26 expected = '1: T3 Title has trailing punctuation (.): "WIP: Thïs is a title."\n'
27 self.assertEqualStdout(output, expected)
28
29 def test_verbosity(self):
30 self.create_simple_commit("WIP: Thïs is a title.\nContënt on the second line")
31 output = gitlint("-v", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3])
32
33 expected = "1: T3\n1: T5\n2: B4\n"
34 self.assertEqualStdout(output, expected)
35
36 output = gitlint("-vv", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3])
37 self.assertEqualStdout(output, self.get_expected("test_config/test_verbosity_1"))
38
39 output = gitlint("-vvv", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3])
40 self.assertEqualStdout(output, self.get_expected("test_config/test_verbosity_2"))
41
42 # test silent mode
43 output = gitlint("--silent", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3])
44 self.assertEqualStdout(output, "")
45
46 def test_set_rule_option(self):
47 self.create_simple_commit("This ïs a title.")
48 output = gitlint("-c", "title-max-length.line-length=5", _tty_in=True, _cwd=self.tmp_git_repo, _ok_code=[3])
49 self.assertEqualStdout(output, self.get_expected("test_config/test_set_rule_option_1"))
50
51 def test_config_from_file(self):
52 commit_msg = (
53 "WIP: Thïs is a title thåt is a bit longer.\nContent on the second line\n"
54 "This line of the body is here because we need it"
55 )
56 self.create_simple_commit(commit_msg)
57 config_path = self.get_sample_path("config/gitlintconfig")
58 output = gitlint("--config", config_path, _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[5])
59 self.assertEqualStdout(output, self.get_expected("test_config/test_config_from_file_1"))
60
61 def test_config_from_file_debug(self):
62 # Test both on existing and new repo (we've had a bug in the past that was unique to empty repos)
63 repos = [self.tmp_git_repo, self.create_tmp_git_repo()]
64 for target_repo in repos:
65 commit_msg = (
66 "WIP: Thïs is a title thåt is a bit longer.\nContent on the second line\n"
67 "This line of the body is here because we need it"
68 )
69 filename = self.create_simple_commit(commit_msg, git_repo=target_repo)
70 config_path = self.get_sample_path(os.path.join("config", "gitlintconfig"))
71 output = gitlint("--config", config_path, "--debug", _cwd=target_repo, _tty_in=True, _ok_code=[5])
72
73 expected_kwargs = self.get_debug_vars_last_commit(git_repo=target_repo)
74 expected_kwargs.update(
75 {
76 "config_path": config_path,
77 "changed_files": [filename],
78 "changed_files_stats": f"{filename}: 0 additions, 0 deletions",
79 }
80 )
81 self.assertEqualStdout(
82 output, self.get_expected("test_config/test_config_from_file_debug_1", expected_kwargs)
83 )
84
85 def test_config_from_env(self):
86 """Test for configuring gitlint from environment variables"""
87
88 # We invoke gitlint, configuring it via env variables, we can check whether gitlint picks these up correctly
89 # by comparing the debug output with what we'd expect
90 target_repo = self.create_tmp_git_repo()
91 commit_msg = (
92 "WIP: Thïs is a title thåt is a bit longer.\nContent on the second line\n"
93 "This line of the body is here because we need it"
94 )
95 filename = self.create_simple_commit(commit_msg, git_repo=target_repo)
96 env = self.create_environment(
97 {
98 "GITLINT_DEBUG": "1",
99 "GITLINT_VERBOSITY": "2",
100 "GITLINT_IGNORE": "T1,T2",
101 "GITLINT_CONTRIB": "CC1,CT1",
102 "GITLINT_FAIL_WITHOUT_COMMITS": "1",
103 "GITLINT_IGNORE_STDIN": "1",
104 "GITLINT_TARGET": target_repo,
105 "GITLINT_COMMITS": self.get_last_commit_hash(git_repo=target_repo),
106 }
107 )
108 output = gitlint(_env=env, _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[5])
109 expected_kwargs = self.get_debug_vars_last_commit(git_repo=target_repo)
110 expected_kwargs.update(
111 {"changed_files": [filename], "changed_files_stats": f"{filename}: 0 additions, 0 deletions"}
112 )
113
114 self.assertEqualStdout(output, self.get_expected("test_config/test_config_from_env_1", expected_kwargs))
115
116 # For some env variables, we need a separate test ast they are mutually exclusive with the ones tested above
117 tmp_commit_msg_file = self.create_tmpfile("WIP: msg-fïlename test.")
118 env = self.create_environment(
119 {"GITLINT_DEBUG": "1", "GITLINT_TARGET": target_repo, "GITLINT_SILENT": "1", "GITLINT_STAGED": "1"}
120 )
121
122 output = gitlint(
123 "--msg-filename", tmp_commit_msg_file, _env=env, _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3]
124 )
125
126 # Extract date from actual output to insert it into the expected output
127 # We have to do this since there's no way for us to deterministically know that date otherwise
128 p = re.compile("Date: (.*)\n", re.UNICODE | re.MULTILINE)
129 result = p.search(str(output))
130 date = result.group(1).strip()
131 expected_kwargs.update({"date": date})
132
133 self.assertEqualStdout(output, self.get_expected("test_config/test_config_from_env_2", expected_kwargs))