1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
from __future__ import annotations
from unittest import mock
import click
import pytest
from check_jsonschema import main as cli_main
from check_jsonschema.cli.parse_result import ParseResult, SchemaLoadingMode
class BoxedContext:
ref = None
def touch_files(dirpath, *filenames):
for fname in filenames:
(dirpath / fname).touch()
@pytest.fixture
def boxed_context():
return BoxedContext()
@pytest.fixture
def mock_parse_result():
args = ParseResult()
with mock.patch("check_jsonschema.cli.main_command.ParseResult") as m:
m.return_value = args
yield args
@pytest.fixture(autouse=True)
def mock_cli_exec(boxed_context):
def get_ctx(*args):
boxed_context.ref = click.get_current_context()
with mock.patch(
"check_jsonschema.cli.main_command.execute", side_effect=get_ctx
) as m:
yield m
@pytest.mark.parametrize(
"schemafile,builtin_schema,check_metaschema,expect_mode",
[
("foo.json", None, False, SchemaLoadingMode.filepath),
(None, "foo", False, SchemaLoadingMode.builtin),
(None, None, True, SchemaLoadingMode.metaschema),
],
)
def test_parse_result_set_schema(
schemafile, builtin_schema, check_metaschema, expect_mode
):
args = ParseResult()
# starts as None (always)
assert args.schema_path is None
args.set_schema(schemafile, builtin_schema, check_metaschema)
assert args.schema_mode == expect_mode
if schemafile:
assert args.schema_path == schemafile
if builtin_schema:
assert args.schema_path == builtin_schema
if check_metaschema:
assert args.schema_path is None
def test_requires_some_args(cli_runner):
result = cli_runner.invoke(cli_main, [])
assert result.exit_code == 2
def test_schemafile_and_instancefile(
cli_runner, mock_parse_result, in_tmp_dir, tmp_path
):
touch_files(tmp_path, "foo.json")
cli_runner.invoke(cli_main, ["--schemafile", "schema.json", "foo.json"])
assert mock_parse_result.schema_mode == SchemaLoadingMode.filepath
assert mock_parse_result.schema_path == "schema.json"
assert isinstance(mock_parse_result.instancefiles, tuple)
for f in mock_parse_result.instancefiles:
assert isinstance(f, click.utils.LazyFile)
assert tuple(f.name for f in mock_parse_result.instancefiles) == ("foo.json",)
def test_requires_at_least_one_instancefile(cli_runner):
result = cli_runner.invoke(cli_main, ["--schemafile", "schema.json"])
assert result.exit_code == 2
def test_requires_schemafile(cli_runner, in_tmp_dir, tmp_path):
touch_files(tmp_path, "foo.json")
result = cli_runner.invoke(cli_main, ["foo.json"])
assert result.exit_code == 2
def test_no_cache_defaults_false(cli_runner, mock_parse_result):
cli_runner.invoke(cli_main, ["--schemafile", "schema.json", "foo.json"])
assert mock_parse_result.disable_cache is False
def test_no_cache_flag_is_true(cli_runner, mock_parse_result, in_tmp_dir, tmp_path):
touch_files(tmp_path, "foo.json")
cli_runner.invoke(
cli_main, ["--schemafile", "schema.json", "foo.json", "--no-cache"]
)
assert mock_parse_result.disable_cache is True
@pytest.mark.parametrize(
"cmd_args",
[
[
"--schemafile",
"x.json",
"--builtin-schema",
"vendor.travis",
],
[
"--schemafile",
"x.json",
"--builtin-schema",
"vendor.travis",
"--check-metaschema",
],
[
"--schemafile",
"x.json",
"--check-metaschema",
],
[
"--builtin-schema",
"vendor.travis",
"--check-metaschema",
],
],
)
def test_mutex_schema_opts(cli_runner, cmd_args, in_tmp_dir, tmp_path):
touch_files(tmp_path, "foo.json")
result = cli_runner.invoke(cli_main, cmd_args + ["foo.json"])
assert result.exit_code == 2
assert "are mutually exclusive" in result.stderr
@pytest.mark.parametrize(
"cmd_args",
[
["--version"],
["--help"],
["-h"],
],
)
def test_supports_common_option(cli_runner, cmd_args):
result = cli_runner.invoke(cli_main, cmd_args)
assert result.exit_code == 0
@pytest.mark.parametrize(
"setting,expect_value", [(None, None), ("1", False), ("0", False)]
)
def test_no_color_env_var(
cli_runner, monkeypatch, setting, expect_value, boxed_context, in_tmp_dir, tmp_path
):
if setting is None:
monkeypatch.delenv("NO_COLOR", raising=False)
else:
monkeypatch.setenv("NO_COLOR", setting)
touch_files(tmp_path, "foo.json")
cli_runner.invoke(cli_main, ["--schemafile", "schema.json", "foo.json"])
assert boxed_context.ref.color == expect_value
@pytest.mark.parametrize(
"setting,expected_value",
[(None, None), ("auto", None), ("always", True), ("never", False)],
)
def test_color_cli_option(
cli_runner, setting, expected_value, boxed_context, in_tmp_dir, tmp_path
):
args = ["--schemafile", "schema.json", "foo.json"]
if setting:
args.extend(("--color", setting))
touch_files(tmp_path, "foo.json")
cli_runner.invoke(cli_main, args)
assert boxed_context.ref.color == expected_value
def test_no_color_env_var_overrides_cli_option(
cli_runner, monkeypatch, mock_cli_exec, boxed_context, in_tmp_dir, tmp_path
):
monkeypatch.setenv("NO_COLOR", "1")
touch_files(tmp_path, "foo.json")
cli_runner.invoke(
cli_main, ["--color=always", "--schemafile", "schema.json", "foo.json"]
)
assert boxed_context.ref.color is False
@pytest.mark.parametrize(
"setting,expected_value",
[("auto", 0), ("always", 0), ("never", 0), ("anything_else", 2)],
)
def test_color_cli_option_is_choice(
cli_runner, setting, expected_value, in_tmp_dir, tmp_path
):
touch_files(tmp_path, "foo.json")
assert (
cli_runner.invoke(
cli_main,
["--color", setting, "--schemafile", "schema.json", "foo.json"],
).exit_code
== expected_value
)
def test_formats_default_to_enabled(
cli_runner, mock_parse_result, in_tmp_dir, tmp_path
):
touch_files(tmp_path, "foo.json")
cli_runner.invoke(cli_main, ["--schemafile", "schema.json", "foo.json"])
assert mock_parse_result.disable_all_formats is False
assert mock_parse_result.disable_formats == ()
@pytest.mark.parametrize(
"addargs",
(
[
"--disable-formats",
"uri-reference",
"--disable-formats",
"date-time",
],
["--disable-formats", "uri-reference,date-time"],
),
)
def test_disable_selected_formats(
cli_runner, mock_parse_result, addargs, in_tmp_dir, tmp_path
):
touch_files(tmp_path, "foo.json")
cli_runner.invoke(
cli_main,
[
"--schemafile",
"schema.json",
"foo.json",
]
+ addargs,
)
assert mock_parse_result.disable_all_formats is False
assert set(mock_parse_result.disable_formats) == {"uri-reference", "date-time"}
@pytest.mark.parametrize(
"addargs",
(
[
"--disable-formats",
"uri-reference",
"--disable-formats",
"date-time",
"--disable-formats",
"*",
],
["--disable-formats", "*"],
["--disable-formats", "*,email"],
),
)
def test_disable_all_formats(
cli_runner, mock_parse_result, addargs, in_tmp_dir, tmp_path
):
touch_files(tmp_path, "foo.json")
# this should be an override, with or without other args
cli_runner.invoke(
cli_main,
[
"--schemafile",
"schema.json",
"foo.json",
]
+ addargs,
)
assert mock_parse_result.disable_all_formats is True
def test_can_specify_custom_validator_class(
cli_runner, mock_parse_result, mock_module, in_tmp_dir, tmp_path
):
mock_module("foo.py", "class MyValidator: pass")
import foo
touch_files(tmp_path, "foo.json")
result = cli_runner.invoke(
cli_main,
[
"--schemafile",
"schema.json",
"foo.json",
"--validator-class",
"foo:MyValidator",
],
)
assert result.exit_code == 0
assert mock_parse_result.validator_class == foo.MyValidator
@pytest.mark.parametrize(
"failmode", ("syntax", "import", "attr", "function", "non_callable")
)
def test_custom_validator_class_fails(
cli_runner, mock_parse_result, mock_module, failmode, in_tmp_dir, tmp_path
):
mock_module(
"foo.py",
"""\
class MyValidator: pass
def validator_func(*args, **kwargs):
return MyValidator(*args, **kwargs)
other_thing = 100
""",
)
if failmode == "syntax":
arg = "foo.MyValidator"
elif failmode == "import":
arg = "foo.bar:MyValidator"
elif failmode == "attr":
arg = "foo:no_such_attr"
elif failmode == "function":
arg = "foo:validator_func"
elif failmode == "non_callable":
arg = "foo:other_thing"
else:
raise NotImplementedError
touch_files(tmp_path, "foo.json")
result = cli_runner.invoke(
cli_main,
["--schemafile", "schema.json", "foo.json", "--validator-class", arg],
)
assert result.exit_code == 2
if failmode == "syntax":
assert "is not a valid specifier" in result.stderr
elif failmode == "import":
assert "was not an importable module" in result.stderr
elif failmode == "attr":
assert "was not resolvable to a class" in result.stderr
elif failmode in ("function", "non_callable"):
assert "is not a class" in result.stderr
else:
raise NotImplementedError
|