1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# -*- coding: utf-8 -*-
import os
try:
# python 2.x
from mock import patch, ANY, mock_open
except ImportError:
# python 3.x
from unittest.mock import patch, ANY, mock_open # pylint: disable=no-name-in-module, import-error
from gitlint.tests.base import BaseTestCase
from gitlint.config import LintConfig
from gitlint.hooks import GitHookInstaller, GitHookInstallerError, COMMIT_MSG_HOOK_SRC_PATH, COMMIT_MSG_HOOK_DST_PATH, \
GITLINT_HOOK_IDENTIFIER
class HookTests(BaseTestCase):
@patch('gitlint.hooks.git_hooks_dir')
def test_commit_msg_hook_path(self, git_hooks_dir):
git_hooks_dir.return_value = os.path.join(u"/föo", u"bar")
lint_config = LintConfig()
lint_config.target = self.SAMPLES_DIR
expected_path = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH)
path = GitHookInstaller.commit_msg_hook_path(lint_config)
git_hooks_dir.assert_called_once_with(self.SAMPLES_DIR)
self.assertEqual(path, expected_path)
@staticmethod
@patch('os.chmod')
@patch('os.stat')
@patch('gitlint.hooks.shutil.copy')
@patch('os.path.exists', return_value=False)
@patch('os.path.isdir', return_value=True)
@patch('gitlint.hooks.git_hooks_dir')
def test_install_commit_msg_hook(git_hooks_dir, isdir, path_exists, copy, stat, chmod):
lint_config = LintConfig()
lint_config.target = os.path.join(u"/hür", u"dur")
git_hooks_dir.return_value = os.path.join(u"/föo", u"bar", ".git", "hooks")
expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH)
GitHookInstaller.install_commit_msg_hook(lint_config)
isdir.assert_called_with(git_hooks_dir.return_value)
path_exists.assert_called_once_with(expected_dst)
copy.assert_called_once_with(COMMIT_MSG_HOOK_SRC_PATH, expected_dst)
stat.assert_called_once_with(expected_dst)
chmod.assert_called_once_with(expected_dst, ANY)
git_hooks_dir.assert_called_with(lint_config.target)
@patch('gitlint.hooks.shutil.copy')
@patch('os.path.exists', return_value=False)
@patch('os.path.isdir', return_value=True)
@patch('gitlint.hooks.git_hooks_dir')
def test_install_commit_msg_hook_negative(self, git_hooks_dir, isdir, path_exists, copy):
lint_config = LintConfig()
lint_config.target = os.path.join(u"/hür", u"dur")
git_hooks_dir.return_value = os.path.join(u"/föo", u"bar", ".git", "hooks")
# mock that current dir is not a git repo
isdir.return_value = False
expected_msg = u"{0} is not a git repository".format(lint_config.target)
with self.assertRaisesRegex(GitHookInstallerError, expected_msg):
GitHookInstaller.install_commit_msg_hook(lint_config)
isdir.assert_called_with(git_hooks_dir.return_value)
path_exists.assert_not_called()
copy.assert_not_called()
# mock that there is already a commit hook present
isdir.return_value = True
path_exists.return_value = True
expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH)
expected_msg = u"There is already a commit-msg hook file present in {0}.\n".format(expected_dst) + \
"gitlint currently does not support appending to an existing commit-msg file."
with self.assertRaisesRegex(GitHookInstallerError, expected_msg):
GitHookInstaller.install_commit_msg_hook(lint_config)
@staticmethod
@patch('os.remove')
@patch('os.path.exists', return_value=True)
@patch('os.path.isdir', return_value=True)
@patch('gitlint.hooks.git_hooks_dir')
def test_uninstall_commit_msg_hook(git_hooks_dir, isdir, path_exists, remove):
lint_config = LintConfig()
git_hooks_dir.return_value = os.path.join(u"/föo", u"bar", ".git", "hooks")
lint_config.target = os.path.join(u"/hür", u"dur")
read_data = "#!/bin/sh\n" + GITLINT_HOOK_IDENTIFIER
with patch('gitlint.hooks.io.open', mock_open(read_data=read_data), create=True):
GitHookInstaller.uninstall_commit_msg_hook(lint_config)
expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH)
isdir.assert_called_with(git_hooks_dir.return_value)
path_exists.assert_called_once_with(expected_dst)
remove.assert_called_with(expected_dst)
git_hooks_dir.assert_called_with(lint_config.target)
@patch('os.remove')
@patch('os.path.exists', return_value=True)
@patch('os.path.isdir', return_value=True)
@patch('gitlint.hooks.git_hooks_dir')
def test_uninstall_commit_msg_hook_negative(self, git_hooks_dir, isdir, path_exists, remove):
lint_config = LintConfig()
lint_config.target = os.path.join(u"/hür", u"dur")
git_hooks_dir.return_value = os.path.join(u"/föo", u"bar", ".git", "hooks")
# mock that the current directory is not a git repo
isdir.return_value = False
expected_msg = u"{0} is not a git repository".format(lint_config.target)
with self.assertRaisesRegex(GitHookInstallerError, expected_msg):
GitHookInstaller.uninstall_commit_msg_hook(lint_config)
isdir.assert_called_with(git_hooks_dir.return_value)
path_exists.assert_not_called()
remove.assert_not_called()
# mock that there is no commit hook present
isdir.return_value = True
path_exists.return_value = False
expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH)
expected_msg = u"There is no commit-msg hook present in {0}.".format(expected_dst)
with self.assertRaisesRegex(GitHookInstallerError, expected_msg):
GitHookInstaller.uninstall_commit_msg_hook(lint_config)
isdir.assert_called_with(git_hooks_dir.return_value)
path_exists.assert_called_once_with(expected_dst)
remove.assert_not_called()
# mock that there is a different (=not gitlint) commit hook
isdir.return_value = True
path_exists.return_value = True
read_data = "#!/bin/sh\nfoo"
expected_dst = os.path.join(git_hooks_dir.return_value, COMMIT_MSG_HOOK_DST_PATH)
expected_msg = u"The commit-msg hook in {0} was not installed by gitlint ".format(expected_dst) + \
"(or it was modified).\nUninstallation of 3th party or modified gitlint hooks " + \
"is not supported."
with patch('gitlint.hooks.io.open', mock_open(read_data=read_data), create=True):
with self.assertRaisesRegex(GitHookInstallerError, expected_msg):
GitHookInstaller.uninstall_commit_msg_hook(lint_config)
remove.assert_not_called()
|