1+ import json
12import os
23
34import pytest
@@ -30,6 +31,14 @@ def ask(self):
3031 'tag_format = "$version"\n '
3132)
3233
34+ EXPECTED_JSON_CONFIG = {
35+ "commitizen" : {
36+ "name" : "cz_conventional_commits" ,
37+ "version" : "0.0.1" ,
38+ "tag_format" : "$version" ,
39+ }
40+ }
41+
3342
3443def test_init_without_setup_pre_commit_hook (tmpdir , mocker , config ):
3544 mocker .patch (
@@ -85,47 +94,54 @@ def test_init_without_choosing_tag(config, mocker, tmpdir):
8594
8695
8796class TestPreCommitCases :
88- @pytest .fixture (scope = "function" , autouse = True )
89- def default_choices ( _ , mocker ):
97+ @pytest .fixture (scope = "function" , params = [ "pyproject.toml" , ".cz.json" ] )
98+ def default_choice ( _ , request , mocker ):
9099 mocker .patch (
91100 "questionary.select" ,
92101 side_effect = [
93- FakeQuestion ("pyproject.toml" ),
102+ FakeQuestion (request . param ),
94103 FakeQuestion ("cz_conventional_commits" ),
95104 ],
96105 )
97106 mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
98107 mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
99108 mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
109+ return request .param
100110
101- def test_no_existing_pre_commit_conifg ( _ , tmpdir , config ):
111+ def test_no_existing_pre_commit_json_conifg ( _ , default_choice , tmpdir , config ):
102112 with tmpdir .as_cwd ():
103113 commands .Init (config )()
104114
105- with open ("pyproject.toml" , "r" ) as toml_file :
106- config_data = toml_file .read ()
107- assert config_data == expected_config
115+ with open (default_choice , "r" ) as file :
116+ if "json" in default_choice :
117+ assert json .load (file ) == EXPECTED_JSON_CONFIG
118+ else :
119+ config_data = file .read ()
120+ assert config_data == expected_config
108121
109122 with open (pre_commit_config_filename , "r" ) as pre_commit_file :
110123 pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
111124 assert pre_commit_config_data == {"repos" : [cz_hook_config ]}
112125
113- def test_empty_pre_commit_config (_ , tmpdir , config ):
126+ def test_empty_pre_commit_config (_ , default_choice , tmpdir , config ):
114127 with tmpdir .as_cwd ():
115128 p = tmpdir .join (pre_commit_config_filename )
116129 p .write ("" )
117130
118131 commands .Init (config )()
119132
120- with open ("pyproject.toml" , "r" ) as toml_file :
121- config_data = toml_file .read ()
122- assert config_data == expected_config
133+ with open (default_choice , "r" ) as file :
134+ if "json" in default_choice :
135+ assert json .load (file ) == EXPECTED_JSON_CONFIG
136+ else :
137+ config_data = file .read ()
138+ assert config_data == expected_config
123139
124140 with open (pre_commit_config_filename , "r" ) as pre_commit_file :
125141 pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
126142 assert pre_commit_config_data == {"repos" : [cz_hook_config ]}
127143
128- def test_pre_commit_config_without_cz_hook (_ , tmpdir , config ):
144+ def test_pre_commit_config_without_cz_hook (_ , default_choice , tmpdir , config ):
129145 existing_hook_config = {
130146 "repo" : "https://github.com/pre-commit/pre-commit-hooks" ,
131147 "rev" : "v1.2.3" ,
@@ -138,26 +154,32 @@ def test_pre_commit_config_without_cz_hook(_, tmpdir, config):
138154
139155 commands .Init (config )()
140156
141- with open ("pyproject.toml" , "r" ) as toml_file :
142- config_data = toml_file .read ()
143- assert config_data == expected_config
157+ with open (default_choice , "r" ) as file :
158+ if "json" in default_choice :
159+ assert json .load (file ) == EXPECTED_JSON_CONFIG
160+ else :
161+ config_data = file .read ()
162+ assert config_data == expected_config
144163
145164 with open (pre_commit_config_filename , "r" ) as pre_commit_file :
146165 pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
147166 assert pre_commit_config_data == {
148167 "repos" : [existing_hook_config , cz_hook_config ]
149168 }
150169
151- def test_cz_hook_exists_in_pre_commit_config (_ , tmpdir , config ):
170+ def test_cz_hook_exists_in_pre_commit_config (_ , default_choice , tmpdir , config ):
152171 with tmpdir .as_cwd ():
153172 p = tmpdir .join (pre_commit_config_filename )
154173 p .write (yaml .safe_dump ({"repos" : [cz_hook_config ]}))
155174
156175 commands .Init (config )()
157176
158- with open ("pyproject.toml" , "r" ) as toml_file :
159- config_data = toml_file .read ()
160- assert config_data == expected_config
177+ with open (default_choice , "r" ) as file :
178+ if "json" in default_choice :
179+ assert json .load (file ) == EXPECTED_JSON_CONFIG
180+ else :
181+ config_data = file .read ()
182+ assert config_data == expected_config
161183
162184 with open (pre_commit_config_filename , "r" ) as pre_commit_file :
163185 pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
0 commit comments