Skip to content

Commit 3c45b81

Browse files
authored
Merge pull request #460 from amosonn/remove-remotes-from-script
Remove remotes from bin/jsonschema_suite
2 parents b09e48d + ebbcbc8 commit 3c45b81

File tree

1 file changed

+25
-93
lines changed

1 file changed

+25
-93
lines changed

bin/jsonschema_suite

Lines changed: 25 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,12 @@ ROOT_DIR = os.path.abspath(
2626
os.path.join(os.path.dirname(__file__), os.pardir).rstrip("__pycache__"),
2727
)
2828
SUITE_ROOT_DIR = os.path.join(ROOT_DIR, "tests")
29-
30-
REMOTES = {
31-
"integer.json": {u"type": u"integer"},
32-
"name.json": {
33-
u"type": "string",
34-
u"definitions": {
35-
u"orNull": {u"anyOf": [{u"type": u"null"}, {u"$ref": u"#"}]},
36-
},
37-
},
38-
"name-defs.json": {
39-
u"type": "string",
40-
u"$defs": {
41-
u"orNull": {u"anyOf": [{u"type": u"null"}, {u"$ref": u"#"}]},
42-
},
43-
},
44-
"subSchemas.json": {
45-
u"integer": {u"type": u"integer"},
46-
u"refToInteger": {u"$ref": u"#/integer"},
47-
},
48-
"subSchemas-defs.json": {
49-
u"$defs": {
50-
u"integer": {u"type": u"integer"},
51-
u"refToInteger": {u"$ref": u"#/$defs/integer"},
52-
}
53-
},
54-
"baseUriChange/folderInteger.json": {u"type": u"integer"},
55-
"baseUriChangeFolder/folderInteger.json": {u"type": u"integer"},
56-
"baseUriChangeFolderInSubschema/folderInteger.json": {u"type": u"integer"},
57-
}
5829
REMOTES_DIR = os.path.join(ROOT_DIR, "remotes")
5930

6031
with open(os.path.join(ROOT_DIR, "test-schema.json")) as schema:
6132
TESTSUITE_SCHEMA = json.load(schema)
6233

34+
6335
def files(paths):
6436
for path in paths:
6537
with open(path) as test_file:
@@ -80,7 +52,7 @@ def cases(paths):
8052

8153

8254
def collect(root_dir):
83-
for root, dirs, files in os.walk(root_dir):
55+
for root, _, files in os.walk(root_dir):
8456
for filename in fnmatch.filter(files, "*.json"):
8557
yield os.path.join(root, filename)
8658

@@ -89,18 +61,30 @@ class SanityTests(unittest.TestCase):
8961
@classmethod
9062
def setUpClass(cls):
9163
print("Looking for tests in %s" % SUITE_ROOT_DIR)
64+
print("Looking for remotes in %s" % REMOTES_DIR)
9265
cls.test_files = list(collect(SUITE_ROOT_DIR))
66+
cls.remote_files = list(collect(REMOTES_DIR))
9367
print("Found %s test files" % len(cls.test_files))
68+
print("Found %s remote files" % len(cls.remote_files))
9469
assert cls.test_files, "Didn't find the test files!"
70+
assert cls.remote_files, "Didn't find the remote files!"
9571

96-
def test_all_files_are_valid_json(self):
72+
def test_all_test_files_are_valid_json(self):
9773
for path in self.test_files:
9874
with open(path) as test_file:
9975
try:
10076
json.load(test_file)
10177
except ValueError as error:
10278
self.fail("%s contains invalid JSON (%s)" % (path, error))
10379

80+
def test_all_remote_files_are_valid_json(self):
81+
for path in self.remote_files:
82+
with open(path) as remote_file:
83+
try:
84+
json.load(remote_file)
85+
except ValueError as error:
86+
self.fail("%s contains invalid JSON (%s)" % (path, error))
87+
10488
def test_all_descriptions_have_reasonable_length(self):
10589
for case in cases(self.test_files):
10690
description = case["description"]
@@ -146,48 +130,6 @@ class SanityTests(unittest.TestCase):
146130
except jsonschema.ValidationError as error:
147131
self.fail(str(error))
148132

