1
1
from __future__ import annotations
2
2
3
- import typing as t
4
3
from unittest import mock
5
4
6
5
import click
7
6
import pytest
8
7
from click .testing import CliRunner
9
- from click .testing import Result as ClickResult
10
8
11
9
from check_jsonschema import main as cli_main
12
10
from check_jsonschema .cli import ParseResult , SchemaLoadingMode
13
11
14
12
13
+ class BoxedContext :
14
+ ref = None
15
+
16
+
17
+ @pytest .fixture
18
+ def boxed_context ():
19
+ return BoxedContext ()
20
+
21
+
15
22
@pytest .fixture
16
23
def mock_parse_result ():
17
24
args = ParseResult ()
@@ -21,8 +28,11 @@ def mock_parse_result():
21
28
22
29
23
30
@pytest .fixture (autouse = True )
24
- def mock_cli_exec ():
25
- with mock .patch ("check_jsonschema.cli.execute" ) as m :
31
+ def mock_cli_exec (boxed_context ):
32
+ def get_ctx (* args ):
33
+ boxed_context .ref = click .get_current_context ()
34
+
35
+ with mock .patch ("check_jsonschema.cli.execute" , side_effect = get_ctx ) as m :
26
36
yield m
27
37
28
38
@@ -31,28 +41,6 @@ def runner() -> CliRunner:
31
41
return CliRunner (mix_stderr = False )
32
42
33
43
34
- def invoke_and_get_ctx (
35
- runner : CliRunner ,
36
- cmd : click .Command ,
37
- args : t .Sequence [str ],
38
- ) -> tuple [ClickResult , click .Context ]:
39
- # There doesn't appear to be a good way to get the Click context used by a
40
- # test invocation, so we replace the invoke method with a wrapper that
41
- # calls `click.get_current_context` to extract the context object.
42
-
43
- ctx = None
44
-
45
- def extract_ctx (* args , ** kwargs ):
46
- nonlocal ctx
47
- ctx = click .get_current_context ()
48
- return click .Command .invoke (* args , ** kwargs )
49
-
50
- with mock .patch ("click.Command.invoke" , extract_ctx ):
51
- results = runner .invoke (cmd , args )
52
-
53
- return results , ctx
54
-
55
-
56
44
@pytest .mark .parametrize (
57
45
"schemafile,builtin_schema,check_metaschema,expect_mode" ,
58
46
[
@@ -78,34 +66,34 @@ def test_parse_result_set_schema(
78
66
assert args .schema_path is None
79
67
80
68
81
- def test_requires_some_args (runner : CliRunner ):
69
+ def test_requires_some_args (runner ):
82
70
result = runner .invoke (cli_main , [])
83
71
assert result .exit_code == 2
84
72
85
73
86
- def test_schemafile_and_instancefile (runner : CliRunner , mock_parse_result : ParseResult ):
74
+ def test_schemafile_and_instancefile (runner , mock_parse_result ):
87
75
runner .invoke (cli_main , ["--schemafile" , "schema.json" , "foo.json" ])
88
76
assert mock_parse_result .schema_mode == SchemaLoadingMode .filepath
89
77
assert mock_parse_result .schema_path == "schema.json"
90
78
assert mock_parse_result .instancefiles == ("foo.json" ,)
91
79
92
80
93
- def test_requires_at_least_one_instancefile (runner : CliRunner ):
81
+ def test_requires_at_least_one_instancefile (runner ):
94
82
result = runner .invoke (cli_main , ["--schemafile" , "schema.json" ])
95
83
assert result .exit_code == 2
96
84
97
85
98
- def test_requires_schemafile (runner : CliRunner ):
86
+ def test_requires_schemafile (runner ):
99
87
result = runner .invoke (cli_main , ["foo.json" ])
100
88
assert result .exit_code == 2
101
89
102
90
103
- def test_no_cache_defaults_false (runner : CliRunner , mock_parse_result : ParseResult ):
91
+ def test_no_cache_defaults_false (runner , mock_parse_result ):
104
92
runner .invoke (cli_main , ["--schemafile" , "schema.json" , "foo.json" ])
105
93
assert mock_parse_result .disable_cache is False
106
94
107
95
108
- def test_no_cache_flag_is_true (runner : CliRunner , mock_parse_result : ParseResult ):
96
+ def test_no_cache_flag_is_true (runner , mock_parse_result ):
109
97
runner .invoke (cli_main , ["--schemafile" , "schema.json" , "foo.json" , "--no-cache" ])
110
98
assert mock_parse_result .disable_cache is True
111
99
@@ -142,7 +130,7 @@ def test_no_cache_flag_is_true(runner: CliRunner, mock_parse_result: ParseResult
142
130
],
143
131
],
144
132
)
145
- def test_mutex_schema_opts (runner : CliRunner , cmd_args : list [ str ] ):
133
+ def test_mutex_schema_opts (runner , cmd_args ):
146
134
result = runner .invoke (cli_main , cmd_args )
147
135
assert result .exit_code == 2
148
136
assert "are mutually exclusive" in result .stderr
@@ -156,65 +144,51 @@ def test_mutex_schema_opts(runner: CliRunner, cmd_args: list[str]):
156
144
["-h" ],
157
145
],
158
146
)
159
- def test_supports_common_option (runner : CliRunner , cmd_args : list [ str ] ):
147
+ def test_supports_common_option (runner , cmd_args ):
160
148
result = runner .invoke (cli_main , cmd_args )
161
149
assert result .exit_code == 0
162
150
163
151
164
152
@pytest .mark .parametrize (
165
153
"setting,expect_value" , [(None , None ), ("1" , False ), ("0" , False )]
166
154
)
167
- def test_no_color_env_var (
168
- runner : CliRunner ,
169
- monkeypatch : pytest .MonkeyPatch ,
170
- setting : str | None ,
171
- expect_value : bool | None ,
172
- ):
155
+ def test_no_color_env_var (runner , monkeypatch , setting , expect_value , boxed_context ):
173
156
if setting is None :
174
157
monkeypatch .delenv ("NO_COLOR" , raising = False )
175
158
else :
176
159
monkeypatch .setenv ("NO_COLOR" , setting )
177
160
178
- _ , ctx = invoke_and_get_ctx (
179
- runner , cli_main , ["--schemafile" , "schema.json" , "foo.json" ]
180
- )
181
- assert ctx .color == expect_value
161
+ runner .invoke (cli_main , ["--schemafile" , "schema.json" , "foo.json" ])
162
+ assert boxed_context .ref .color == expect_value
182
163
183
164
184
165
@pytest .mark .parametrize (
185
166
"setting,expected_value" ,
186
167
[(None , None ), ("auto" , None ), ("always" , True ), ("never" , False )],
187
168
)
188
- def test_color_cli_option (
189
- runner : CliRunner ,
190
- setting : str | None ,
191
- expected_value : bool | None ,
192
- ):
169
+ def test_color_cli_option (runner , setting , expected_value , boxed_context ):
193
170
args = ["--schemafile" , "schema.json" , "foo.json" ]
194
171
if setting :
195
172
args .extend (("--color" , setting ))
196
- _ , ctx = invoke_and_get_ctx ( runner , cli_main , args )
197
- assert ctx .color == expected_value
173
+ runner . invoke ( cli_main , args )
174
+ assert boxed_context . ref .color == expected_value
198
175
199
176
200
177
def test_no_color_env_var_overrides_cli_option (
201
- runner : CliRunner , monkeypatch : pytest . MonkeyPatch
178
+ runner , monkeypatch , mock_cli_exec , boxed_context
202
179
):
203
180
monkeypatch .setenv ("NO_COLOR" , "1" )
204
-
205
- _ , ctx = invoke_and_get_ctx (
206
- runner , cli_main , ["--color=always" , "--schemafile" , "schema.json" , "foo.json" ]
181
+ runner .invoke (
182
+ cli_main , ["--color=always" , "--schemafile" , "schema.json" , "foo.json" ]
207
183
)
208
- assert ctx .color is False
184
+ assert boxed_context . ref .color is False
209
185
210
186
211
187
@pytest .mark .parametrize (
212
188
"setting,expected_value" ,
213
189
[("auto" , 0 ), ("always" , 0 ), ("never" , 0 ), ("anything_else" , 2 )],
214
190
)
215
- def test_color_cli_option_is_choice (
216
- runner : CliRunner , setting : str , expected_value : int
217
- ):
191
+ def test_color_cli_option_is_choice (runner , setting , expected_value ):
218
192
assert (
219
193
runner .invoke (
220
194
cli_main , ["--color" , setting , "--schemafile" , "schema.json" , "foo.json" ]
0 commit comments