3
3
4
4
from unittest .mock import MagicMock , call , mock_open , patch
5
5
6
+ import pytest
7
+
6
8
from commitlint .cli import get_args , main
7
9
from commitlint .exceptions import CommitlintException
8
10
from commitlint .messages import (
@@ -137,15 +139,14 @@ def test__main__valid_commit_message_using_skip_detail(
137
139
),
138
140
)
139
141
@patch ("sys.stderr.write" )
140
- @patch ("sys.exit" )
141
142
def test__main__invalid_commit_message (
142
143
self ,
143
- mock_sys_exit ,
144
144
mock_stderr_write ,
145
145
* _ ,
146
146
):
147
- main ()
148
- mock_sys_exit .assert_called_with (1 )
147
+ with pytest .raises (SystemExit ):
148
+ main ()
149
+
149
150
mock_stderr_write .assert_has_calls (
150
151
[
151
152
call ("⧗ Input:\n Invalid commit message\n \n " ),
@@ -166,15 +167,14 @@ def test__main__invalid_commit_message(
166
167
),
167
168
)
168
169
@patch ("sys.stderr.write" )
169
- @patch ("sys.exit" )
170
170
def test__main__invalid_commit_message_using_skip_detail (
171
171
self ,
172
- mock_sys_exit ,
173
172
mock_stderr_write ,
174
173
* _ ,
175
174
):
176
- main ()
177
- mock_sys_exit .assert_called_with (1 )
175
+ with pytest .raises (SystemExit ):
176
+ main ()
177
+
178
178
mock_stderr_write .assert_has_calls (
179
179
[
180
180
call ("⧗ Input:\n Invalid commit message\n \n " ),
@@ -199,13 +199,11 @@ def test__main__valid_commit_message_with_file(self, mock_stdout_write, *_):
199
199
return_value = MagicMock (file = "path/to/file.txt" , skip_detail = False , quiet = False ),
200
200
)
201
201
@patch ("sys.stderr.write" )
202
- @patch ("sys.exit" )
203
202
@patch ("builtins.open" , mock_open (read_data = "Invalid commit message 2" ))
204
- def test__main__invalid_commit_message_with_file (
205
- self , mock_sys_exit , mock_stderr_write , * _
206
- ):
207
- main ()
208
- mock_sys_exit .assert_called_with (1 )
203
+ def test__main__invalid_commit_message_with_file (self , mock_stderr_write , * _ ):
204
+ with pytest .raises (SystemExit ):
205
+ main ()
206
+
209
207
mock_stderr_write .assert_has_calls (
210
208
[
211
209
call ("⧗ Input:\n Invalid commit message 2\n \n " ),
@@ -239,13 +237,14 @@ def test__main__valid_commit_message_with_hash(
239
237
)
240
238
@patch ("commitlint.cli.get_commit_message_of_hash" )
241
239
@patch ("sys.stderr.write" )
242
- @patch ("sys.exit" )
243
240
def test__main__invalid_commit_message_with_hash (
244
- self , mock_sys_exit , mock_stderr_write , mock_get_commit_message_of_hash , * _
241
+ self , mock_stderr_write , mock_get_commit_message_of_hash , * _
245
242
):
246
243
mock_get_commit_message_of_hash .return_value = "Invalid commit message"
247
- main ()
248
- mock_sys_exit .assert_called_with (1 )
244
+
245
+ with pytest .raises (SystemExit ):
246
+ main ()
247
+
249
248
mock_stderr_write .assert_has_calls (
250
249
[
251
250
call ("⧗ Input:\n Invalid commit message\n \n " ),
@@ -292,16 +291,16 @@ def test__main__valid_commit_message_with_hash_range(
292
291
)
293
292
@patch ("sys.stderr.write" )
294
293
@patch ("commitlint.cli.get_commit_messages_of_hash_range" )
295
- @patch ("sys.exit" )
296
294
def test__main__invalid_commit_message_with_hash_range (
297
- self , mock_sys_exit , mock_get_commit_messages , * _
295
+ self , mock_get_commit_messages , * _
298
296
):
299
297
mock_get_commit_messages .return_value = [
300
298
"Invalid commit message 1" ,
301
299
"Invalid commit message 2" ,
302
300
]
303
- main ()
304
- mock_sys_exit .assert_called_with (1 )
301
+
302
+ with pytest .raises (SystemExit ):
303
+ main ()
305
304
306
305
# main : exception handling
307
306
@@ -315,13 +314,14 @@ def test__main__invalid_commit_message_with_hash_range(
315
314
"commitlint.cli.lint_commit_message" ,
316
315
)
317
316
@patch ("sys.stderr.write" )
318
- @patch ("sys.exit" )
319
317
def test__main__handle_exceptions (
320
- self , mock_sys_exit , mock_stderr_write , mock_lint_commit_message , * _
318
+ self , mock_stderr_write , mock_lint_commit_message , * _
321
319
):
322
320
mock_lint_commit_message .side_effect = CommitlintException ("Test message" )
323
- main ()
324
- mock_sys_exit .assert_called_with (1 )
321
+
322
+ with pytest .raises (SystemExit ):
323
+ main ()
324
+
325
325
mock_stderr_write .assert_called_with ("Test message\n " )
326
326
327
327
@patch (
@@ -337,11 +337,12 @@ def test__main__handle_exceptions(
337
337
)
338
338
@patch ("sys.stdout.write" )
339
339
@patch ("sys.stderr.write" )
340
- @patch ("sys.exit" )
341
340
def test__main__quiet_option_with_invalid_commit_message (
342
- self , mock_sys_exit , mock_stderr_write , mock_stdout_write , * _
341
+ self , mock_stderr_write , mock_stdout_write , * _
343
342
):
344
- main ()
343
+ with pytest .raises (SystemExit ):
344
+ main ()
345
+
345
346
mock_stderr_write .assert_not_called ()
346
347
mock_stdout_write .assert_not_called ()
347
348
@@ -358,14 +359,12 @@ def test__main__quiet_option_with_invalid_commit_message(
358
359
)
359
360
@patch ("sys.stdout.write" )
360
361
@patch ("sys.stderr.write" )
361
- @patch ("sys.exit" )
362
362
def test__main__quiet_option_with_valid_commit_message (
363
- self , mock_sys_exit , mock_stderr_write , mock_stdout_write , * _
363
+ self , mock_stderr_write , mock_stdout_write , * _
364
364
):
365
365
main ()
366
366
mock_stderr_write .assert_not_called ()
367
367
mock_stdout_write .assert_not_called ()
368
- mock_sys_exit .assert_not_called ()
369
368
370
369
@patch (
371
370
"commitlint.cli.get_args" ,
@@ -402,22 +401,22 @@ def test__valid_commit_message_with_hash_range_in_quiet(
402
401
),
403
402
)
404
403
@patch ("commitlint.cli.get_commit_messages_of_hash_range" )
405
- @patch ("sys.exit" )
406
404
@patch ("sys.stdout.write" )
407
405
@patch ("sys.stderr.write" )
408
406
def test__invalid_commit_message_with_hash_range_in_quiet (
409
407
self ,
410
408
mock_stderr_write ,
411
409
mock_stdout_write ,
412
- mock_sys_exit ,
413
410
mock_get_commit_messages ,
414
411
* _ ,
415
412
):
416
413
mock_get_commit_messages .return_value = [
417
414
"Invalid commit message 1" ,
418
415
"Invalid commit message 2" ,
419
416
]
420
- main ()
417
+
418
+ with pytest .raises (SystemExit ):
419
+ main ()
420
+
421
421
mock_stderr_write .assert_not_called ()
422
- mock_sys_exit .assert_called_once_with (1 )
423
422
mock_stdout_write .assert_not_called ()
0 commit comments