1
1
from __future__ import annotations
2
2
3
+ import io
3
4
from unittest import mock
4
5
5
6
import click
@@ -14,6 +15,11 @@ class BoxedContext:
14
15
ref = None
15
16
16
17
18
+ def touch_files (dirpath , * filenames ):
19
+ for fname in filenames :
20
+ (dirpath / fname ).touch ()
21
+
22
+
17
23
@pytest .fixture
18
24
def boxed_context ():
19
25
return BoxedContext ()
@@ -73,19 +79,24 @@ def test_requires_some_args(runner):
73
79
assert result .exit_code == 2
74
80
75
81
76
- def test_schemafile_and_instancefile (runner , mock_parse_result ):
82
+ def test_schemafile_and_instancefile (runner , mock_parse_result , in_tmp_dir , tmp_path ):
83
+ touch_files (tmp_path , "foo.json" )
77
84
runner .invoke (cli_main , ["--schemafile" , "schema.json" , "foo.json" ])
78
85
assert mock_parse_result .schema_mode == SchemaLoadingMode .filepath
79
86
assert mock_parse_result .schema_path == "schema.json"
80
- assert mock_parse_result .instancefiles == ("foo.json" ,)
87
+ assert isinstance (mock_parse_result .instancefiles , tuple )
88
+ for f in mock_parse_result .instancefiles :
89
+ assert isinstance (f , (io .BytesIO , io .BufferedReader ))
90
+ assert tuple (f .name for f in mock_parse_result .instancefiles ) == ("foo.json" ,)
81
91
82
92
83
93
def test_requires_at_least_one_instancefile (runner ):
84
94
result = runner .invoke (cli_main , ["--schemafile" , "schema.json" ])
85
95
assert result .exit_code == 2
86
96
87
97
88
- def test_requires_schemafile (runner ):
98
+ def test_requires_schemafile (runner , in_tmp_dir , tmp_path ):
99
+ touch_files (tmp_path , "foo.json" )
89
100
result = runner .invoke (cli_main , ["foo.json" ])
90
101
assert result .exit_code == 2
91
102
@@ -95,7 +106,8 @@ def test_no_cache_defaults_false(runner, mock_parse_result):
95
106
assert mock_parse_result .disable_cache is False
96
107
97
108
98
- def test_no_cache_flag_is_true (runner , mock_parse_result ):
109
+ def test_no_cache_flag_is_true (runner , mock_parse_result , in_tmp_dir , tmp_path ):
110
+ touch_files (tmp_path , "foo.json" )
99
111
runner .invoke (cli_main , ["--schemafile" , "schema.json" , "foo.json" , "--no-cache" ])
100
112
assert mock_parse_result .disable_cache is True
101
113
@@ -108,32 +120,29 @@ def test_no_cache_flag_is_true(runner, mock_parse_result):
108
120
"x.json" ,
109
121
"--builtin-schema" ,
110
122
"vendor.travis" ,
111
- "foo.json" ,
112
123
],
113
124
[
114
125
"--schemafile" ,
115
126
"x.json" ,
116
127
"--builtin-schema" ,
117
128
"vendor.travis" ,
118
129
"--check-metaschema" ,
119
- "foo.json" ,
120
130
],
121
131
[
122
132
"--schemafile" ,
123
133
"x.json" ,
124
134
"--check-metaschema" ,
125
- "foo.json" ,
126
135
],
127
136
[
128
137
"--builtin-schema" ,
129
138
"vendor.travis" ,
130
139
"--check-metaschema" ,
131
- "foo.json" ,
132
140
],
133
141
],
134
142
)
135
- def test_mutex_schema_opts (runner , cmd_args ):
136
- result = runner .invoke (cli_main , cmd_args )
143
+ def test_mutex_schema_opts (runner , cmd_args , in_tmp_dir , tmp_path ):
144
+ touch_files (tmp_path , "foo.json" )
145
+ result = runner .invoke (cli_main , cmd_args + ["foo.json" ])
137
146
assert result .exit_code == 2
138
147
assert "are mutually exclusive" in result .stderr
139
148
@@ -154,12 +163,15 @@ def test_supports_common_option(runner, cmd_args):
154
163
@pytest .mark .parametrize (
155
164
"setting,expect_value" , [(None , None ), ("1" , False ), ("0" , False )]
156
165
)
157
- def test_no_color_env_var (runner , monkeypatch , setting , expect_value , boxed_context ):
166
+ def test_no_color_env_var (
167
+ runner , monkeypatch , setting , expect_value , boxed_context , in_tmp_dir , tmp_path
168
+ ):
158
169
if setting is None :
159
170
monkeypatch .delenv ("NO_COLOR" , raising = False )
160
171
else :
161
172
monkeypatch .setenv ("NO_COLOR" , setting )
162
173
174
+ touch_files (tmp_path , "foo.json" )
163
175
runner .invoke (cli_main , ["--schemafile" , "schema.json" , "foo.json" ])
164
176
assert boxed_context .ref .color == expect_value
165
177
@@ -168,18 +180,22 @@ def test_no_color_env_var(runner, monkeypatch, setting, expect_value, boxed_cont
168
180
"setting,expected_value" ,
169
181
[(None , None ), ("auto" , None ), ("always" , True ), ("never" , False )],
170
182
)
171
- def test_color_cli_option (runner , setting , expected_value , boxed_context ):
183
+ def test_color_cli_option (
184
+ runner , setting , expected_value , boxed_context , in_tmp_dir , tmp_path
185
+ ):
172
186
args = ["--schemafile" , "schema.json" , "foo.json" ]
173
187
if setting :
174
188
args .extend (("--color" , setting ))
189
+ touch_files (tmp_path , "foo.json" )
175
190
runner .invoke (cli_main , args )
176
191
assert boxed_context .ref .color == expected_value
177
192
178
193
179
194
def test_no_color_env_var_overrides_cli_option (
180
- runner , monkeypatch , mock_cli_exec , boxed_context
195
+ runner , monkeypatch , mock_cli_exec , boxed_context , in_tmp_dir , tmp_path
181
196
):
182
197
monkeypatch .setenv ("NO_COLOR" , "1" )
198
+ touch_files (tmp_path , "foo.json" )
183
199
runner .invoke (
184
200
cli_main , ["--color=always" , "--schemafile" , "schema.json" , "foo.json" ]
185
201
)
@@ -190,16 +206,21 @@ def test_no_color_env_var_overrides_cli_option(
190
206
"setting,expected_value" ,
191
207
[("auto" , 0 ), ("always" , 0 ), ("never" , 0 ), ("anything_else" , 2 )],
192
208
)
193
- def test_color_cli_option_is_choice (runner , setting , expected_value ):
209
+ def test_color_cli_option_is_choice (
210
+ runner , setting , expected_value , in_tmp_dir , tmp_path
211
+ ):
212
+ touch_files (tmp_path , "foo.json" )
194
213
assert (
195
214
runner .invoke (
196
- cli_main , ["--color" , setting , "--schemafile" , "schema.json" , "foo.json" ]
215
+ cli_main ,
216
+ ["--color" , setting , "--schemafile" , "schema.json" , "foo.json" ],
197
217
).exit_code
198
218
== expected_value
199
219
)
200
220
201
221
202
- def test_formats_default_to_enabled (runner , mock_parse_result ):
222
+ def test_formats_default_to_enabled (runner , mock_parse_result , in_tmp_dir , tmp_path ):
223
+ touch_files (tmp_path , "foo.json" )
203
224
runner .invoke (cli_main , ["--schemafile" , "schema.json" , "foo.json" ])
204
225
assert mock_parse_result .disable_all_formats is False
205
226
assert mock_parse_result .disable_formats == ()
@@ -217,7 +238,10 @@ def test_formats_default_to_enabled(runner, mock_parse_result):
217
238
["--disable-formats" , "uri-reference,date-time" ],
218
239
),
219
240
)
220
- def test_disable_selected_formats (runner , mock_parse_result , addargs ):
241
+ def test_disable_selected_formats (
242
+ runner , mock_parse_result , addargs , in_tmp_dir , tmp_path
243
+ ):
244
+ touch_files (tmp_path , "foo.json" )
221
245
runner .invoke (
222
246
cli_main ,
223
247
[
@@ -246,7 +270,8 @@ def test_disable_selected_formats(runner, mock_parse_result, addargs):
246
270
["--disable-formats" , "*,email" ],
247
271
),
248
272
)
249
- def test_disable_all_formats (runner , mock_parse_result , addargs ):
273
+ def test_disable_all_formats (runner , mock_parse_result , addargs , in_tmp_dir , tmp_path ):
274
+ touch_files (tmp_path , "foo.json" )
250
275
# this should be an override, with or without other args
251
276
runner .invoke (
252
277
cli_main ,
@@ -260,10 +285,13 @@ def test_disable_all_formats(runner, mock_parse_result, addargs):
260
285
assert mock_parse_result .disable_all_formats is True
261
286
262
287
263
- def test_can_specify_custom_validator_class (runner , mock_parse_result , mock_module ):
288
+ def test_can_specify_custom_validator_class (
289
+ runner , mock_parse_result , mock_module , in_tmp_dir , tmp_path
290
+ ):
264
291
mock_module ("foo.py" , "class MyValidator: pass" )
265
292
import foo
266
293
294
+ touch_files (tmp_path , "foo.json" )
267
295
result = runner .invoke (
268
296
cli_main ,
269
297
[
@@ -281,7 +309,9 @@ def test_can_specify_custom_validator_class(runner, mock_parse_result, mock_modu
281
309
@pytest .mark .parametrize (
282
310
"failmode" , ("syntax" , "import" , "attr" , "function" , "non_callable" )
283
311
)
284
- def test_custom_validator_class_fails (runner , mock_parse_result , mock_module , failmode ):
312
+ def test_custom_validator_class_fails (
313
+ runner , mock_parse_result , mock_module , failmode , in_tmp_dir , tmp_path
314
+ ):
285
315
mock_module (
286
316
"foo.py" ,
287
317
"""\
@@ -307,6 +337,7 @@ def validator_func(*args, **kwargs):
307
337
else :
308
338
raise NotImplementedError
309
339
340
+ touch_files (tmp_path , "foo.json" )
310
341
result = runner .invoke (
311
342
cli_main ,
312
343
["--schemafile" , "schema.json" , "foo.json" , "--validator-class" , arg ],
0 commit comments