| 1 |
import re |
| 2 |
|
| 3 |
import arrow |
| 4 |
|
| 5 |
from qa.base import BaseTestCase |
| 6 |
from qa.shell import echo, git, gitlint |
| 7 |
|
| 8 |
|
| 9 |
class CommitsTests(BaseTestCase): |
| 10 |
"""Integration tests for the --commits argument, i.e. linting multiple commits or linting specific commits""" |
| 11 |
|
| 12 |
def test_successful(self): |
| 13 |
"""Test linting multiple commits without violations""" |
| 14 |
git("checkout", "-b", "test-branch-commits-base", _cwd=self.tmp_git_repo) |
| 15 |
self.create_simple_commit("Sïmple title\n\nSimple bödy describing the commit") |
| 16 |
git("checkout", "-b", "test-branch-commits", _cwd=self.tmp_git_repo) |
| 17 |
self.create_simple_commit("Sïmple title2\n\nSimple bödy describing the commit2") |
| 18 |
self.create_simple_commit("Sïmple title3\n\nSimple bödy describing the commit3") |
| 19 |
output = gitlint( |
| 20 |
"--commits", "test-branch-commits-base...test-branch-commits", _cwd=self.tmp_git_repo, _tty_in=True |
| 21 |
) |
| 22 |
self.assertEqualStdout(output, "") |
| 23 |
|
| 24 |
def test_violations(self): |
| 25 |
"""Test linting multiple commits with violations""" |
| 26 |
git("checkout", "-b", "test-branch-commits-violations-base", _cwd=self.tmp_git_repo) |
| 27 |
self.create_simple_commit("Sïmple title.\n") |
| 28 |
git("checkout", "-b", "test-branch-commits-violations", _cwd=self.tmp_git_repo) |
| 29 |
|
| 30 |
self.create_simple_commit("Sïmple title2.\n") |
| 31 |
commit_sha1 = self.get_last_commit_hash()[:10] |
| 32 |
self.create_simple_commit("Sïmple title3.\n") |
| 33 |
commit_sha2 = self.get_last_commit_hash()[:10] |
| 34 |
output = gitlint( |
| 35 |
"--commits", |
| 36 |
"test-branch-commits-violations-base...test-branch-commits-violations", |
| 37 |
_cwd=self.tmp_git_repo, |
| 38 |
_tty_in=True, |
| 39 |
_ok_code=[4], |
| 40 |
) |
| 41 |
|
| 42 |
self.assertEqual(output.exit_code, 4) |
| 43 |
expected_kwargs = {"commit_sha1": commit_sha1, "commit_sha2": commit_sha2} |
| 44 |
self.assertEqualStdout(output, self.get_expected("test_commits/test_violations_1", expected_kwargs)) |
| 45 |
|
| 46 |
def test_csv_hash_list(self): |
| 47 |
"""Test linting multiple commits (comma-separated) with violations""" |
| 48 |
git("checkout", "-b", "test-branch-commits-violations-base", _cwd=self.tmp_git_repo) |
| 49 |
self.create_simple_commit("Sïmple title1.\n") |
| 50 |
commit_sha1 = self.get_last_commit_hash()[:10] |
| 51 |
git("checkout", "-b", "test-branch-commits-violations", _cwd=self.tmp_git_repo) |
| 52 |
|
| 53 |
self.create_simple_commit("Sïmple title2.\n") |
| 54 |
commit_sha2 = self.get_last_commit_hash()[:10] |
| 55 |
self.create_simple_commit("Sïmple title3.\n") |
| 56 |
self.create_simple_commit("Sïmple title4.\n") |
| 57 |
commit_sha4 = self.get_last_commit_hash()[:10] |
| 58 |
|
| 59 |
|
| 60 |
output = gitlint( |
| 61 |
"--commits", |
| 62 |
f"{commit_sha2},{commit_sha1},{commit_sha4}", |
| 63 |
_cwd=self.tmp_git_repo, |
| 64 |
_tty_in=True, |
| 65 |
_ok_code=[6], |
| 66 |
) |
| 67 |
|
| 68 |
self.assertEqual(output.exit_code, 6) |
| 69 |
expected_kwargs = {"commit_sha1": commit_sha1, "commit_sha2": commit_sha2, "commit_sha4": commit_sha4} |
| 70 |
self.assertEqualStdout(output, self.get_expected("test_commits/test_csv_hash_list_1", expected_kwargs)) |
| 71 |
|
| 72 |
def test_lint_empty_commit_range(self): |
| 73 |
"""Tests `gitlint --commits <sha>^...<sha>` --fail-without-commits where the provided range is empty.""" |
| 74 |
self.create_simple_commit("Sïmple title.\n") |
| 75 |
self.create_simple_commit("Sïmple title2.\n") |
| 76 |
commit_sha = self.get_last_commit_hash() |
| 77 |
|
| 78 |
refspec = f"{commit_sha}..{commit_sha}" |
| 79 |
|
| 80 |
|
| 81 |
output = gitlint("--commits", refspec, _cwd=self.tmp_git_repo, _tty_in=True) |
| 82 |
self.assertEqual(output.exit_code, 0) |
| 83 |
self.assertEqualStdout(output, "") |
| 84 |
|
| 85 |
|
| 86 |
output = gitlint( |
| 87 |
"--commits", |
| 88 |
refspec, |
| 89 |
"--fail-without-commits", |
| 90 |
_cwd=self.tmp_git_repo, |
| 91 |
_tty_in=True, |
| 92 |
_ok_code=[self.GITLINT_USAGE_ERROR], |
| 93 |
) |
| 94 |
self.assertEqual(output.exit_code, self.GITLINT_USAGE_ERROR) |
| 95 |
self.assertEqualStdout(output, f'Error: No commits in range "{refspec}"\n') |
| 96 |
|
| 97 |
def test_lint_single_commit(self): |
| 98 |
"""Tests `gitlint --commits <sha>^...<same sha>`""" |
| 99 |
self.create_simple_commit("Sïmple title.\n") |
| 100 |
first_commit_sha = self.get_last_commit_hash() |
| 101 |
self.create_simple_commit("Sïmple title2.\n") |
| 102 |
commit_sha = self.get_last_commit_hash() |
| 103 |
refspec = f"{commit_sha}^...{commit_sha}" |
| 104 |
self.create_simple_commit("Sïmple title3.\n") |
| 105 |
|
| 106 |
expected = '1: T3 Title has trailing punctuation (.): "Sïmple title2."\n' + "3: B6 Body message is missing\n" |
| 107 |
|
| 108 |
|
| 109 |
output = gitlint("--commit", commit_sha, _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[2]) |
| 110 |
self.assertEqual(output.exit_code, 2) |
| 111 |
self.assertEqualStdout(output, expected) |
| 112 |
|
| 113 |
|
| 114 |
output = gitlint("--commits", f"{commit_sha},", _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[2]) |
| 115 |
self.assertEqual(output.exit_code, 2) |
| 116 |
self.assertEqualStdout(output, expected) |
| 117 |
|
| 118 |
|
| 119 |
output = gitlint("--commits", refspec, _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[2]) |
| 120 |
self.assertEqual(output.exit_code, 2) |
| 121 |
self.assertEqualStdout(output, expected) |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
expected = '1: T3 Title has trailing punctuation (.): "Sïmple title."\n' + "3: B6 Body message is missing\n" |
| 126 |
output = gitlint("--commit", first_commit_sha, _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[2]) |
| 127 |
self.assertEqual(output.exit_code, 2) |
| 128 |
self.assertEqualStdout(output, expected) |
| 129 |
|
| 130 |
|
| 131 |
refspec = f"{first_commit_sha}^...{first_commit_sha}" |
| 132 |
output = gitlint("--commits", refspec, _cwd=self.tmp_git_repo, _tty_in=True, _ok_code=[254]) |
| 133 |
self.assertEqual(output.exit_code, 254) |
| 134 |
|
| 135 |
def test_lint_staged_stdin(self): |
| 136 |
"""Tests linting a staged commit. Gitint should lint the passed commit message and fetch additional meta-data |
| 137 |
from the underlying repository. The easiest way to test this is by inspecting `--debug` output. |
| 138 |
This is the equivalent of doing: |
| 139 |
echo "WIP: Pïpe test." | gitlint --staged --debug |
| 140 |
""" |
| 141 |
|
| 142 |
self.create_simple_commit("Sïmple title.\n") |
| 143 |
|
| 144 |
|
| 145 |
filename1 = self.create_file(self.tmp_git_repo) |
| 146 |
git("add", filename1, _cwd=self.tmp_git_repo) |
| 147 |
filename2 = self.create_file(self.tmp_git_repo) |
| 148 |
git("add", filename2, _cwd=self.tmp_git_repo) |
| 149 |
|
| 150 |
output = gitlint( |
| 151 |
echo("WIP: Pïpe test."), |
| 152 |
"--staged", |
| 153 |
"--debug", |
| 154 |
_cwd=self.tmp_git_repo, |
| 155 |
_tty_in=False, |
| 156 |
_err_to_out=True, |
| 157 |
_ok_code=[3], |
| 158 |
) |
| 159 |
|
| 160 |
|
| 161 |
expected_kwargs = self.get_debug_vars_last_commit() |
| 162 |
filenames = sorted([filename1, filename2]) |
| 163 |
expected_kwargs.update( |
| 164 |
{ |
| 165 |
"changed_files": filenames, |
| 166 |
"changed_files_stats": ( |
| 167 |
f"{filenames[0]}: 0 additions, 0 deletions\n {filenames[1]}: 0 additions, 0 deletions" |
| 168 |
), |
| 169 |
} |
| 170 |
) |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
matches = re.search(r"^Date:\s+(.*)", str(output), re.MULTILINE) |
| 177 |
if matches: |
| 178 |
expected_date = arrow.get(str(matches.group(1)), "YYYY-MM-DD HH:mm:ss Z").format("YYYY-MM-DD HH:mm:ss Z") |
| 179 |
expected_kwargs["staged_date"] = expected_date |
| 180 |
|
| 181 |
self.assertEqualStdout(output, self.get_expected("test_commits/test_lint_staged_stdin_1", expected_kwargs)) |
| 182 |
self.assertEqual(output.exit_code, 3) |
| 183 |
|
| 184 |
def test_lint_staged_msg_filename(self): |
| 185 |
"""Tests linting a staged commit. Gitint should lint the passed commit message andfetch additional meta-data |
| 186 |
from the underlying repository. The easiest way to test this is by inspecting `--debug` output. |
| 187 |
This is the equivalent of doing: |
| 188 |
gitlint --msg-filename /tmp/my-commit-msg --staged --debug |
| 189 |
""" |
| 190 |
|
| 191 |
self.create_simple_commit("Sïmple title.\n") |
| 192 |
|
| 193 |
|
| 194 |
filename1 = self.create_file(self.tmp_git_repo) |
| 195 |
git("add", filename1, _cwd=self.tmp_git_repo) |
| 196 |
filename2 = self.create_file(self.tmp_git_repo) |
| 197 |
git("add", filename2, _cwd=self.tmp_git_repo) |
| 198 |
|
| 199 |
tmp_commit_msg_file = self.create_tmpfile("WIP: from fïle test.") |
| 200 |
|
| 201 |
output = gitlint( |
| 202 |
"--msg-filename", |
| 203 |
tmp_commit_msg_file, |
| 204 |
"--staged", |
| 205 |
"--debug", |
| 206 |
_cwd=self.tmp_git_repo, |
| 207 |
_tty_in=False, |
| 208 |
_err_to_out=True, |
| 209 |
_ok_code=[3], |
| 210 |
) |
| 211 |
|
| 212 |
|
| 213 |
expected_kwargs = self.get_debug_vars_last_commit() |
| 214 |
filenames = sorted([filename1, filename2]) |
| 215 |
expected_kwargs.update( |
| 216 |
{ |
| 217 |
"changed_files": filenames, |
| 218 |
"changed_files_stats": ( |
| 219 |
f"{filenames[0]}: 0 additions, 0 deletions\n {filenames[1]}: 0 additions, 0 deletions" |
| 220 |
), |
| 221 |
} |
| 222 |
) |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
matches = re.search(r"^Date:\s+(.*)", str(output), re.MULTILINE) |
| 229 |
if matches: |
| 230 |
expected_date = arrow.get(str(matches.group(1)), "YYYY-MM-DD HH:mm:ss Z").format("YYYY-MM-DD HH:mm:ss Z") |
| 231 |
expected_kwargs["staged_date"] = expected_date |
| 232 |
|
| 233 |
expected = self.get_expected("test_commits/test_lint_staged_msg_filename_1", expected_kwargs) |
| 234 |
self.assertEqualStdout(output, expected) |
| 235 |
self.assertEqual(output.exit_code, 3) |
| 236 |
|
| 237 |
def test_lint_head(self): |
| 238 |
"""Testing whether we can also recognize special refs like 'HEAD'""" |
| 239 |
tmp_git_repo = self.create_tmp_git_repo() |
| 240 |
self.create_simple_commit("Sïmple title.\n\nSimple bödy describing the commit", git_repo=tmp_git_repo) |
| 241 |
self.create_simple_commit("Sïmple title", git_repo=tmp_git_repo) |
| 242 |
self.create_simple_commit("WIP: Sïmple title\n\nSimple bödy describing the commit", git_repo=tmp_git_repo) |
| 243 |
output = gitlint("--commits", "HEAD", _cwd=tmp_git_repo, _tty_in=True, _ok_code=[3]) |
| 244 |
revlist = git("rev-list", "HEAD", _tty_in=True, _cwd=tmp_git_repo).split() |
| 245 |
|
| 246 |
expected_kwargs = { |
| 247 |
"commit_sha0": revlist[0][:10], |
| 248 |
"commit_sha1": revlist[1][:10], |
| 249 |
"commit_sha2": revlist[2][:10], |
| 250 |
} |
| 251 |
|
| 252 |
self.assertEqualStdout(output, self.get_expected("test_commits/test_lint_head_1", expected_kwargs)) |
| 253 |
|
| 254 |
def test_ignore_commits(self): |
| 255 |
"""Tests multiple commits of which some rules get ignored because of ignore-* rules""" |
| 256 |
|
| 257 |
tmp_git_repo = self.create_tmp_git_repo() |
| 258 |
self.create_simple_commit("Sïmple title.\n\nSimple bödy describing the commit", git_repo=tmp_git_repo) |
| 259 |
|
| 260 |
|
| 261 |
self.create_simple_commit("Release: WIP tïtle.\n\nShort", git_repo=tmp_git_repo) |
| 262 |
|
| 263 |
self.create_simple_commit("Sïmple WIP title3.\n\nThis is \ta relëase commit\nMore info", git_repo=tmp_git_repo) |
| 264 |
self.create_simple_commit("Sïmple title4.\n\nSimple bödy describing the commit4", git_repo=tmp_git_repo) |
| 265 |
revlist = git("rev-list", "HEAD", _tty_in=True, _cwd=tmp_git_repo).split() |
| 266 |
|
| 267 |
config_path = self.get_sample_path("config/ignore-release-commits") |
| 268 |
output = gitlint("--commits", "HEAD", "--config", config_path, _cwd=tmp_git_repo, _tty_in=True, _ok_code=[4]) |
| 269 |
|
| 270 |
expected_kwargs = { |
| 271 |
"commit_sha0": revlist[0][:10], |
| 272 |
"commit_sha1": revlist[1][:10], |
| 273 |
"commit_sha2": revlist[2][:10], |
| 274 |
"commit_sha3": revlist[3][:10], |
| 275 |
} |
| 276 |
self.assertEqualStdout(output, self.get_expected("test_commits/test_ignore_commits_1", expected_kwargs)) |