1+ import contextlib
12import os
3+ import shutil
4+ import tempfile
25from unittest import mock
36
47import pytest
@@ -15,6 +18,15 @@ def staging_is_clean(mocker):
1518 is_staging_clean_mock .return_value = False
1619
1720
21+ @contextlib .contextmanager
22+ def get_temp_dir ():
23+ temp_dir = tempfile .mkdtemp ()
24+ try :
25+ yield temp_dir
26+ finally :
27+ shutil .rmtree (temp_dir )
28+
29+
1830@pytest .mark .usefixtures ("staging_is_clean" )
1931def test_commit (mocker ):
2032 prompt_mock = mocker .patch ("questionary.prompt" )
@@ -158,3 +170,46 @@ def test_version():
158170
159171 commands .Version (config )()
160172 mocked_write .assert_called_once ()
173+
174+
175+ def test_check_no_conventional_commit (mocker ):
176+ with pytest .raises (SystemExit ):
177+ error_mock = mocker .patch ("commitizen.out.error" )
178+
179+ with get_temp_dir () as dir :
180+
181+ tempfile = os .path .join (dir , "temp_commit_file" )
182+ with open (tempfile , 'w' ) as f :
183+ f .write ("no conventional commit" )
184+
185+ check_cmd = commands .Check (
186+ config = config ,
187+ arguments = {"commit_msg_file" : tempfile }
188+ )
189+ check_cmd ()
190+ error_mock .assert_called_once ()
191+
192+
193+ def test_check_conventional_commit (mocker ):
194+ success_mock = mocker .patch ("commitizen.out.success" )
195+ with get_temp_dir () as dir :
196+
197+ tempfile = os .path .join (dir , "temp_commit_file" )
198+ with open (tempfile , 'w' ) as f :
199+ f .write ("feat(lang): added polish language" )
200+
201+ check_cmd = commands .Check (
202+ config = config ,
203+ arguments = {"commit_msg_file" : tempfile }
204+ )
205+
206+ check_cmd ()
207+ success_mock .assert_called_once ()
208+
209+
210+ def test_check_command_when_commit_file_not_found ():
211+ with pytest .raises (FileNotFoundError ):
212+ commands .Check (
213+ config = config ,
214+ arguments = {"commit_msg_file" : "" }
215+ )()
0 commit comments