@@ -26,40 +26,12 @@ ROOT_DIR = os.path.abspath(
26
26
os .path .join (os .path .dirname (__file__ ), os .pardir ).rstrip ("__pycache__" ),
27
27
)
28
28
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
- }
58
29
REMOTES_DIR = os .path .join (ROOT_DIR , "remotes" )
59
30
60
31
with open (os .path .join (ROOT_DIR , "test-schema.json" )) as schema :
61
32
TESTSUITE_SCHEMA = json .load (schema )
62
33
34
+
63
35
def files (paths ):
64
36
for path in paths :
65
37
with open (path ) as test_file :
@@ -80,27 +52,48 @@ def cases(paths):
80
52
81
53
82
54
def collect (root_dir ):
83
- for root , dirs , files in os .walk (root_dir ):
55
+ for root , _ , files in os .walk (root_dir ):
84
56
for filename in fnmatch .filter (files , "*.json" ):
85
57
yield os .path .join (root , filename )
86
58
87
59
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
+
88
69
class SanityTests (unittest .TestCase ):
89
70
@classmethod
90
71
def setUpClass (cls ):
91
72
print ("Looking for tests in %s" % SUITE_ROOT_DIR )
73
+ print ("Looking for remotes in %s" % REMOTES_DIR )
92
74
cls .test_files = list (collect (SUITE_ROOT_DIR ))
75
+ cls .remote_files = list (collect (REMOTES_DIR ))
93
76
print ("Found %s test files" % len (cls .test_files ))
77
+ print ("Found %s remote files" % len (cls .remote_files ))
94
78
assert cls .test_files , "Didn't find the test files!"
79
+ assert cls .remote_files , "Didn't find the remote files!"
95
80
96
- def test_all_files_are_valid_json (self ):
81
+ def test_all_test_files_are_valid_json (self ):
97
82
for path in self .test_files :
98
83
with open (path ) as test_file :
99
84
try :
100
85
json .load (test_file )
101
86
except ValueError as error :
102
87
self .fail ("%s contains invalid JSON (%s)" % (path , error ))
103
88
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
+
104
97
def test_all_descriptions_have_reasonable_length (self ):
105
98
for case in cases (self .test_files ):
106
99
description = case ["description" ]
@@ -146,49 +139,6 @@ class SanityTests(unittest.TestCase):
146
139
except jsonschema .ValidationError as error :
147
140
self .fail (str (error ))
148
141
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
-
192
142
def main (arguments ):
193
143
if arguments .command == "check" :
194
144
suite = unittest .TestLoader ().loadTestsFromTestCase (SanityTests )
@@ -202,31 +152,19 @@ def main(arguments):
202
152
203
153
json .dump (selected_cases , sys .stdout , indent = 4 , sort_keys = True )
204
154
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 )
206
157
elif arguments .command == "dump_remotes" :
207
158
if arguments .update :
208
159
shutil .rmtree (arguments .out_dir , ignore_errors = True )
209
160
210
161
try :
211
- os . makedirs ( arguments .out_dir )
162
+ shutil . copytree ( REMOTES_DIR , arguments .out_dir )
212
163
except OSError as e :
213
164
if e .errno == errno .EEXIST :
214
165
print ("%s already exists. Aborting." % arguments .out_dir )
215
166
sys .exit (1 )
216
167
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 " )
230
168
elif arguments .command == "serve" :
231
169
try :
232
170
from flask import Flask , jsonify
@@ -242,12 +180,13 @@ def main(arguments):
242
180
""" .strip ("\n " )))
243
181
sys .exit (1 )
244
182
183
+ remotes = collect_contents (REMOTES_DIR )
245
184
app = Flask (__name__ )
246
185
247
186
@app .route ("/<path:path>" )
248
187
def serve_path (path ):
249
- if path in REMOTES :
250
- return jsonify (REMOTES [path ])
188
+ if path in remotes :
189
+ return jsonify (remotes [path ])
251
190
return "Document does not exist." , 404
252
191
253
192
app .run (port = 1234 )
0 commit comments