11from __future__ import annotations
22
3+ import io
34from unittest import mock
45
56import click
@@ -14,6 +15,11 @@ class BoxedContext:
1415 ref = None
1516
1617
18+ def touch_files (dirpath , * filenames ):
19+ for fname in filenames :
20+ (dirpath / fname ).touch ()
21+
22+
1723@pytest .fixture
1824def boxed_context ():
1925 return BoxedContext ()
@@ -73,19 +79,24 @@ def test_requires_some_args(runner):
7379 assert result .exit_code == 2
7480
7581
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" )
7784 runner .invoke (cli_main , ["--schemafile" , "schema.json" , "foo.json" ])
7885 assert mock_parse_result .schema_mode == SchemaLoadingMode .filepath
7986 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" ,)
8191
8292
8393def test_requires_at_least_one_instancefile (runner ):
8494 result = runner .invoke (cli_main , ["--schemafile" , "schema.json" ])
8595 assert result .exit_code == 2
8696
8797
88- def test_requires_schemafile (runner ):
98+ def test_requires_schemafile (runner , in_tmp_dir , tmp_path ):
99+ touch_files (tmp_path , "foo.json" )
89100 result = runner .invoke (cli_main , ["foo.json" ])
90101 assert result .exit_code == 2
91102
@@ -95,7 +106,8 @@ def test_no_cache_defaults_false(runner, mock_parse_result):
95106 assert mock_parse_result .disable_cache is False
96107
97108
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" )
99111 runner .invoke (cli_main , ["--schemafile" , "schema.json" , "foo.json" , "--no-cache" ])
100112 assert mock_parse_result .disable_cache is True
101113
@@ -108,32 +120,29 @@ def test_no_cache_flag_is_true(runner, mock_parse_result):
108120 "x.json" ,
109121 "--builtin-schema" ,
110122 "vendor.travis" ,
111- "foo.json" ,
112123 ],
113124 [
114125 "--schemafile" ,
115126 "x.json" ,
116127 "--builtin-schema" ,
117128 "vendor.travis" ,
118129 "--check-metaschema" ,
119- "foo.json" ,
120130 ],
121131 [
122132 "--schemafile" ,
123133 "x.json" ,
124134 "--check-metaschema" ,
125- "foo.json" ,
126135 ],
127136 [
128137 "--builtin-schema" ,
129138 "vendor.travis" ,
130139 "--check-metaschema" ,
131- "foo.json" ,
132140 ],
133141 ],
134142)
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" ])
137146 assert result .exit_code == 2
138147 assert "are mutually exclusive" in result .stderr
139148
@@ -154,12 +163,15 @@ def test_supports_common_option(runner, cmd_args):
154163@pytest .mark .parametrize (
155164 "setting,expect_value" , [(None , None ), ("1" , False ), ("0" , False )]
156165)
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+ ):
158169 if setting is None :
159170 monkeypatch .delenv ("NO_COLOR" , raising = False )
160171 else :
161172 monkeypatch .setenv ("NO_COLOR" , setting )
162173
174+ touch_files (tmp_path , "foo.json" )
163175 runner .invoke (cli_main , ["--schemafile" , "schema.json" , "foo.json" ])
164176 assert boxed_context .ref .color == expect_value
165177
@@ -168,18 +180,22 @@ def test_no_color_env_var(runner, monkeypatch, setting, expect_value, boxed_cont
168180 "setting,expected_value" ,
169181 [(None , None ), ("auto" , None ), ("always" , True ), ("never" , False )],
170182)
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+ ):
172186 args = ["--schemafile" , "schema.json" , "foo.json" ]
173187 if setting :
174188 args .extend (("--color" , setting ))
189+ touch_files (tmp_path , "foo.json" )
175190 runner .invoke (cli_main , args )
176191 assert boxed_context .ref .color == expected_value
177192
178193
179194def 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
181196):
182197 monkeypatch .setenv ("NO_COLOR" , "1" )
198+ touch_files (tmp_path , "foo.json" )
183199 runner .invoke (
184200 cli_main , ["--color=always" , "--schemafile" , "schema.json" , "foo.json" ]
185201 )
@@ -190,16 +206,21 @@ def test_no_color_env_var_overrides_cli_option(
190206 "setting,expected_value" ,
191207 [("auto" , 0 ), ("always" , 0 ), ("never" , 0 ), ("anything_else" , 2 )],
192208)
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" )
194213 assert (
195214 runner .invoke (
196- cli_main , ["--color" , setting , "--schemafile" , "schema.json" , "foo.json" ]
215+ cli_main ,
216+ ["--color" , setting , "--schemafile" , "schema.json" , "foo.json" ],
197217 ).exit_code
198218 == expected_value
199219 )
200220
201221
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" )
203224 runner .invoke (cli_main , ["--schemafile" , "schema.json" , "foo.json" ])
204225 assert mock_parse_result .disable_all_formats is False
205226 assert mock_parse_result .disable_formats == ()
@@ -217,7 +238,10 @@ def test_formats_default_to_enabled(runner, mock_parse_result):
217238 ["--disable-formats" , "uri-reference,date-time" ],
218239 ),
219240)
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" )
221245 runner .invoke (
222246 cli_main ,
223247 [
@@ -246,7 +270,8 @@ def test_disable_selected_formats(runner, mock_parse_result, addargs):
246270 ["--disable-formats" , "*,email" ],
247271 ),
248272)
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" )
250275 # this should be an override, with or without other args
251276 runner .invoke (
252277 cli_main ,
@@ -260,10 +285,13 @@ def test_disable_all_formats(runner, mock_parse_result, addargs):
260285 assert mock_parse_result .disable_all_formats is True
261286
262287
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+ ):
264291 mock_module ("foo.py" , "class MyValidator: pass" )
265292 import foo
266293
294+ touch_files (tmp_path , "foo.json" )
267295 result = runner .invoke (
268296 cli_main ,
269297 [
@@ -281,7 +309,9 @@ def test_can_specify_custom_validator_class(runner, mock_parse_result, mock_modu
281309@pytest .mark .parametrize (
282310 "failmode" , ("syntax" , "import" , "attr" , "function" , "non_callable" )
283311)
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+ ):
285315 mock_module (
286316 "foo.py" ,
287317 """\
@@ -307,6 +337,7 @@ def validator_func(*args, **kwargs):
307337 else :
308338 raise NotImplementedError
309339
340+ touch_files (tmp_path , "foo.json" )
310341 result = runner .invoke (
311342 cli_main ,
312343 ["--schemafile" , "schema.json" , "foo.json" , "--validator-class" , arg ],
0 commit comments