forked from opensource-nepal/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cli.py
504 lines (437 loc) · 14.9 KB
/
test_cli.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# type: ignore
# pylint: disable=all
from unittest.mock import Mock, call, mock_open, patch
import pytest
from commitlint.cli import get_args, main
from commitlint.config import config
from commitlint.exceptions import CommitlintException
from commitlint.messages import (
INCORRECT_FORMAT_ERROR,
VALIDATION_FAILED,
VALIDATION_SUCCESSFUL,
)
class ArgsMock(Mock):
"""
Args Mock, used for mocking CLI arguments.
Main purpose: returns `None` instead of `Mock` if attribute is not assigned.
```
arg = ArgsMock(value1=10)
arg.value1 # 10
arg.value2 # None
```
"""
def __getattr__(self, name):
if name in self.__dict__:
return self.__dict__[name]
return None
class TestCLIGetArgs:
# get_args
@patch("sys.argv", ["prog", "commit message"])
def test__get_args__with_commit_message(self, *_):
args = get_args()
assert args.commit_message == "commit message"
@patch("sys.argv", ["prog"])
def test__get_args__without_commit_message(self, *_):
with pytest.raises(SystemExit) as ex:
get_args()
assert ex.value.code == 2
@patch("sys.argv", ["prog", "--file", "path/to/file.txt"])
def test__get_args__with_file(self, *_):
args = get_args()
assert args.file == "path/to/file.txt"
@patch("sys.argv", ["prog", "--hash", "commit_hash"])
def test__get_args__with_hash(self, *_):
args = get_args()
assert args.hash == "commit_hash"
@patch("sys.argv", ["prog", "--from-hash", "from_commit_hash"])
def test__get_args__with_from_hash(self, *_):
args = get_args()
assert args.from_hash == "from_commit_hash"
assert args.to_hash == "HEAD"
@patch("sys.argv", ["prog", "--to-hash", "to_commit_hash"])
def test__get_args__with_to_hash_without_from_hash(self, *_):
with pytest.raises(SystemExit) as ex:
get_args()
assert ex.value.code == 2
@patch(
"sys.argv",
["prog", "--from-hash", "from_commit_hash", "--to-hash", "to_commit_hash"],
)
def test__get_args__with_to_hash(self, *_):
args = get_args()
assert args.from_hash == "from_commit_hash"
assert args.to_hash == "to_commit_hash"
@patch("sys.argv", ["prog", "--skip-detail", "commit_msg"])
def test__get_args__with_skip_detail(self, *_):
args = get_args()
assert args.skip_detail is True
@patch("sys.argv", ["prog", "--hide-input", "commit_msg"])
def test__get_args__with_hide_input(self, *_):
args = get_args()
assert args.hide_input is True
@patch("sys.argv", ["prog", "--verbose", "commit_msg"])
def test__get_args__with_verbose(self, *_):
args = get_args()
assert args.verbose is True
@patch("sys.argv", ["prog", "--quiet", "commit_msg"])
def test__get_args__with_quiet(self, *_):
args = get_args()
assert args.quiet is True
@patch("sys.argv", ["prog", "--quiet", "--verbose", "commit_msg"])
def test__get_args___fails_with_quiet_and_verbose(self, *_):
with pytest.raises(SystemExit) as ex:
get_args()
assert ex.value.code == 2
@patch("commitlint.console.success")
@patch("commitlint.console.error")
class TestCLIMain:
# main: commit_message
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(commit_message="feat: valid commit message"),
)
def test__main__valid_commit_message(
self, _mock_get_args, _mock_output_error, mock_output_success
):
main()
mock_output_success.assert_called_with(f"{VALIDATION_SUCCESSFUL}")
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(
commit_message="feat: valid commit message", skip_detail=True
),
)
def test__main__valid_commit_message_using_skip_detail(
self, _mock_get_args, _mock_output_error, mock_output_success
):
main()
mock_output_success.assert_called_once_with(f"{VALIDATION_SUCCESSFUL}")
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(commit_message="Invalid commit message"),
)
def test__main__invalid_commit_message(
self, _mock_get_args, mock_output_error, _mock_output_success
):
with pytest.raises(SystemExit):
main()
mock_output_error.assert_has_calls(
[
call("⧗ Input:\nInvalid commit message\n"),
call("✖ Found 1 error(s)."),
call(f"- {INCORRECT_FORMAT_ERROR}"),
]
)
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(
commit_message="Invalid commit message", skip_detail=True
),
)
def test__main__invalid_commit_message_using_skip_detail(
self, _mock_get_args, mock_output_error, _mock_output_success
):
with pytest.raises(SystemExit):
main()
mock_output_error.assert_has_calls(
[
call("⧗ Input:\nInvalid commit message\n"),
call(f"{VALIDATION_FAILED}"),
]
)
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(commit_message="Invalid commit message", hide_input=True),
)
def test__main__invalid_commit_message_with_hide_input_True(
self, _mock_get_args, mock_output_error, _mock_output_success
):
with pytest.raises(SystemExit):
main()
mock_output_error.assert_has_calls(
[
call("✖ Found 1 error(s)."),
call(f"- {INCORRECT_FORMAT_ERROR}"),
]
)
# main: file
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(file="path/to/file.txt"),
)
@patch("builtins.open", mock_open(read_data="feat: valid commit message"))
def test__main__valid_commit_message_with_file(
self, _mock_get_args, _mock_output_error, mock_output_success
):
main()
mock_output_success.assert_called_with(f"{VALIDATION_SUCCESSFUL}")
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(file="path/to/file.txt"),
)
@patch(
"builtins.open",
mock_open(read_data="feat: valid commit message 2\n#this is a comment"),
)
def test__main__valid_commit_message_and_comments_with_file(
self, _mock_get_args, _mock_output_error, mock_output_success
):
main()
mock_output_success.assert_called_with(f"{VALIDATION_SUCCESSFUL}")
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(file="path/to/file.txt"),
)
@patch("builtins.open", mock_open(read_data="Invalid commit message 2"))
def test__main__invalid_commit_message_with_file(
self, _mock_get_args, mock_output_error, _mock_output_success
):
with pytest.raises(SystemExit):
main()
mock_output_error.assert_has_calls(
[
call("⧗ Input:\nInvalid commit message 2\n"),
call("✖ Found 1 error(s)."),
call(f"- {INCORRECT_FORMAT_ERROR}"),
]
)
# main: hash
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(hash="commit_hash"),
)
@patch("commitlint.cli.get_commit_message_of_hash")
def test__main__valid_commit_message_with_hash(
self,
mock_get_commit_message_of_hash,
_mock_get_args,
_mock_output_error,
mock_output_success,
):
mock_get_commit_message_of_hash.return_value = "feat: valid commit message"
main()
mock_output_success.assert_called_with(f"{VALIDATION_SUCCESSFUL}")
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(hash="commit_hash"),
)
@patch("commitlint.cli.get_commit_message_of_hash")
def test__main__invalid_commit_message_with_hash(
self,
mock_get_commit_message_of_hash,
_mock_get_args,
mock_output_error,
_mock_output_success,
):
mock_get_commit_message_of_hash.return_value = "Invalid commit message"
with pytest.raises(SystemExit):
main()
mock_output_error.assert_has_calls(
[
call("⧗ Input:\nInvalid commit message\n"),
call("✖ Found 1 error(s)."),
call(f"- {INCORRECT_FORMAT_ERROR}"),
]
)
# main: from_hash and to_hash
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(from_hash="start_commit_hash", to_hash="end_commit_hash"),
)
@patch("commitlint.cli.get_commit_messages_of_hash_range")
def test__main__valid_commit_message_with_hash_range(
self,
mock_get_commit_messages,
_mock_get_args,
_mock_output_error,
mock_output_success,
):
mock_get_commit_messages.return_value = [
"feat: commit message 1",
"fix: commit message 2",
]
main()
mock_output_success.assert_called_with(f"{VALIDATION_SUCCESSFUL}")
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(
from_hash="invalid_start_hash", to_hash="end_commit_hash"
),
)
@patch("commitlint.cli.get_commit_messages_of_hash_range")
def test__main__invalid_commit_message_with_hash_range(
self,
mock_get_commit_messages,
_mock_get_args,
_mock_output_error,
_mock_output_success,
):
mock_get_commit_messages.return_value = [
"Invalid commit message 1",
"Invalid commit message 2",
]
with pytest.raises(SystemExit):
main()
# main : exception handling
@patch(
"argparse.ArgumentParser.parse_args",
return_value=ArgsMock(commit_message="feat: commit message"),
)
@patch(
"commitlint.cli.lint_commit_message",
)
def test__main__handle_exceptions(
self,
mock_lint_commit_message,
_mock_get_args,
mock_output_error,
_mock_output_success,
):
mock_lint_commit_message.side_effect = CommitlintException("Test message")
with pytest.raises(SystemExit):
main()
mock_output_error.assert_called_with("Test message")
# main : quiet
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(commit_message="feat: test commit", quiet=True),
)
def test__main__sets_config_for_quiet(
self,
_mock_get_args,
_mock_output_error,
_mock_output_success,
):
main()
assert config.quiet is True
# main : verbose
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(commit_message="feat: test commit", verbose=True),
)
def test__main__sets_config_for_verbose(
self,
_mock_get_args,
_mock_output_error,
_mock_output_success,
):
main()
assert config.verbose is True
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(file="path/to/non_existent_file.txt"),
)
def test__main__with_missing_file(
self, _mock_get_args, _mock_output_error, mock_output_success
):
mock_open().side_effect = FileNotFoundError(
2, "No such file or directory", "path/to/non_existent_file.txt"
)
with pytest.raises(SystemExit):
main()
class TestCLIMainQuiet:
# main : quiet (directly checking stdout and stderr)
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(commit_message="Invalid commit message", quiet=True),
)
@patch("sys.stdout.write")
@patch("sys.stderr.write")
def test__main__quiet_option_with_invalid_commit_message(
self, mock_stderr_write, mock_stdout_write, *_
):
with pytest.raises(SystemExit):
main()
mock_stderr_write.assert_not_called()
mock_stdout_write.assert_not_called()
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(commit_message="feat: valid commit message", quiet=True),
)
@patch("sys.stdout.write")
@patch("sys.stderr.write")
def test__main__quiet_option_with_valid_commit_message(
self, mock_stderr_write, mock_stdout_write, *_
):
main()
mock_stderr_write.assert_not_called()
mock_stdout_write.assert_not_called()
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(
from_hash="start_commit_hash", to_hash="end_commit_hash", quiet=True
),
)
@patch("commitlint.cli.get_commit_messages_of_hash_range")
@patch("sys.stdout.write")
def test__valid_commit_message_with_hash_range_in_quiet(
self, mock_stdout_write, mock_get_commit_messages, *_
):
mock_get_commit_messages.return_value = [
"feat: commit message 1",
"fix: commit message 2",
]
main()
mock_stdout_write.assert_not_called()
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(
from_hash="start_commit_hash", to_hash="end_commit_hash", quiet=True
),
)
@patch("commitlint.cli.get_commit_messages_of_hash_range")
@patch("sys.stdout.write")
@patch("sys.stderr.write")
def test__invalid_commit_message_with_hash_range_in_quiet(
self,
mock_stderr_write,
mock_stdout_write,
mock_get_commit_messages,
*_,
):
mock_get_commit_messages.return_value = [
"Invalid commit message 1",
"Invalid commit message 2",
]
with pytest.raises(SystemExit):
main()
mock_stderr_write.assert_not_called()
mock_stdout_write.assert_not_called()
class TestCliHeaderLength:
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(
commit_message="feat: add method 'formatEnglishDate' \
and 'formatEnglishDateInNepali' (#165)",
quiet=True,
max_header_length=20,
),
)
@patch("sys.stdout.write")
@patch("sys.stderr.write")
def test__main__quiet_option_with_header_max_length(
self, mock_stderr_write, mock_stdout_write, *_
):
with pytest.raises(SystemExit):
main()
mock_stderr_write.assert_not_called()
mock_stdout_write.assert_not_called()
@patch(
"commitlint.cli.get_args",
return_value=ArgsMock(
commit_message="feat: add method 'formatEnglishDate' \
and 'formatEnglishDateInNepali' (#165)",
quiet=True,
max_header_length=20,
disable_header_length_check=True,
),
)
@patch("sys.stdout.write")
@patch("sys.stderr.write")
def test__with_both_header_max_length_and_disabled_max_header_length_check(
self, mock_stderr_write, mock_stdout_write, *_
):
with pytest.raises(CommitlintException):
main()
mock_stderr_write.assert_not_called()
mock_stdout_write.assert_not_called()