forked from coala/coala-bears
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitCommitBearTest.py
435 lines (373 loc) · 18.3 KB
/
GitCommitBearTest.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import os
import platform
import shutil
import stat
import unittest
import unittest.mock
from queue import Queue
from tempfile import mkdtemp
from coalib.testing.BearTestHelper import generate_skip_decorator
from bears.vcs.git.GitCommitBear import GitCommitBear
from coala_utils.string_processing.Core import escape
from coalib.misc.Shell import run_shell_command
from coalib.settings.ConfigurationGathering import get_config_directory
from coalib.settings.Section import Section
from coalib.settings.Setting import Setting
@generate_skip_decorator(GitCommitBear)
class GitCommitBearTest(unittest.TestCase):
@staticmethod
def run_git_command(*args, stdin=None):
run_shell_command(' '.join(('git',) + args), stdin)
@staticmethod
def git_commit(msg):
# Use stdin mode from git, since -m on Windows cmd does not support
# multiline messages.
GitCommitBearTest.run_git_command('commit',
'--allow-empty',
'--allow-empty-message',
'--file=-',
stdin=msg)
def run_uut(self, *args, **kwargs):
"""
Runs the unit-under-test (via `self.uut.run()`) and collects the
messages of the yielded results as a list.
:param args: Positional arguments to forward to the run function.
:param kwargs: Keyword arguments to forward to the run function.
:return: A list of the message strings.
"""
return list(result.message for result in self.uut.run(*args, **kwargs))
def assert_no_msgs(self):
"""
Assert that there are no messages in the message queue of the bear, and
show the messages in the failure messgae if it is not empty.
"""
self.assertTrue(
self.msg_queue.empty(),
'Expected no messages in bear message queue, but got: ' +
str(list(str(i) for i in self.msg_queue.queue)))
def setUp(self):
self.msg_queue = Queue()
self.section = Section('')
self.uut = GitCommitBear(None, self.section, self.msg_queue)
self._old_cwd = os.getcwd()
self.gitdir = mkdtemp()
os.chdir(self.gitdir)
self.run_git_command('init')
self.run_git_command('config', 'user.email coala@coala.io')
self.run_git_command('config', 'user.name coala')
@staticmethod
def _windows_rmtree_remove_readonly(func, path, excinfo):
os.chmod(path, stat.S_IWRITE)
func(path)
def tearDown(self):
os.chdir(self._old_cwd)
if platform.system() == 'Windows':
onerror = self._windows_rmtree_remove_readonly
else:
onerror = None
shutil.rmtree(self.gitdir, onerror=onerror)
def test_check_prerequisites(self):
_shutil_which = shutil.which
try:
shutil.which = lambda *args, **kwargs: None
self.assertEqual(GitCommitBear.check_prerequisites(),
'git is not installed.')
shutil.which = lambda *args, **kwargs: 'path/to/git'
self.assertTrue(GitCommitBear.check_prerequisites())
finally:
shutil.which = _shutil_which
def test_get_metadata(self):
metadata = GitCommitBear.get_metadata()
self.assertEqual(
metadata.name,
"<Merged signature of 'run', 'check_shortlog', 'check_body'"
", 'check_issue_reference'>")
# Test if at least one parameter of each signature is used.
self.assertIn('allow_empty_commit_message', metadata.optional_params)
self.assertIn('shortlog_length', metadata.optional_params)
self.assertIn('body_line_length', metadata.optional_params)
self.assertIn('body_close_issue', metadata.optional_params)
def test_git_failure(self):
# In this case use a reference to a non-existing commit, so just try
# to log all commits on a newly created repository.
self.assertEqual(self.run_uut(), [])
git_error = self.msg_queue.get().message
self.assertEqual(git_error[:4], 'git:')
self.assert_no_msgs()
def test_empty_message(self):
self.git_commit('')
self.assertEqual(self.run_uut(),
['HEAD commit has no message.'])
self.assert_no_msgs()
self.assertEqual(self.run_uut(allow_empty_commit_message=True),
[])
self.assert_no_msgs()
@unittest.mock.patch('bears.vcs.git.GitCommitBear.run_shell_command',
return_value=('one-liner-message\n', ''))
def test_pure_oneliner_message(self, patch):
self.assertEqual(self.run_uut(), [])
self.assert_no_msgs()
def test_shortlog_checks_length(self):
self.git_commit('Commit messages that nearly exceed default limit..')
self.assertEqual(self.run_uut(), [])
self.assert_no_msgs()
self.assertEqual(self.run_uut(shortlog_length=17),
['Shortlog of the HEAD commit contains 50 '
'character(s). This is 33 character(s) longer than '
'the limit (50 > 17).'])
self.assert_no_msgs()
self.git_commit('Add a very long shortlog for a bad project history.')
self.assertEqual(self.run_uut(),
['Shortlog of the HEAD commit contains 51 '
'character(s). This is 1 character(s) longer than '
'the limit (51 > 50).'])
self.assert_no_msgs()
def test_shortlog_checks_shortlog_trailing_period(self):
self.git_commit('Shortlog with dot.')
self.assertEqual(self.run_uut(shortlog_trailing_period=True), [])
self.assertEqual(self.run_uut(shortlog_trailing_period=False),
['Shortlog of HEAD commit contains a period at end.'])
self.assertEqual(self.run_uut(shortlog_trailing_period=None), [])
self.git_commit('Shortlog without dot')
self.assertEqual(
self.run_uut(shortlog_trailing_period=True),
['Shortlog of HEAD commit contains no period at end.'])
self.assertEqual(self.run_uut(shortlog_trailing_period=False), [])
self.assertEqual(self.run_uut(shortlog_trailing_period=None), [])
def test_shortlog_wip_check(self):
self.git_commit('[wip] Shortlog')
self.assertEqual(self.run_uut(shortlog_wip_check=False), [])
self.assertEqual(self.run_uut(shortlog_wip_check=True),
['This commit seems to be marked as work in progress '
'and should not be used in production. Treat '
'carefully.'])
self.assertEqual(self.run_uut(shortlog_wip_check=None), [])
self.git_commit('Shortlog as usual')
self.assertEqual(self.run_uut(shortlog_wip_check=True), [])
def test_shortlog_checks_imperative(self):
self.git_commit('tag: Add shortlog in imperative')
self.assertNotIn("Shortlog of HEAD commit isn't in imperative "
"mood! Bad words are 'added'",
self.run_uut())
self.git_commit('Added invalid shortlog')
self.assertIn("Shortlog of HEAD commit isn't in imperative "
"mood! Bad words are 'Added'",
self.run_uut())
self.git_commit('Adding another invalid shortlog')
self.assertIn("Shortlog of HEAD commit isn't in imperative "
"mood! Bad words are 'Adding'",
self.run_uut())
self.git_commit('Added another invalid shortlog')
self.assertNotIn("Shortlog of HEAD commit isn't in imperative "
"mood! Bad words are 'Added'",
self.run_uut(shortlog_imperative_check=False))
def test_shortlog_checks_regex(self):
pattern = '.*?: .*[^.]'
self.git_commit('tag: message')
self.assertEqual(self.run_uut(shortlog_regex=pattern), [])
self.git_commit('tag: message invalid.')
self.assertEqual(
self.run_uut(shortlog_regex=pattern),
['Shortlog of HEAD commit does not match given regex: {regex}'
.format(regex=pattern)])
self.git_commit('SuCkS cOmPleTely')
self.assertEqual(
self.run_uut(shortlog_regex=pattern),
['Shortlog of HEAD commit does not match given regex: {regex}'
.format(regex=pattern)])
# Check for full-matching.
pattern = 'abcdefg'
self.git_commit('abcdefg')
self.assertEqual(self.run_uut(shortlog_regex=pattern), [])
self.git_commit('abcdefgNO MATCH')
self.assertEqual(
self.run_uut(shortlog_regex=pattern),
['Shortlog of HEAD commit does not match given regex: {regex}'
.format(regex=pattern)])
def test_body_checks(self):
self.git_commit(
'Commits message with a body\n\n'
'nearly exceeding the default length of a body, but not quite. '
'haaaaaands')
self.assertEqual(self.run_uut(), [])
self.assert_no_msgs()
self.git_commit('Shortlog only')
self.assertEqual(self.run_uut(), [])
self.assert_no_msgs()
# Force a body.
self.git_commit('Shortlog only ...')
self.assertEqual(self.run_uut(force_body=True),
['No commit message body at HEAD.'])
self.assert_no_msgs()
# Miss a newline between shortlog and body.
self.git_commit('Shortlog\nOops, body too early')
self.assertEqual(self.run_uut(),
['No newline found between shortlog and body at '
'HEAD commit. Please add one.'])
self.assert_no_msgs()
# And now too long lines.
self.git_commit('Shortlog\n\n'
'This line is ok.\n'
'This line is by far too long (in this case).\n'
'This one too, blablablablablablablablabla.')
self.assertEqual(self.run_uut(body_line_length=41),
['Body of HEAD commit contains too long lines. '
'Commit body lines should not exceed 41 '
'characters.'])
self.assert_no_msgs()
# Allow long lines with ignore regex
self.git_commit('Shortlog\n\n'
'This line is ok.\n'
'This line is by far too long (in this case).')
self.assertEqual(self.run_uut(body_line_length=41,
ignore_length_regex=('^.*too long',)),
[])
self.assertTrue(self.msg_queue.empty())
def test_check_issue_reference(self):
# Commit with no remotes configured
self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
'Another line, blablablablablabla.\n'
'Closes #01112')
self.assertEqual(self.run_uut(body_close_issue=True), [])
# Adding BitBucket remote for testing
self.run_git_command('remote', 'add', 'test',
'https://bitbucket.com/user/repo.git')
# Unsupported Host - Bitbucket
self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
'Another line, blablablablablabla.\n'
'Closes #1112')
self.assertEqual(self.run_uut(
body_close_issue=True,
body_close_issue_full_url=True), [])
# Adding GitHub remote for testing, ssh way :P
self.run_git_command('remote', 'set-url', 'test',
'git@github.com:user/repo.git')
# GitHub host with an issue
self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
'Another line, blablablablablabla.\n'
'Fixed https://github.com/usr/repo/issues/1112\n'
'and https://github.com/usr/repo/issues/1312')
self.assertEqual(self.run_uut(
body_close_issue=True,
body_close_issue_full_url=True), [])
# No keywords and no issues
self.git_commit('Shortlog\n\n'
'This line is ok.\n'
'This line is by far too long (in this case).\n'
'This one too, blablablablablablablablabla.')
self.assertEqual(self.run_uut(body_close_issue=True,),
['Body of HEAD commit does not contain any issue '
'reference.'])
self.assert_no_msgs()
# Has keyword but no valid issue URL
self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
'Another line, blablablablablabla.\n'
'Fix https://github.com/user/repo.git')
self.assertEqual(self.run_uut(
body_close_issue=True,
body_close_issue_full_url=True),
['Body of HEAD commit does not contain any full issue'
' reference.'])
self.assert_no_msgs()
# GitHub host with short issue tag
self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
'Another line, blablablablablabla.\n'
'Fix #1112, #1115 and #123')
self.assertEqual(self.run_uut(body_close_issue=True,), [])
# GitHub host with invalid short issue tag
self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
'Another line, blablablablablabla.\n'
'Fix #01112 and #111')
self.assertEqual(self.run_uut(body_close_issue=True,),
['Invalid issue number at #01112'])
self.assert_no_msgs()
# GitHub host with no full issue reference
self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
'Another line, blablablablablabla.\n'
'Fix #01112')
self.assertEqual(self.run_uut(
body_close_issue=True,
body_close_issue_full_url=True),
['Body of HEAD commit does not contain any full issue'
' reference.'])
self.assert_no_msgs()
# Adding GitLab remote for testing
self.run_git_command('remote', 'set-url', 'test',
'https://gitlab.com/user/repo.git')
# GitLab chosen and has an issue
self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
'Another line, blablablablablabla.\n'
'Resolve https://gitlab.com/usr/repo/issues/1112\n'
'and https://gitlab.com/usr/repo/issues/1312')
self.assertEqual(self.run_uut(
body_close_issue=True,
body_close_issue_full_url=True), [])
# Invalid issue number in URL
self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
'Another line, blablablablablabla.\n'
'Closing https://gitlab.com/user/repo/issues/123\n'
'and https://gitlab.com/user/repo/issues/not_num')
self.assertEqual(self.run_uut(
body_close_issue=True,
body_close_issue_full_url=True),
['Invalid issue number at '
'https://gitlab.com/user/repo/issues/not_num'])
self.assert_no_msgs()
# Invalid URL
self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
'Another line, blablablablablabla.\n'
'Fix http://google.com/issues/hehehe')
self.assertEqual(self.run_uut(
body_close_issue=True,
body_close_issue_full_url=True),
['Body of HEAD commit does not contain any full issue'
' reference.'])
self.assert_no_msgs()
# One of the short references is broken
self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
'Another line, blablablablablabla.\n'
'Resolve #11 and close #notnum')
self.assertEqual(self.run_uut(body_close_issue=True,),
['Invalid issue number at #notnum'])
self.assert_no_msgs()
# Last line enforce full URL
self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
'Fix http://gitlab.com/user/repo/issues/1112\n'
'Another line, blablablablablabla.\n')
self.assertEqual(self.run_uut(
body_close_issue=True,
body_close_issue_full_url=True,
body_close_issue_on_last_line=True),
['Body of HEAD commit does not contain any full issue'
' reference in the last line.'])
self.assert_no_msgs()
def test_different_path(self):
no_git_dir = mkdtemp()
self.git_commit('Add a very long shortlog for a bad project history.')
os.chdir(no_git_dir)
# When section doesn't have a project_dir
self.assertEqual(self.run_uut(), [])
git_error = self.msg_queue.get().message
self.assertEqual(git_error[:4], 'git:')
# when section does have a project_dir
self.section.append(Setting('project_dir', escape(self.gitdir, '\\')))
self.assertEqual(self.run_uut(),
['Shortlog of the HEAD commit contains 51 '
'character(s). This is 1 character(s) longer than '
'the limit (51 > 50).'])
self.assertEqual(get_config_directory(self.section),
self.gitdir)
os.chdir(self.gitdir)
os.rmdir(no_git_dir)