summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwillson-chen <willson.chenwx@gmail.com>2020-09-14 20:42:41 +0800
committerwillson-chen <willson.chenwx@gmail.com>2020-09-14 21:06:15 +0800
commita0ec5e9dff5c987966efe240f69f49e3c98696f8 (patch)
treeba14b5ebe031228e2c16f451a169cf183e913da5
parent0955ad094e725a7c0fc34e145326f6a62f3c5c5a (diff)
downloadjsonschema-a0ec5e9dff5c987966efe240f69f49e3c98696f8.tar.gz
Optimize the input parameters of '--base-uri'
-rw-r--r--jsonschema/cli.py18
-rw-r--r--jsonschema/tests/test_cli.py53
2 files changed, 4 insertions, 67 deletions
diff --git a/jsonschema/cli.py b/jsonschema/cli.py
index 093975c..c33352e 100644
--- a/jsonschema/cli.py
+++ b/jsonschema/cli.py
@@ -7,7 +7,6 @@ from textwrap import dedent
import argparse
import errno
import json
-import os
import sys
import traceback
@@ -16,7 +15,7 @@ import attr
from jsonschema import __version__
from jsonschema._reflect import namedAny
from jsonschema.exceptions import SchemaError
-from jsonschema.validators import validator_for, RefResolver
+from jsonschema.validators import RefResolver, validator_for
class _CannotLoadFile(Exception):
@@ -261,19 +260,10 @@ def run(arguments, stdout=sys.stdout, stderr=sys.stderr, stdin=sys.stdin):
raise _CannotLoadFile()
instances = ["<stdin>"]
- if arguments["base_uri"] is None:
- resolver = None
- elif arguments["base_uri"] == ".":
- file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
- resolver = RefResolver(
- base_uri=file_prefix.format(os.getcwd()),
+ resolver = RefResolver(
+ base_uri=arguments["base_uri"],
referrer=schema,
- )
- else:
- resolver = RefResolver(
- base_uri=arguments["base_uri"],
- referrer=schema,
- )
+ ) if arguments["base_uri"] is not None else None
validator = arguments["validator"](schema, resolver=resolver)
exit_code = 0
diff --git a/jsonschema/tests/test_cli.py b/jsonschema/tests/test_cli.py
index 63499df..d549b6d 100644
--- a/jsonschema/tests/test_cli.py
+++ b/jsonschema/tests/test_cli.py
@@ -684,59 +684,6 @@ class TestCLI(TestCase):
stderr="",
)
- def test_successful_validate_with_specifying_base_uri_relative_path(self):
- try:
- schema_file = tempfile.NamedTemporaryFile(
- mode='w+',
- prefix='schema',
- suffix='.json',
- dir='.',
- delete=False
- )
- self.addCleanup(os.remove, schema_file.name)
- schema = """
- {"type": "object", "properties": {"KEY1":
- {"$ref": %s%s#definitions/schemas"}},
- "definitions": {"schemas": {"type": "string"}}}
- """ % ("\"", os.path.basename(schema_file.name))
- schema_file.write(schema)
- finally:
- schema_file.close()
-
- self.assertOutputs(
- files=dict(some_schema=schema, some_instance='{"KEY1": "1"}'),
- argv=["-i", "some_instance", "--base-uri", ".", "some_schema"],
- stdout="",
- stderr="",
- )
-
- def test_failure_validate_with_specifying_base_uri_relative_path(self):
- try:
- schema_file = tempfile.NamedTemporaryFile(
- mode='w+',
- prefix='schema',
- suffix='.json',
- dir='.',
- delete=False
- )
- self.addCleanup(os.remove, schema_file.name)
- schema = """
- {"type": "object", "properties": {"KEY1":
- {"$ref": %s%s#definitions/schemas"}},
- "definitions": {"schemas": {"type": "string"}}}
- """ % ("\"", os.path.basename(schema_file.name))
- schema_file.write(schema)
- finally:
- schema_file.close()
-
- self.assertOutputs(
- files=dict(some_schema=schema, some_instance='{"KEY1": 1}'),
- argv=["-i", "some_instance", "--base-uri", ".", "some_schema"],
- exit_code=1,
- stdout="",
- stderr="1: 1 is not of type 'string'\n",
- )
-
def test_successful_validate_with_specifying_base_uri_absolute_path(self):
absolute_path = os.getcwd()
try: