1+ import os
2+
13import pytest
4+ import yaml
25
36from commitizen import commands
7+ from commitizen .__version__ import __version__
48from commitizen .exceptions import NoAnswersError
59
610
@@ -12,31 +16,42 @@ def ask(self):
1216 return self .expected_return
1317
1418
15- def test_init (tmpdir , mocker , config ):
19+ pre_commit_config_filename = ".pre-commit-config.yaml"
20+ cz_hook_config = {
21+ "repo" : "https://github.com/commitizen-tools/commitizen" ,
22+ "rev" : f"v{ __version__ } " ,
23+ "hooks" : [{"id" : "commitizen" , "stages" : ["commit-msg" ]}],
24+ }
25+
26+ expected_config = (
27+ "[tool.commitizen]\n "
28+ 'name = "cz_conventional_commits"\n '
29+ 'version = "0.0.1"\n '
30+ 'tag_format = "$version"\n '
31+ )
32+
33+
34+ def test_init_without_setup_pre_commit_hook (tmpdir , mocker , config ):
1635 mocker .patch (
1736 "questionary.select" ,
1837 side_effect = [
1938 FakeQuestion ("pyproject.toml" ),
2039 FakeQuestion ("cz_conventional_commits" ),
2140 ],
2241 )
23- mocker .patch ("questionary.confirm" , return_value = FakeQuestion ("y" ))
24- mocker .patch ("questionary.text" , return_value = FakeQuestion ("y" ))
25- expected_config = (
26- "[tool.commitizen]\n "
27- 'name = "cz_conventional_commits"\n '
28- 'version = "0.0.1"\n '
29- 'tag_format = "y"\n '
30- )
42+ mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
43+ mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
44+ mocker .patch ("questionary.confirm" , return_value = FakeQuestion (False ))
3145
3246 with tmpdir .as_cwd ():
3347 commands .Init (config )()
3448
3549 with open ("pyproject.toml" , "r" ) as toml_file :
3650 config_data = toml_file .read ()
37-
3851 assert config_data == expected_config
3952
53+ assert not os .path .isfile (pre_commit_config_filename )
54+
4055
4156def test_init_when_config_already_exists (config , capsys ):
4257 # Set config path
@@ -67,3 +82,85 @@ def test_init_without_choosing_tag(config, mocker, tmpdir):
6782 with tmpdir .as_cwd ():
6883 with pytest .raises (NoAnswersError ):
6984 commands .Init (config )()
85+
86+
87+ class TestPreCommitCases :
88+ @pytest .fixture (scope = "function" , autouse = True )
89+ def default_choices (_ , mocker ):
90+ mocker .patch (
91+ "questionary.select" ,
92+ side_effect = [
93+ FakeQuestion ("pyproject.toml" ),
94+ FakeQuestion ("cz_conventional_commits" ),
95+ ],
96+ )
97+ mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
98+ mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
99+ mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
100+
101+ def test_no_existing_pre_commit_conifg (_ , tmpdir , config ):
102+ with tmpdir .as_cwd ():
103+ commands .Init (config )()
104+
105+ with open ("pyproject.toml" , "r" ) as toml_file :
106+ config_data = toml_file .read ()
107+ assert config_data == expected_config
108+
109+ with open (pre_commit_config_filename , "r" ) as pre_commit_file :
110+ pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
111+ assert pre_commit_config_data == {"repos" : [cz_hook_config ]}
112+
113+ def test_empty_pre_commit_config (_ , tmpdir , config ):
114+ with tmpdir .as_cwd ():
115+ p = tmpdir .join (pre_commit_config_filename )
116+ p .write ("" )
117+
118+ commands .Init (config )()
119+
120+ with open ("pyproject.toml" , "r" ) as toml_file :
121+ config_data = toml_file .read ()
122+ assert config_data == expected_config
123+
124+ with open (pre_commit_config_filename , "r" ) as pre_commit_file :
125+ pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
126+ assert pre_commit_config_data == {"repos" : [cz_hook_config ]}
127+
128+ def test_pre_commit_config_without_cz_hook (_ , tmpdir , config ):
129+ existing_hook_config = {
130+ "repo" : "https://github.com/pre-commit/pre-commit-hooks" ,
131+ "rev" : "v1.2.3" ,
132+ "hooks" : [{"id" , "trailing-whitespace" }],
133+ }
134+
135+ with tmpdir .as_cwd ():
136+ p = tmpdir .join (pre_commit_config_filename )
137+ p .write (yaml .safe_dump ({"repos" : [existing_hook_config ]}))
138+
139+ commands .Init (config )()
140+
141+ with open ("pyproject.toml" , "r" ) as toml_file :
142+ config_data = toml_file .read ()
143+ assert config_data == expected_config
144+
145+ with open (pre_commit_config_filename , "r" ) as pre_commit_file :
146+ pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
147+ assert pre_commit_config_data == {
148+ "repos" : [existing_hook_config , cz_hook_config ]
149+ }
150+
151+ def test_cz_hook_exists_in_pre_commit_config (_ , tmpdir , config ):
152+ with tmpdir .as_cwd ():
153+ p = tmpdir .join (pre_commit_config_filename )
154+ p .write (yaml .safe_dump ({"repos" : [cz_hook_config ]}))
155+
156+ commands .Init (config )()
157+
158+ with open ("pyproject.toml" , "r" ) as toml_file :
159+ config_data = toml_file .read ()
160+ assert config_data == expected_config
161+
162+ with open (pre_commit_config_filename , "r" ) as pre_commit_file :
163+ pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
164+
165+ # check that config is not duplicated
166+ assert pre_commit_config_data == {"repos" : [cz_hook_config ]}
0 commit comments