149-
def test_remote_schemas_are_updated(self):
150-
files = {}
151-
for parent, _, paths in os.walk(REMOTES_DIR):
152-
for path in paths:
153-
absolute_path = os.path.join(parent, path)
154-
with open(absolute_path) as schema_file:
155-
files[absolute_path] = json.load(schema_file)
156-
157-
expected = {
158-
os.path.join(REMOTES_DIR, path): contents
159-
for path, contents in REMOTES.items()
160-
}
161-
162-
missing = set(files).symmetric_difference(expected)
163-
changed = {
164-
path
165-
for path, contents in expected.items()
166-
if path in files
167-
and contents != files[path]
168-
}
169-
170-
self.assertEqual(
171-
files,
172-
expected,
173-
msg=textwrap.dedent(
174-
"""
175-
Remotes in the remotes/ directory do not match those in the
176-
``jsonschema_suite`` Python script.
177-
178-
Unfortunately for the minute, each remote file is duplicated in
179-
two places.""" + ("""
180-
181-
Only present in one location:
182-
183-
{}""".format("\n".join(missing)) if missing else "") + ("""
184-
185-
Conflicting between the two:
186-
187-
{}""".format("\n".join(changed)) if changed else "")
188-
)
189-
)
190-
191133

192134
def main(arguments):
193135
if arguments.command == "check":
@@ -202,34 +144,26 @@ def main(arguments):
202144

203145
json.dump(selected_cases, sys.stdout, indent=4, sort_keys=True)
204146
elif arguments.command == "remotes":
205-
json.dump(REMOTES, sys.stdout, indent=4, sort_keys=True)
147+
remotes = {}
148+
for path in collect(REMOTES_DIR):
149+
relative_path = os.path.relpath(path, REMOTES_DIR)
150+
with open(path) as schema_file:
151+
remotes[relative_path] = json.load(schema_file)
152+
json.dump(remotes, sys.stdout, indent=4, sort_keys=True)
206153
elif arguments.command == "dump_remotes":
207154
if arguments.update:
208155
shutil.rmtree(arguments.out_dir, ignore_errors=True)
209156

210157
try:
211-
os.makedirs(arguments.out_dir)
158+
shutil.copytree(REMOTES_DIR, arguments.out_dir)
212159
except OSError as e:
213160
if e.errno == errno.EEXIST:
214161
print("%s already exists. Aborting." % arguments.out_dir)
215162
sys.exit(1)
216163
raise
217-
218-
for url, schema in REMOTES.items():
219-
filepath = os.path.join(arguments.out_dir, url)
220-
221-
try:
222-
os.makedirs(os.path.dirname(filepath))
223-
except OSError as e:
224-
if e.errno != errno.EEXIST:
225-
raise
226-
227-
with open(filepath, "w") as out_file:
228-
json.dump(schema, out_file, indent=4, sort_keys=True)
229-
out_file.write("\n")
230164
elif arguments.command == "serve":
231165
try:
232-
from flask import Flask, jsonify
166+
import flask
233167
except ImportError:
234168
print(textwrap.dedent("""
235169
The Flask library is required to serve the remote schemas.
@@ -242,13 +176,11 @@ def main(arguments):
242176
""".strip("\n")))
243177
sys.exit(1)
244178

245-
app = Flask(__name__)
179+
app = flask.Flask(__name__)
246180

247181
@app.route("/<path:path>")
248182
def serve_path(path):
249-
if path in REMOTES:
250-
return jsonify(REMOTES[path])
251-
return "Document does not exist.", 404
183+
return flask.send_from_directory(REMOTES_DIR, path)
252184

253185
app.run(port=1234)
254186

0 commit comments

Comments
 (0)