Skip to content

Commit 1390d48

Browse files
committed
Merge branch 'CLI_bug_fix' into master
* CLI_bug_fix: Move the latest validator CLI test to where it must go now. Simplify the schemas in the tests for CLI validator detection. add test case Delete the failed test case Fix the bug of processing arguments[validator] in parse_args
2 parents 756de12 + 9c6c79a commit 1390d48

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

jsonschema/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,6 @@ def _namedAnyWithDefault(name):
191191

192192
def parse_args(args):
193193
arguments = vars(parser.parse_args(args=args or ["--help"]))
194-
if arguments["validator"] is None:
195-
arguments["validator"] = validator_for(arguments["schema"])
196194
if arguments["output"] != "plain" and arguments["error_format"]:
197195
raise parser.error(
198196
"--error-format can only be used with --output plain"
@@ -229,6 +227,9 @@ def run(arguments, stdout=sys.stdout, stderr=sys.stderr, stdin=sys.stdin):
229227
except _CannotLoadFile:
230228
return 1
231229

230+
if arguments["validator"] is None:
231+
arguments["validator"] = validator_for(schema)
232+
232233
try:
233234
arguments["validator"].check_schema(schema)
234235
except SchemaError as error:

jsonschema/tests/test_cli.py

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import subprocess
99
import sys
1010

11-
from jsonschema import Draft4Validator, __version__, cli
11+
from jsonschema import Draft4Validator, Draft7Validator, __version__, cli
1212
from jsonschema.exceptions import SchemaError, ValidationError
1313
from jsonschema.tests._helpers import captured_output
1414
from jsonschema.validators import _LATEST_VERSION, validate
@@ -675,17 +675,62 @@ def test_successful_validation_of_just_the_schema(self):
675675
stderr="",
676676
)
677677

678-
def test_successful_validation__of_just_the_schema_pretty_output(self):
678+
def test_successful_validation_of_just_the_schema_pretty_output(self):
679679
self.assertOutputs(
680680
files=dict(some_schema="{}", some_instance="{}"),
681681
argv=["--output", "pretty", "-i", "some_instance", "some_schema"],
682682
stdout="===[SUCCESS]===(some_instance)===\n",
683683
stderr="",
684684
)
685685

686-
def test_real_validator(self):
686+
def test_it_validates_using_the_latest_validator_when_unspecified(self):
687+
# There isn't a better way now I can think of to ensure that the
688+
# latest version was used, given that the call to validator_for
689+
# is hidden inside the CLI, so guard that that's the case, and
690+
# this test will have to be updated when versions change until
691+
# we can think of a better way to ensure this behavior.
692+
self.assertIs(Draft7Validator, _LATEST_VERSION)
693+
694+
self.assertOutputs(
695+
files=dict(some_schema='{"const": "check"}', some_instance='"a"'),
696+
argv=["-i", "some_instance", "some_schema"],
697+
exit_code=1,
698+
stdout="",
699+
stderr="a: 'check' was expected\n",
700+
)
701+
702+
def test_it_validates_using_draft7_when_specified(self):
703+
"""
704+
Specifically, `const` validation applies for Draft 7.
705+
"""
706+
schema = """
707+
{
708+
"$schema": "http://json-schema.org/draft-07/schema#",
709+
"const": "check"
710+
}
711+
"""
712+
instance = '"foo"'
687713
self.assertOutputs(
688-
files=dict(some_schema='{"minimum": 30}', some_instance="37"),
714+
files=dict(some_schema=schema, some_instance=instance),
715+
argv=["-i", "some_instance", "some_schema"],
716+
exit_code=1,
717+
stdout="",
718+
stderr="foo: 'check' was expected\n",
719+
)
720+
721+
def test_it_validates_using_draft4_when_specified(self):
722+
"""
723+
Specifically, `const` validation *does not* apply for Draft 4.
724+
"""
725+
schema = """
726+
{
727+
"$schema": "http://json-schema.org/draft-04/schema#",
728+
"const": "check"
729+
}
730+
"""
731+
instance = '"foo"'
732+
self.assertOutputs(
733+
files=dict(some_schema=schema, some_instance=instance),
689734
argv=["-i", "some_instance", "some_schema"],
690735
stdout="",
691736
stderr="",
@@ -717,15 +762,6 @@ def test_find_validator_in_jsonschema(self):
717762
)
718763
self.assertIs(arguments["validator"], Draft4Validator)
719764

720-
def test_latest_validator_is_the_default(self):
721-
arguments = cli.parse_args(
722-
[
723-
"--instance", "mem://some/instance",
724-
"mem://some/schema",
725-
]
726-
)
727-
self.assertIs(arguments["validator"], _LATEST_VERSION)
728-
729765
def test_unknown_output(self):
730766
# Avoid the help message on stdout
731767
with captured_output() as (stdout, stderr):

0 commit comments

Comments
 (0)