| 1 |
import os |
| 2 |
|
| 3 |
from qa.base import BaseTestCase |
| 4 |
from qa.shell import git, gitlint |
| 5 |
|
| 6 |
|
| 7 |
class HookTests(BaseTestCase): |
| 8 |
"""Integration tests for gitlint commitmsg hooks""" |
| 9 |
|
| 10 |
VIOLATIONS = [ |
| 11 |
"gitlint: checking commit message...\n", |
| 12 |
'1: T3 Title has trailing punctuation (.): "WIP: This ïs a title."\n', |
| 13 |
"1: T5 Title contains the word 'WIP' (case-insensitive): \"WIP: This ïs a title.\"\n", |
| 14 |
'2: B4 Second line is not empty: "Contënt on the second line"\n', |
| 15 |
"3: B6 Body message is missing\n", |
| 16 |
"-----------------------------------------------\n", |
| 17 |
"gitlint: \x1b[31mYour commit message contains violations.\x1b[0m\n", |
| 18 |
] |
| 19 |
|
| 20 |
def setUp(self): |
| 21 |
super().setUp() |
| 22 |
self.responses = [] |
| 23 |
self.response_index = 0 |
| 24 |
self.githook_output = [] |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
self.create_simple_commit("Commït Title\n\nCommit Body explaining commit.") |
| 30 |
|
| 31 |
|
| 32 |
output_installed = gitlint("install-hook", _cwd=self.tmp_git_repo) |
| 33 |
commit_msg_hook_path = os.path.join(self.tmp_git_repo, ".git", "hooks", "commit-msg") |
| 34 |
expected_installed = f"Successfully installed gitlint commit-msg hook in {commit_msg_hook_path}\n" |
| 35 |
|
| 36 |
self.assertEqualStdout(output_installed, expected_installed) |
| 37 |
|
| 38 |
def tearDown(self): |
| 39 |
|
| 40 |
output_uninstalled = gitlint("uninstall-hook", _cwd=self.tmp_git_repo) |
| 41 |
commit_msg_hook_path = os.path.join(self.tmp_git_repo, ".git", "hooks", "commit-msg") |
| 42 |
expected_uninstalled = f"Successfully uninstalled gitlint commit-msg hook from {commit_msg_hook_path}\n" |
| 43 |
|
| 44 |
self.assertEqualStdout(output_uninstalled, expected_uninstalled) |
| 45 |
super().tearDown() |
| 46 |
|
| 47 |
def _violations(self): |
| 48 |
|
| 49 |
return list(self.VIOLATIONS) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
def _interact(self, line, stdin): |
| 54 |
self.githook_output.append(line) |
| 55 |
|
| 56 |
if "Your commit message contains violations" in line: |
| 57 |
response = self.responses[self.response_index] |
| 58 |
stdin.put(f"{response}\n") |
| 59 |
self.response_index = (self.response_index + 1) % len(self.responses) |
| 60 |
|
| 61 |
def test_commit_hook_no_violations(self): |
| 62 |
test_filename = self.create_simple_commit( |
| 63 |
"This ïs a title\n\nBody contënt that should work", out=self._interact, tty_in=True |
| 64 |
) |
| 65 |
|
| 66 |
short_hash = self.get_last_commit_short_hash() |
| 67 |
expected_output = [ |
| 68 |
"gitlint: checking commit message...\n", |
| 69 |
"gitlint: \x1b[32mOK\x1b[0m (no violations in commit message)\n", |
| 70 |
f"[main {short_hash}] This ïs a title\n", |
| 71 |
" 1 file changed, 0 insertions(+), 0 deletions(-)\n", |
| 72 |
f" create mode 100644 {test_filename}\n", |
| 73 |
] |
| 74 |
for output, expected in zip(self.githook_output, expected_output): |
| 75 |
self.assertMultiLineEqual(output.replace("\r", ""), expected.replace("\r", "")) |
| 76 |
|
| 77 |
def test_commit_hook_continue(self): |
| 78 |
self.responses = ["y"] |
| 79 |
test_filename = self.create_simple_commit( |
| 80 |
"WIP: This ïs a title.\nContënt on the second line", out=self._interact, tty_in=True |
| 81 |
) |
| 82 |
|
| 83 |
|
| 84 |
short_hash = self.get_last_commit_short_hash() |
| 85 |
|
| 86 |
expected_output = self._violations() |
| 87 |
expected_output += [ |
| 88 |
"Continue with commit anyways (this keeps the current commit message)? " |
| 89 |
"[y(es)/n(no)/e(dit)] " |
| 90 |
f"[main {short_hash}] WIP: This ïs a title. Contënt on the second line\n", |
| 91 |
" 1 file changed, 0 insertions(+), 0 deletions(-)\n", |
| 92 |
f" create mode 100644 {test_filename}\n", |
| 93 |
] |
| 94 |
|
| 95 |
for output, expected in zip(self.githook_output, expected_output): |
| 96 |
self.assertMultiLineEqual(output.replace("\r", ""), expected.replace("\r", "")) |
| 97 |
|
| 98 |
def test_commit_hook_abort(self): |
| 99 |
self.responses = ["n"] |
| 100 |
test_filename = self.create_simple_commit( |
| 101 |
"WIP: This ïs a title.\nContënt on the second line", out=self._interact, ok_code=1, tty_in=True |
| 102 |
) |
| 103 |
git("rm", "-f", test_filename, _cwd=self.tmp_git_repo) |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
expected_output = self._violations() |
| 108 |
expected_output += [ |
| 109 |
"Continue with commit anyways (this keeps the current commit message)? " |
| 110 |
"[y(es)/n(no)/e(dit)] " |
| 111 |
"Commit aborted.\n", |
| 112 |
"Your commit message: \n", |
| 113 |
"-----------------------------------------------\n", |
| 114 |
"WIP: This ïs a title.\n", |
| 115 |
"Contënt on the second line\n", |
| 116 |
"-----------------------------------------------\n", |
| 117 |
] |
| 118 |
|
| 119 |
for output, expected in zip(self.githook_output, expected_output): |
| 120 |
self.assertMultiLineEqual(output.replace("\r", ""), expected.replace("\r", "")) |
| 121 |
|
| 122 |
def test_commit_hook_edit(self): |
| 123 |
self.responses = ["e", "y"] |
| 124 |
env = {"EDITOR": ":"} |
| 125 |
test_filename = self.create_simple_commit( |
| 126 |
"WIP: This ïs a title.\nContënt on the second line", out=self._interact, env=env, tty_in=True |
| 127 |
) |
| 128 |
git("rm", "-f", test_filename, _cwd=self.tmp_git_repo) |
| 129 |
|
| 130 |
short_hash = git("rev-parse", "--short", "HEAD", _cwd=self.tmp_git_repo, _tty_in=True).replace("\n", "") |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
expected_output = self._violations() |
| 135 |
expected_output += [ |
| 136 |
"Continue with commit anyways (this keeps the current commit message)? [y(es)/n(no)/e(dit)] " |
| 137 |
+ self._violations()[0] |
| 138 |
] |
| 139 |
expected_output += self._violations()[1:] |
| 140 |
expected_output += [ |
| 141 |
"Continue with commit anyways (this keeps the current commit message)? [y(es)/n(no)/e(dit)] " |
| 142 |
f"[main {short_hash}] WIP: This ïs a title. Contënt on the second line\n", |
| 143 |
" 1 file changed, 0 insertions(+), 0 deletions(-)\n", |
| 144 |
f" create mode 100644 {test_filename}\n", |
| 145 |
] |
| 146 |
|
| 147 |
for output, expected in zip(self.githook_output, expected_output): |
| 148 |
self.assertMultiLineEqual(output.replace("\r", ""), expected.replace("\r", "")) |
| 149 |
|
| 150 |
def test_commit_hook_worktree(self): |
| 151 |
"""Tests that hook installation and un-installation also work in git worktrees. |
| 152 |
Test steps: |
| 153 |
```sh |
| 154 |
git init <tmpdir> |
| 155 |
cd <tmpdir> |
| 156 |
git worktree add <worktree-tempdir> |
| 157 |
cd <worktree-tempdir> |
| 158 |
gitlint install-hook |
| 159 |
gitlint uninstall-hook |
| 160 |
``` |
| 161 |
""" |
| 162 |
tmp_git_repo = self.create_tmp_git_repo() |
| 163 |
self.create_simple_commit("Simple title\n\nContënt in the body", git_repo=tmp_git_repo) |
| 164 |
|
| 165 |
worktree_dir = self.generate_temp_path() |
| 166 |
self.tmp_git_repos.append(worktree_dir) |
| 167 |
|
| 168 |
git("worktree", "add", worktree_dir, _cwd=tmp_git_repo, _tty_in=True) |
| 169 |
|
| 170 |
output_installed = gitlint("install-hook", _cwd=worktree_dir) |
| 171 |
expected_hook_path = os.path.join(tmp_git_repo, ".git", "hooks", "commit-msg") |
| 172 |
expected_msg = f"Successfully installed gitlint commit-msg hook in {expected_hook_path}\n" |
| 173 |
self.assertEqualStdout(output_installed, expected_msg) |
| 174 |
|
| 175 |
output_uninstalled = gitlint("uninstall-hook", _cwd=worktree_dir) |
| 176 |
expected_hook_path = os.path.join(tmp_git_repo, ".git", "hooks", "commit-msg") |
| 177 |
expected_msg = f"Successfully uninstalled gitlint commit-msg hook from {expected_hook_path}\n" |
| 178 |
self.assertEqualStdout(output_uninstalled, expected_msg) |