Skip to content

Commit 787a798

Browse files
authored
Try to avoid emitting spurious warnings when there's a colon in the f… (#652)
* Try to avoid emitting spurious warnings when there's a colon in the filename. * Add tests for missing namespace warning * bonus fix, expand_url should use supported_schemes instead of hardcoded list
1 parent 8f9e407 commit 787a798

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

schema_salad/metaschema.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,10 @@ def expand_url(
317317
split = urlsplit(url)
318318

319319
if (
320-
(bool(split.scheme) and split.scheme in ["http", "https", "file"])
320+
(
321+
bool(split.scheme)
322+
and split.scheme in loadingOptions.fetcher.supported_schemes()
323+
)
321324
or url.startswith("$(")
322325
or url.startswith("${")
323326
):

schema_salad/python_codegen_support.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,10 @@ def expand_url(
314314
split = urlsplit(url)
315315

316316
if (
317-
(bool(split.scheme) and split.scheme in ["http", "https", "file"])
317+
(
318+
bool(split.scheme)
319+
and split.scheme in loadingOptions.fetcher.supported_schemes()
320+
)
318321
or url.startswith("$(")
319322
or url.startswith("${")
320323
):

schema_salad/ref_resolver.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,15 @@ def expand_url(
229229

230230
if bool(self.vocab) and ":" in url:
231231
prefix = url.split(":")[0]
232-
if prefix in self.vocab:
232+
if not prefix:
233+
pass
234+
elif prefix in self.vocab:
233235
url = self.vocab[prefix] + url[len(prefix) + 1 :]
234-
elif prefix not in self.fetcher.supported_schemes():
236+
elif (
237+
prefix not in self.fetcher.supported_schemes()
238+
and "/" not in prefix
239+
and "#" not in prefix
240+
):
235241
_logger.warning(
236242
"URI prefix '%s' of '%s' not recognized, are you missing a "
237243
"$namespaces section?",
@@ -242,7 +248,7 @@ def expand_url(
242248
split = urllib.parse.urlsplit(url)
243249

244250
if (
245-
(bool(split.scheme) and split.scheme in ["http", "https", "file"])
251+
(bool(split.scheme) and split.scheme in self.fetcher.supported_schemes())
246252
or url.startswith("$(")
247253
or url.startswith("${")
248254
):

schema_salad/tests/test_errors.py

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from schema_salad.sourceline import cmap
1313

1414
from .util import get_data
15+
from .test_cli_args import captured_output
1516

1617

1718
def test_errors() -> None:
@@ -77,8 +78,7 @@ def test_error_message2() -> None:
7778

7879
t = "test_schema/test2.cwl"
7980
match = r"""
80-
^.+test2\.cwl:2:1: Field `class`\s+contains\s+undefined\s+reference\s+to
81-
\s+`file://.+/schema_salad/tests/test_schema/xWorkflow`$"""[
81+
^.+test2\.cwl:2:1: Field `class`\s+contains\s+undefined\s+reference\s+to\s+`file://.+/schema_salad/tests/test_schema/xWorkflow`$"""[
8282
1:
8383
]
8484
path2 = get_data("tests/" + t)
@@ -182,8 +182,7 @@ def test_error_message8() -> None:
182182
match = r"""
183183
^.+test8\.cwl:7:1: checking field\s+`steps`
184184
.+test8\.cwl:8:3: checking object\s+`.+test8\.cwl#step1`
185-
.+test8\.cwl:9:5: Field\s+`scatterMethod`\s+contains\s+undefined\s+reference\s+to
186-
\s+`file:///.+/tests/test_schema/abc`$"""[
185+
.+test8\.cwl:9:5: Field\s+`scatterMethod`\s+contains\s+undefined\s+reference\s+to\s+`file:///.+/tests/test_schema/abc`$"""[
187186
1:
188187
]
189188
with pytest.raises(ValidationException, match=match):
@@ -245,8 +244,7 @@ def test_error_message11() -> None:
245244
match = r"""
246245
^.+test11\.cwl:7:1: checking field\s+`steps`
247246
.+test11\.cwl:8:3: checking object\s+`.+test11\.cwl#step1`
248-
.+test11\.cwl:9:5: Field `run`\s+contains\s+undefined\s+reference\s+to
249-
\s+`file://.+/tests/test_schema/blub\.cwl`$"""[
247+
.+test11\.cwl:9:5: Field `run`\s+contains\s+undefined\s+reference\s+to\s+`file://.+/tests/test_schema/blub\.cwl`$"""[
250248
1:
251249
]
252250
with pytest.raises(ValidationException, match=match):
@@ -348,6 +346,74 @@ def test_namespaces_type() -> None:
348346
)
349347

350348

349+
def test_namespaces_undeclared(caplog: pytest.LogCaptureFixture) -> None:
350+
"""Confirm warning message a namespace is used but not declared."""
351+
352+
ldr, _, _, _ = schema_salad.schema.load_schema(
353+
cmap(
354+
{
355+
"$base": "Y",
356+
"name": "X",
357+
"$graph": [
358+
{
359+
"name": "namesp:ExampleType",
360+
"type": "enum",
361+
"symbols": ["asym", "bsym"],
362+
}
363+
],
364+
}
365+
)
366+
)
367+
368+
match = r""".*URI prefix 'namesp' of 'namesp:ExampleType' not recognized, are you missing a \$namespaces section?.*"""
369+
370+
assert re.match(match, caplog.text)
371+
372+
373+
def test_not_a_namespace1(caplog: pytest.LogCaptureFixture) -> None:
374+
"""Confirm warning message a namespace is used but not declared."""
375+
376+
ldr, _, _, _ = schema_salad.schema.load_schema(
377+
cmap(
378+
{
379+
"$base": "Y",
380+
"name": "X",
381+
"$graph": [
382+
{
383+
"name": "foo/colon:ExampleType",
384+
"type": "enum",
385+
"symbols": ["asym", "bsym"],
386+
}
387+
],
388+
}
389+
)
390+
)
391+
392+
assert caplog.text == ""
393+
394+
395+
def test_not_a_namespace2(caplog: pytest.LogCaptureFixture) -> None:
396+
"""Confirm warning message a namespace is used but not declared."""
397+
398+
ldr, _, _, _ = schema_salad.schema.load_schema(
399+
cmap(
400+
{
401+
"$base": "Y",
402+
"name": "X",
403+
"$graph": [
404+
{
405+
"name": "foo#colon:ExampleType",
406+
"type": "enum",
407+
"symbols": ["asym", "bsym"],
408+
}
409+
],
410+
}
411+
)
412+
)
413+
414+
assert caplog.text == ""
415+
416+
351417
def test_schemas_type() -> None:
352418
"""Confirm helpful error message when $schemas is the wrong type."""
353419
path = get_data("tests/EDAM.owl")

0 commit comments

Comments
 (0)