10
10
import json
11
11
import os
12
12
import re
13
- import subprocess
14
13
import sys
15
14
import unittest
16
15
21
20
if TYPE_CHECKING :
22
21
from collections .abc import Iterable , Mapping , Sequence
23
22
23
+ from referencing .jsonschema import Schema
24
24
import pyperf
25
25
26
26
from jsonschema .validators import _VALIDATORS
27
27
import jsonschema
28
28
29
+ MAGIC_REMOTE_URL = "http://localhost:1234"
30
+
29
31
_DELIMITERS = re .compile (r"[\W\- ]+" )
30
32
31
33
@@ -51,38 +53,7 @@ def _find_suite():
51
53
class Suite :
52
54
53
55
_root : Path = field (factory = _find_suite )
54
- _remotes : referencing .jsonschema .SchemaRegistry = field (init = False )
55
-
56
- def __attrs_post_init__ (self ):
57
- jsonschema_suite = self ._root .joinpath ("bin" , "jsonschema_suite" )
58
- argv = [sys .executable , str (jsonschema_suite ), "remotes" ]
59
- remotes = subprocess .check_output (argv ).decode ("utf-8" )
60
-
61
- resources = json .loads (remotes )
62
56
63
- li = "http://localhost:1234/locationIndependentIdentifierPre2019.json"
64
- li4 = "http://localhost:1234/locationIndependentIdentifierDraft4.json"
65
-
66
- registry = Registry ().with_resources (
67
- [
68
- (
69
- li ,
70
- referencing .jsonschema .DRAFT7 .create_resource (
71
- contents = resources .pop (li ),
72
- ),
73
- ),
74
- (
75
- li4 ,
76
- referencing .jsonschema .DRAFT4 .create_resource (
77
- contents = resources .pop (li4 ),
78
- ),
79
- ),
80
- ],
81
- ).with_contents (
82
- resources .items (),
83
- default_specification = referencing .jsonschema .DRAFT202012 ,
84
- )
85
- object .__setattr__ (self , "_remotes" , registry )
86
57
87
58
def benchmark (self , runner : pyperf .Runner ): # pragma: no cover
88
59
for name , Validator in _VALIDATORS .items ():
@@ -92,10 +63,18 @@ def benchmark(self, runner: pyperf.Runner): # pragma: no cover
92
63
)
93
64
94
65
def version (self , name ) -> Version :
66
+ Validator = _VALIDATORS [name ]
67
+ uri : str = Validator .ID_OF (Validator .META_SCHEMA ) # type: ignore[assignment]
68
+ specification = referencing .jsonschema .specification_with (uri )
69
+
70
+ registry = Registry ().with_contents (
71
+ remotes_in (root = self ._root / "remotes" , name = name , uri = uri ),
72
+ default_specification = specification ,
73
+ )
95
74
return Version (
96
75
name = name ,
97
76
path = self ._root / "tests" / name ,
98
- remotes = self . _remotes ,
77
+ remotes = registry ,
99
78
)
100
79
101
80
@@ -187,6 +166,36 @@ def benchmark(self, runner: pyperf.Runner, **kwargs): # pragma: no cover
187
166
)
188
167
189
168
169
+ def remotes_in (
170
+ root : Path ,
171
+ name : str ,
172
+ uri : str ,
173
+ ) -> Iterable [tuple [str , Schema ]]:
174
+ # This messy logic is because the test suite is terrible at indicating
175
+ # what remotes are needed for what drafts, and mixes in schemas which
176
+ # have no $schema and which are invalid under earlier versions, in with
177
+ # other schemas which are needed for tests.
178
+
179
+ for each in root .rglob ("*.json" ):
180
+ schema = json .loads (each .read_text ())
181
+
182
+ relative = str (each .relative_to (root )).replace ("\\ " , "/" )
183
+
184
+ if (
185
+ ( # invalid boolean schema
186
+ name in {"draft3" , "draft4" }
187
+ and each .stem == "tree"
188
+ ) or
189
+ ( # draft<NotThisDialect>/*.json
190
+ "$schema" not in schema
191
+ and relative .startswith ("draft" )
192
+ and not relative .startswith (name )
193
+ )
194
+ ):
195
+ continue
196
+ yield f"{ MAGIC_REMOTE_URL } /{ relative } " , schema
197
+
198
+
190
199
@frozen (repr = False )
191
200
class _Test :
192
201
0 commit comments