| 1 |
import os |
| 2 |
|
| 3 |
from qa.base import BaseTestCase |
| 4 |
from qa.shell import echo, git, gitlint |
| 5 |
from qa.utils import FILE_ENCODING |
| 6 |
|
| 7 |
|
| 8 |
class IntegrationTests(BaseTestCase): |
| 9 |
"""Simple set of integration tests for gitlint""" |
| 10 |
|
| 11 |
def test_successful(self): |
| 12 |
|
| 13 |
self.create_simple_commit("Sïmple title\n\nSimple bödy describing the commit") |
| 14 |
output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True, _err_to_out=True) |
| 15 |
self.assertEqualStdout(output, "") |
| 16 |
|
| 17 |
def test_successful_gitconfig(self): |
| 18 |
"""Test gitlint when the underlying repo has specific git config set. |
| 19 |
In the past, we've had issues with gitlint failing on some of these, so this acts as a regression test.""" |
| 20 |
|
| 21 |
|
| 22 |
git("config", "--add", "core.commentchar", "$", _cwd=self.tmp_git_repo) |
| 23 |
self.create_simple_commit("Sïmple title\n\nSimple bödy describing the commit\n$after commentchar\t ignored") |
| 24 |
output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True, _err_to_out=True) |
| 25 |
self.assertEqualStdout(output, "") |
| 26 |
|
| 27 |
def test_successful_merge_commit(self): |
| 28 |
|
| 29 |
self.create_simple_commit("Cömmit on main\n\nSimple bödy") |
| 30 |
|
| 31 |
|
| 32 |
git("checkout", "-b", "test-branch", _cwd=self.tmp_git_repo) |
| 33 |
git("checkout", "test-branch", _cwd=self.tmp_git_repo) |
| 34 |
commit_title = "Commit on test-brånch with a pretty long title that will cause issues when merging" |
| 35 |
self.create_simple_commit(f"{commit_title}\n\nSïmple body") |
| 36 |
hash = self.get_last_commit_hash() |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
git("checkout", "main", _cwd=self.tmp_git_repo) |
| 42 |
git("merge", "--no-ff", "-m", f"Merge '{commit_title}'", hash, _cwd=self.tmp_git_repo) |
| 43 |
|
| 44 |
|
| 45 |
output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True) |
| 46 |
self.assertEqualStdout(output, "") |
| 47 |
|
| 48 |
|
| 49 |
output = gitlint("-c", "general.ignore-merge-commits=false", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[1]) |
| 50 |
self.assertEqual(output.exit_code, 1) |
| 51 |
self.assertEqualStdout(output, f"1: T1 Title exceeds max length (90>72): \"Merge '{commit_title}'\"\n") |
| 52 |
|
| 53 |
def test_fixup_commit(self): |
| 54 |
|
| 55 |
test_filename = self.create_simple_commit("Cömmit on WIP main\n\nSimple bödy that is long enough") |
| 56 |
output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[1]) |
| 57 |
expected = "1: T5 Title contains the word 'WIP' (case-insensitive): \"Cömmit on WIP main\"\n" |
| 58 |
self.assertEqualStdout(output, expected) |
| 59 |
|
| 60 |
|
| 61 |
with open(os.path.join(self.tmp_git_repo, test_filename), "a", encoding=FILE_ENCODING) as fh: |
| 62 |
fh.write("Appending söme stuff\n") |
| 63 |
|
| 64 |
git("add", test_filename, _cwd=self.tmp_git_repo) |
| 65 |
|
| 66 |
git("commit", "--fixup", self.get_last_commit_hash(), _cwd=self.tmp_git_repo) |
| 67 |
|
| 68 |
|
| 69 |
output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True) |
| 70 |
|
| 71 |
self.assertEqualStdout(output, "") |
| 72 |
|
| 73 |
|
| 74 |
output = gitlint("-c", "general.ignore-fixup-commits=false", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[2]) |
| 75 |
expected = ( |
| 76 |
"1: T5 Title contains the word 'WIP' (case-insensitive): \"fixup! Cömmit on WIP main\"\n" |
| 77 |
"3: B6 Body message is missing\n" |
| 78 |
) |
| 79 |
|
| 80 |
self.assertEqualStdout(output, expected) |
| 81 |
|
| 82 |
def test_fixup_amend_commit(self): |
| 83 |
|
| 84 |
test_filename = self.create_simple_commit("Cömmit on WIP main\n\nSimple bödy that is long enough") |
| 85 |
output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[1]) |
| 86 |
expected = "1: T5 Title contains the word 'WIP' (case-insensitive): \"Cömmit on WIP main\"\n" |
| 87 |
self.assertEqualStdout(output, expected) |
| 88 |
|
| 89 |
|
| 90 |
with open(os.path.join(self.tmp_git_repo, test_filename), "a", encoding=FILE_ENCODING) as fh: |
| 91 |
fh.write("Appending söme stuff\n") |
| 92 |
|
| 93 |
git("add", test_filename, _cwd=self.tmp_git_repo) |
| 94 |
|
| 95 |
|
| 96 |
git("commit", "--no-edit", f"--fixup=amend:{self.get_last_commit_hash()}", _cwd=self.tmp_git_repo) |
| 97 |
|
| 98 |
|
| 99 |
output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True) |
| 100 |
|
| 101 |
self.assertEqualStdout(output, "") |
| 102 |
|
| 103 |
|
| 104 |
output = gitlint( |
| 105 |
"-c", "general.ignore-fixup-amend-commits=false", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[1] |
| 106 |
) |
| 107 |
expected = "1: T5 Title contains the word 'WIP' (case-insensitive): \"amend! Cömmit on WIP main\"\n" |
| 108 |
|
| 109 |
self.assertEqualStdout(output, expected) |
| 110 |
|
| 111 |
def test_revert_commit(self): |
| 112 |
self.create_simple_commit("WIP: Cömmit on main.\n\nSimple bödy") |
| 113 |
hash = self.get_last_commit_hash() |
| 114 |
git("revert", hash, _cwd=self.tmp_git_repo) |
| 115 |
|
| 116 |
|
| 117 |
output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True) |
| 118 |
self.assertEqualStdout(output, "") |
| 119 |
|
| 120 |
|
| 121 |
output = gitlint( |
| 122 |
"-c", "general.ignore-revert-commits=false", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[1] |
| 123 |
) |
| 124 |
self.assertEqual(output.exit_code, 1) |
| 125 |
expected = '1: T5 Title contains the word \'WIP\' (case-insensitive): "Revert "WIP: Cömmit on main.""\n' |
| 126 |
self.assertEqualStdout(output, expected) |
| 127 |
|
| 128 |
def test_squash_commit(self): |
| 129 |
|
| 130 |
test_filename = self.create_simple_commit("Cömmit on WIP main\n\nSimple bödy that is long enough") |
| 131 |
output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[1]) |
| 132 |
expected = "1: T5 Title contains the word 'WIP' (case-insensitive): \"Cömmit on WIP main\"\n" |
| 133 |
self.assertEqualStdout(output, expected) |
| 134 |
|
| 135 |
|
| 136 |
with open(os.path.join(self.tmp_git_repo, test_filename), "a", encoding=FILE_ENCODING) as fh: |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
fh.write("Appending some stuff\n") |
| 142 |
|
| 143 |
git("add", test_filename, _cwd=self.tmp_git_repo) |
| 144 |
|
| 145 |
git("commit", "--squash", self.get_last_commit_hash(), "-m", "Töo short body", _cwd=self.tmp_git_repo) |
| 146 |
|
| 147 |
|
| 148 |
output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True) |
| 149 |
|
| 150 |
self.assertEqualStdout(output, "") |
| 151 |
|
| 152 |
|
| 153 |
output = gitlint( |
| 154 |
"-c", "general.ignore-squash-commits=false", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[2] |
| 155 |
) |
| 156 |
expected = ( |
| 157 |
"1: T5 Title contains the word 'WIP' (case-insensitive): \"squash! Cömmit on WIP main\"\n" |
| 158 |
'3: B5 Body message is too short (14<20): "Töo short body"\n' |
| 159 |
) |
| 160 |
|
| 161 |
self.assertEqualStdout(output, expected) |
| 162 |
|
| 163 |
def test_violations(self): |
| 164 |
commit_msg = "WIP: This ïs a title.\nContent on the sëcond line" |
| 165 |
self.create_simple_commit(commit_msg) |
| 166 |
output = gitlint(_cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[3]) |
| 167 |
self.assertEqualStdout(output, self.get_expected("test_gitlint/test_violations_1")) |
| 168 |
|
| 169 |
def test_msg_filename(self): |
| 170 |
tmp_commit_msg_file = self.create_tmpfile("WIP: msg-fïlename test.") |
| 171 |
output = gitlint("--msg-filename", tmp_commit_msg_file, _tty_in=True, _cwd=self.tmp_git_repo, _ok_code=[3]) |
| 172 |
self.assertEqualStdout(output, self.get_expected("test_gitlint/test_msg_filename_1")) |
| 173 |
|
| 174 |
def test_msg_filename_no_tty(self): |
| 175 |
"""Make sure --msg-filename option also works with no TTY attached""" |
| 176 |
tmp_commit_msg_file = self.create_tmpfile("WIP: msg-fïlename NO TTY test.") |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
output = gitlint( |
| 184 |
"--msg-filename", |
| 185 |
tmp_commit_msg_file, |
| 186 |
_cwd=self.tmp_git_repo, |
| 187 |
_in=" ", |
| 188 |
_tty_in=False, |
| 189 |
_err_to_out=True, |
| 190 |
_ok_code=[3], |
| 191 |
) |
| 192 |
|
| 193 |
self.assertEqualStdout(output, self.get_expected("test_gitlint/test_msg_filename_no_tty_1")) |
| 194 |
|
| 195 |
def test_no_git_name_set(self): |
| 196 |
"""Ensure we print out a helpful message if user.name is not set""" |
| 197 |
tmp_commit_msg_file = self.create_tmpfile("WIP: msg-fïlename NO name test.") |
| 198 |
|
| 199 |
|
| 200 |
env = self.create_tmp_git_config("[user]\n email = test-emå[email protected]m\n") |
| 201 |
output = gitlint( |
| 202 |
"--staged", |
| 203 |
"--msg-filename", |
| 204 |
tmp_commit_msg_file, |
| 205 |
_ok_code=[self.GIT_CONTEXT_ERROR_CODE], |
| 206 |
_env=env, |
| 207 |
_cwd=self.tmp_git_repo, |
| 208 |
) |
| 209 |
expected = "Missing git configuration: please set user.name\n" |
| 210 |
self.assertEqualStdout(output, expected) |
| 211 |
|
| 212 |
def test_no_git_email_set(self): |
| 213 |
"""Ensure we print out a helpful message if user.email is not set""" |
| 214 |
tmp_commit_msg_file = self.create_tmpfile("WIP: msg-fïlename NO email test.") |
| 215 |
env = self.create_tmp_git_config("[user]\n name = test åuthor\n") |
| 216 |
output = gitlint( |
| 217 |
"--staged", |
| 218 |
"--msg-filename", |
| 219 |
tmp_commit_msg_file, |
| 220 |
_ok_code=[self.GIT_CONTEXT_ERROR_CODE], |
| 221 |
_env=env, |
| 222 |
_cwd=self.tmp_git_repo, |
| 223 |
) |
| 224 |
expected = "Missing git configuration: please set user.email\n" |
| 225 |
self.assertEqualStdout(output, expected) |
| 226 |
|
| 227 |
def test_git_empty_repo(self): |
| 228 |
|
| 229 |
empty_git_repo = self.create_tmp_git_repo() |
| 230 |
output = gitlint(_cwd=empty_git_repo, _tty_in=True, _ok_code=[self.GIT_CONTEXT_ERROR_CODE]) |
| 231 |
|
| 232 |
expected = "Current branch has no commits. Gitlint requires at least one commit to function.\n" |
| 233 |
self.assertEqualStdout(output, expected) |
| 234 |
|
| 235 |
def test_git_empty_repo_staged(self): |
| 236 |
"""When repo is empty, we can still use gitlint when using --staged flag and piping a message into it""" |
| 237 |
empty_git_repo = self.create_tmp_git_repo() |
| 238 |
expected = ( |
| 239 |
'1: T3 Title has trailing punctuation (.): "WIP: Pïpe test."\n' |
| 240 |
"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: Pïpe test.\"\n" |
| 241 |
"3: B6 Body message is missing\n" |
| 242 |
) |
| 243 |
|
| 244 |
output = gitlint( |
| 245 |
echo("WIP: Pïpe test."), "--staged", _cwd=empty_git_repo, _tty_in=False, _err_to_out=True, _ok_code=[3] |
| 246 |
) |
| 247 |
self.assertEqualStdout(output, expected) |
| 248 |
|
| 249 |
def test_commit_binary_file(self): |
| 250 |
"""When committing a binary file, git shows somewhat different output in diff commands, |
| 251 |
this test ensures gitlint deals with that correctly""" |
| 252 |
binary_filename = self.create_simple_commit("Sïmple commit", file_contents=bytes([0x48, 0x00, 0x49, 0x00])) |
| 253 |
output = gitlint( |
| 254 |
"--debug", |
| 255 |
_ok_code=[1], |
| 256 |
_cwd=self.tmp_git_repo, |
| 257 |
) |
| 258 |
|
| 259 |
expected_kwargs = self.get_debug_vars_last_commit() |
| 260 |
expected_kwargs.update( |
| 261 |
{ |
| 262 |
"changed_files": [binary_filename], |
| 263 |
"changed_files_stats": (f"{binary_filename}: None additions, None deletions"), |
| 264 |
} |
| 265 |
) |
| 266 |
expected = self.get_expected("test_gitlint/test_commit_binary_file_1", expected_kwargs) |
| 267 |
self.assertEqualStdout(output, expected) |