33
44from unittest .mock import MagicMock , call , mock_open , patch
55
6+ import pytest
7+
68from commitlint .cli import get_args , main
79from commitlint .exceptions import CommitlintException
810from commitlint .messages import (
@@ -137,15 +139,14 @@ def test__main__valid_commit_message_using_skip_detail(
137139 ),
138140 )
139141 @patch ("sys.stderr.write" )
140- @patch ("sys.exit" )
141142 def test__main__invalid_commit_message (
142143 self ,
143- mock_sys_exit ,
144144 mock_stderr_write ,
145145 * _ ,
146146 ):
147- main ()
148- mock_sys_exit .assert_called_with (1 )
147+ with pytest .raises (SystemExit ):
148+ main ()
149+
149150 mock_stderr_write .assert_has_calls (
150151 [
151152 call ("⧗ Input:\n Invalid commit message\n \n " ),
@@ -166,15 +167,14 @@ def test__main__invalid_commit_message(
166167 ),
167168 )
168169 @patch ("sys.stderr.write" )
169- @patch ("sys.exit" )
170170 def test__main__invalid_commit_message_using_skip_detail (
171171 self ,
172- mock_sys_exit ,
173172 mock_stderr_write ,
174173 * _ ,
175174 ):
176- main ()
177- mock_sys_exit .assert_called_with (1 )
175+ with pytest .raises (SystemExit ):
176+ main ()
177+
178178 mock_stderr_write .assert_has_calls (
179179 [
180180 call ("⧗ Input:\n Invalid commit message\n \n " ),
@@ -199,13 +199,11 @@ def test__main__valid_commit_message_with_file(self, mock_stdout_write, *_):
199199 return_value = MagicMock (file = "path/to/file.txt" , skip_detail = False , quiet = False ),
200200 )
201201 @patch ("sys.stderr.write" )
202- @patch ("sys.exit" )
203202 @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+
209207 mock_stderr_write .assert_has_calls (
210208 [
211209 call ("⧗ Input:\n Invalid commit message 2\n \n " ),
@@ -239,13 +237,14 @@ def test__main__valid_commit_message_with_hash(
239237 )
240238 @patch ("commitlint.cli.get_commit_message_of_hash" )
241239 @patch ("sys.stderr.write" )
242- @patch ("sys.exit" )
243240 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 , * _
245242 ):
246243 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+
249248 mock_stderr_write .assert_has_calls (
250249 [
251250 call ("⧗ Input:\n Invalid commit message\n \n " ),
@@ -292,16 +291,16 @@ def test__main__valid_commit_message_with_hash_range(
292291 )
293292 @patch ("sys.stderr.write" )
294293 @patch ("commitlint.cli.get_commit_messages_of_hash_range" )
295- @patch ("sys.exit" )
296294 def test__main__invalid_commit_message_with_hash_range (
297- self , mock_sys_exit , mock_get_commit_messages , * _
295+ self , mock_get_commit_messages , * _
298296 ):
299297 mock_get_commit_messages .return_value = [
300298 "Invalid commit message 1" ,
301299 "Invalid commit message 2" ,
302300 ]
303- main ()
304- mock_sys_exit .assert_called_with (1 )
301+
302+ with pytest .raises (SystemExit ):
303+ main ()
305304
306305 # main : exception handling
307306
@@ -315,13 +314,14 @@ def test__main__invalid_commit_message_with_hash_range(
315314 "commitlint.cli.lint_commit_message" ,
316315 )
317316 @patch ("sys.stderr.write" )
318- @patch ("sys.exit" )
319317 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 , * _
321319 ):
322320 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+
325325 mock_stderr_write .assert_called_with ("Test message\n " )
326326
327327 @patch (
@@ -337,11 +337,12 @@ def test__main__handle_exceptions(
337337 )
338338 @patch ("sys.stdout.write" )
339339 @patch ("sys.stderr.write" )
340- @patch ("sys.exit" )
341340 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 , * _
343342 ):
344- main ()
343+ with pytest .raises (SystemExit ):
344+ main ()
345+
345346 mock_stderr_write .assert_not_called ()
346347 mock_stdout_write .assert_not_called ()
347348
@@ -358,14 +359,12 @@ def test__main__quiet_option_with_invalid_commit_message(
358359 )
359360 @patch ("sys.stdout.write" )
360361 @patch ("sys.stderr.write" )
361- @patch ("sys.exit" )
362362 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 , * _
364364 ):
365365 main ()
366366 mock_stderr_write .assert_not_called ()
367367 mock_stdout_write .assert_not_called ()
368- mock_sys_exit .assert_not_called ()
369368
370369 @patch (
371370 "commitlint.cli.get_args" ,
@@ -402,22 +401,22 @@ def test__valid_commit_message_with_hash_range_in_quiet(
402401 ),
403402 )
404403 @patch ("commitlint.cli.get_commit_messages_of_hash_range" )
405- @patch ("sys.exit" )
406404 @patch ("sys.stdout.write" )
407405 @patch ("sys.stderr.write" )
408406 def test__invalid_commit_message_with_hash_range_in_quiet (
409407 self ,
410408 mock_stderr_write ,
411409 mock_stdout_write ,
412- mock_sys_exit ,
413410 mock_get_commit_messages ,
414411 * _ ,
415412 ):
416413 mock_get_commit_messages .return_value = [
417414 "Invalid commit message 1" ,
418415 "Invalid commit message 2" ,
419416 ]
420- main ()
417+
418+ with pytest .raises (SystemExit ):
419+ main ()
420+
421421 mock_stderr_write .assert_not_called ()
422- mock_sys_exit .assert_called_once_with (1 )
423422 mock_stdout_write .assert_not_called ()
0 commit comments