1010import json
1111import os
1212import re
13- import subprocess
1413import sys
1514import unittest
1615
2120if TYPE_CHECKING :
2221 from collections .abc import Iterable , Mapping , Sequence
2322
23+ from referencing .jsonschema import Schema
2424 import pyperf
2525
2626from jsonschema .validators import _VALIDATORS
2727import jsonschema
2828
29+ MAGIC_REMOTE_URL = "http://localhost:1234"
30+
2931_DELIMITERS = re .compile (r"[\W\- ]+" )
3032
3133
@@ -51,38 +53,7 @@ def _find_suite():
5153class Suite :
5254
5355 _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 )
6256
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 )
8657
8758 def benchmark (self , runner : pyperf .Runner ): # pragma: no cover
8859 for name , Validator in _VALIDATORS .items ():
@@ -92,10 +63,18 @@ def benchmark(self, runner: pyperf.Runner): # pragma: no cover
9263 )
9364
9465 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+ )
9574 return Version (
9675 name = name ,
9776 path = self ._root / "tests" / name ,
98- remotes = self . _remotes ,
77+ remotes = registry ,
9978 )
10079
10180
@@ -187,6 +166,36 @@ def benchmark(self, runner: pyperf.Runner, **kwargs): # pragma: no cover
187166 )
188167
189168
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+
190199@frozen (repr = False )
191200class _Test :
192201
0 commit comments