Skip to content

Commit e6f4a03

Browse files
committed
Cope with the suite permuting of remotes.
This new-but-similarly-messy logic comes modified from Bowtie (which itself may need to be modified there, we'll see). `jsonschema_suite remotes` is somewhat semantically broken (as it doesn't really help with not emitting schemas which aren't valid under the version under test), so it's time to give up and use the other filesystem mechanism of walking the remotes directory. Refs: json-schema-org/JSON-Schema-Test-Suite#753
1 parent bdee92d commit e6f4a03

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

jsonschema/tests/_suite.py

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import json
1111
import os
1212
import re
13-
import subprocess
1413
import sys
1514
import unittest
1615

@@ -21,11 +20,14 @@
2120
if TYPE_CHECKING:
2221
from collections.abc import Iterable, Mapping, Sequence
2322

23+
from referencing.jsonschema import Schema
2424
import pyperf
2525

2626
from jsonschema.validators import _VALIDATORS
2727
import 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():
5153
class 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)
191200
class _Test:
192201

0 commit comments

Comments
 (0)