1
+ import json
1
2
import os
2
3
3
4
import pytest
@@ -30,6 +31,14 @@ def ask(self):
30
31
'tag_format = "$version"\n '
31
32
)
32
33
34
+ EXPECTED_JSON_CONFIG = {
35
+ "commitizen" : {
36
+ "name" : "cz_conventional_commits" ,
37
+ "version" : "0.0.1" ,
38
+ "tag_format" : "$version" ,
39
+ }
40
+ }
41
+
33
42
34
43
def test_init_without_setup_pre_commit_hook (tmpdir , mocker , config ):
35
44
mocker .patch (
@@ -85,47 +94,54 @@ def test_init_without_choosing_tag(config, mocker, tmpdir):
85
94
86
95
87
96
class 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 ):
90
99
mocker .patch (
91
100
"questionary.select" ,
92
101
side_effect = [
93
- FakeQuestion ("pyproject.toml" ),
102
+ FakeQuestion (request . param ),
94
103
FakeQuestion ("cz_conventional_commits" ),
95
104
],
96
105
)
97
106
mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
98
107
mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
99
108
mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
109
+ return request .param
100
110
101
- def test_no_existing_pre_commit_conifg ( _ , tmpdir , config ):
111
+ def test_no_existing_pre_commit_json_conifg ( _ , default_choice , tmpdir , config ):
102
112
with tmpdir .as_cwd ():
103
113
commands .Init (config )()
104
114
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
108
121
109
122
with open (pre_commit_config_filename , "r" ) as pre_commit_file :
110
123
pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
111
124
assert pre_commit_config_data == {"repos" : [cz_hook_config ]}
112
125
113
- def test_empty_pre_commit_config (_ , tmpdir , config ):
126
+ def test_empty_pre_commit_config (_ , default_choice , tmpdir , config ):
114
127
with tmpdir .as_cwd ():
115
128
p = tmpdir .join (pre_commit_config_filename )
116
129
p .write ("" )
117
130
118
131
commands .Init (config )()
119
132
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
123
139
124
140
with open (pre_commit_config_filename , "r" ) as pre_commit_file :
125
141
pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
126
142
assert pre_commit_config_data == {"repos" : [cz_hook_config ]}
127
143
128
- def test_pre_commit_config_without_cz_hook (_ , tmpdir , config ):
144
+ def test_pre_commit_config_without_cz_hook (_ , default_choice , tmpdir , config ):
129
145
existing_hook_config = {
130
146
"repo" : "https://github.com/pre-commit/pre-commit-hooks" ,
131
147
"rev" : "v1.2.3" ,
@@ -138,26 +154,32 @@ def test_pre_commit_config_without_cz_hook(_, tmpdir, config):
138
154
139
155
commands .Init (config )()
140
156
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
144
163
145
164
with open (pre_commit_config_filename , "r" ) as pre_commit_file :
146
165
pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
147
166
assert pre_commit_config_data == {
148
167
"repos" : [existing_hook_config , cz_hook_config ]
149
168
}
150
169
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 ):
152
171
with tmpdir .as_cwd ():
153
172
p = tmpdir .join (pre_commit_config_filename )
154
173
p .write (yaml .safe_dump ({"repos" : [cz_hook_config ]}))
155
174
156
175
commands .Init (config )()
157
176
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
161
183
162
184
with open (pre_commit_config_filename , "r" ) as pre_commit_file :
163
185
pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
0 commit comments