Skip to content

Commit 54594c3

Browse files
committed
Remove remotes from bin/jsonschema_suite
Now it fetches them from the filesystem for serving, checking and displaying
1 parent fcae732 commit 54594c3

File tree

1 file changed

+30
-91
lines changed

1 file changed

+30
-91
lines changed

bin/jsonschema_suite

+30-91
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,27 +52,48 @@ 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

8759

60+
def collect_contents(root_dir):
61+
files = {}
62+
for path in collect(root_dir):
63+
relative_path = os.path.relpath(path, root_dir)
64+
with open(path) as schema_file:
65+
files[relative_path] = json.load(schema_file)
66+
return files
67+
68+
8869
class SanityTests(unittest.TestCase):
8970
@classmethod
9071
def setUpClass(cls):
9172
print("Looking for tests in %s" % SUITE_ROOT_DIR)
73+
print("Looking for remotes in %s" % REMOTES_DIR)
9274
cls.test_files = list(collect(SUITE_ROOT_DIR))
75+
cls.remote_files = list(collect(REMOTES_DIR))
9376
print("Found %s test files" % len(cls.test_files))
77+
print("Found %s remote files" % len(cls.remote_files))
9478
assert cls.test_files, "Didn't find the test files!"
79+
assert cls.remote_files, "Didn't find the remote files!"
9580

96-
def test_all_files_are_valid_json(self):
81+
def test_all_test_files_are_valid_json(self):
9782
for path in self.test_files:
9883
with open(path) as test_file:
9984
try:
10085
json.load(test_file)
10186
except ValueError as error:
10287
self.fail("%s contains invalid JSON (%s)" % (path, error))
10388

89+
def test_all_remote_files_are_valid_json(self):
90+
for path in self.remote_files:
91+
with open(path) as remote_file:
92+
try:
93+
json.load(remote_file)
94+
except ValueError as error:
95+
self.fail("%s contains invalid JSON (%s)" % (path, error))
96+
10497
def test_all_descriptions_have_reasonable_length(self):
10598
for case in cases(self.test_files):
10699
description = case["description"]
@@ -146,49 +139,6 @@ class SanityTests(unittest.TestCase):
146139
except jsonschema.ValidationError as error:
147140
self.fail(str(error))
148141

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-
191-
192142
def main(arguments):
193143
if arguments.command == "check":
194144
suite = unittest.TestLoader().loadTestsFromTestCase(SanityTests)
@@ -202,31 +152,19 @@ def main(arguments):
202152

203153
json.dump(selected_cases, sys.stdout, indent=4, sort_keys=True)
204154
elif arguments.command == "remotes":
205-
json.dump(REMOTES, sys.stdout, indent=4, sort_keys=True)
155+
remotes = collect_contents(REMOTES_DIR)
156+
json.dump(remotes, sys.stdout, indent=4, sort_keys=True)
206157
elif arguments.command == "dump_remotes":
207158
if arguments.update:
208159
shutil.rmtree(arguments.out_dir, ignore_errors=True)
209160

210161
try:
211-
os.makedirs(arguments.out_dir)
162+
shutil.copytree(REMOTES_DIR, arguments.out_dir)
212163
except OSError as e:
213164
if e.errno == errno.EEXIST:
214165
print("%s already exists. Aborting." % arguments.out_dir)
215166
sys.exit(1)
216167
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")
230168
elif arguments.command == "serve":
231169
try:
232170
from flask import Flask, jsonify
@@ -242,12 +180,13 @@ def main(arguments):
242180
""".strip("\n")))
243181
sys.exit(1)
244182

183+
remotes = collect_contents(REMOTES_DIR)
245184
app = Flask(__name__)
246185

247186
@app.route("/<path:path>")
248187
def serve_path(path):
249-
if path in REMOTES:
250-
return jsonify(REMOTES[path])
188+
if path in remotes:
189+
return jsonify(remotes[path])
251190
return "Document does not exist.", 404
252191

253192
app.run(port=1234)

0 commit comments

Comments
 (0)