Skip to content

Commit

Permalink
Fix output URLs in validation report
Browse files Browse the repository at this point in the history
  • Loading branch information
avillar committed Oct 11, 2023
1 parent beb7305 commit 09b3731
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ogc/bblocks/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
test_outputs_path=args.test_outputs_path,
github_base_url=args.github_base_url,
imported_registers=imported_registers,
filter=args.filter)
bb_filter=args.filter)

# 2. Uplift register.json
print(f"Running semantic uplift of {register_file}", file=sys.stderr)
Expand Down
12 changes: 6 additions & 6 deletions ogc/bblocks/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def postprocess(registered_items_path: str | Path = 'registereditems',
test_outputs_path: str | Path = 'build/tests',
github_base_url: str | None = None,
imported_registers: list[str] | None = None,
filter: str | None = None) -> list[BuildingBlock]:
bb_filter: str | None = None) -> list[BuildingBlock]:

cwd = Path().resolve()

Expand Down Expand Up @@ -120,9 +120,9 @@ def do_postprocess(bblock: BuildingBlock, light: bool = False) -> bool:
if not light:
print(f" > Running tests for {bblock.identifier}", file=sys.stderr)
validation_passed, test_count = validate_test_resources(bblock,
registered_items_path=registered_items_path,
bblocks_register=bbr,
outputs_path=test_outputs_path)
outputs_path=test_outputs_path,
base_url=base_url)
bblock.metadata['validationPassed'] = validation_passed
if not validation_passed:
bblock.metadata['status'] = 'invalid'
Expand Down Expand Up @@ -158,9 +158,9 @@ def do_postprocess(bblock: BuildingBlock, light: bool = False) -> bool:
registered_items_path = Path(registered_items_path)

filter_id = None
if filter:
if bb_filter:
filter_id = False
filter_p = Path(filter)
filter_p = Path(bb_filter)
if filter_p.exists():
# Find closest bblocks.json
for p in itertools.chain((filter_p,), filter_p.parents):
Expand All @@ -172,7 +172,7 @@ def do_postprocess(bblock: BuildingBlock, light: bool = False) -> bool:
if filter_id:
break
else:
filter_id = filter
filter_id = bb_filter

if transformers.transform_modules:
print("Available transformers:", file=sys.stderr)
Expand Down
39 changes: 29 additions & 10 deletions ogc/bblocks/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pathlib import Path
from time import time
from typing import Any, Sequence
from urllib.parse import urlsplit
from urllib.parse import urlsplit, urljoin
from urllib.request import urlopen
from datetime import datetime, timezone

Expand Down Expand Up @@ -55,7 +55,7 @@ class ValidationReportSection(Enum):
@dataclasses.dataclass
class ValidationItemSource:
type: ValidationItemSourceType
filename: str | Path | None = None
filename: Path | None = None
example_index: int | None = None
snippet_index: int | None = None
language: str | None = None
Expand Down Expand Up @@ -123,7 +123,10 @@ def uplifted_files(self) -> dict[str, tuple[Path, str]]:
return self._uplifted_files


def _report_to_json(bblock: BuildingBlock, items: Sequence[ValidationReportItem], report_fn: Path | None) -> str | None:
def _report_to_json(bblock: BuildingBlock,
items: Sequence[ValidationReportItem],
report_fn: Path | None,
base_url: str | None = None) -> str | None:
result = {
'title': f"Validation report for {bblock.identifier} - {bblock.name}",
'bblockId': bblock.identifier,
Expand All @@ -132,14 +135,17 @@ def _report_to_json(bblock: BuildingBlock, items: Sequence[ValidationReportItem]
}

global_errors = {}
cwd = Path().resolve()

for item in items:
source = {
'type': item.source.type.name,
'require_fail': item.source.require_fail,
}
if item.source.filename:
source['filename'] = str(item.source.filename)
source['filename'] = str(os.path.relpath(item.source.filename, cwd))
if base_url:
source['filename'] = urljoin(base_url, source['filename'])
if item.source.example_index:
source['exampleIndex'] = item.source.example_index
if item.source.snippet_index:
Expand All @@ -154,8 +160,20 @@ def _report_to_json(bblock: BuildingBlock, items: Sequence[ValidationReportItem]
entry_dict = {}
if entry.payload:
for k, v in entry.payload.items():
entry_dict[k] = str(k) if isinstance(k, Path) else k
entry_dict.update(entry.payload)
if isinstance(v, Path):
v = str(os.path.relpath(v.resolve(), cwd))
if base_url:
v = urljoin(base_url, v)
elif k == 'files' and isinstance(v, list):
fv = []
for f in v:
if isinstance(f, Path):
f = str(os.path.relpath(f.resolve(), cwd))
if base_url:
f = urljoin(base_url, f)
fv.append(f)
v = fv
entry_dict[k] = v
entry_dict['is_error'] = entry.is_error
entry_dict['message'] = entry.message
if not entry.is_global:
Expand Down Expand Up @@ -542,9 +560,9 @@ def validate_inner():


def validate_test_resources(bblock: BuildingBlock,
registered_items_path: Path,
bblocks_register: BuildingBlockRegister,
outputs_path: str | Path | None = None) -> tuple[bool, int]:
outputs_path: str | Path | None = None,
base_url: str | None = None) -> tuple[bool, int]:
final_result = True
test_count = 0

Expand All @@ -553,14 +571,15 @@ def validate_test_resources(bblock: BuildingBlock,

shacl_graph = Graph()
shacl_error = None
cwd = Path().resolve()

shacl_files = []
try:
for shacl_file in bblocks_register.get_inherited_shacl_rules(bblock.identifier):
if isinstance(shacl_file, Path) or (isinstance(shacl_file, str) and not is_url(shacl_file)):
# assume file
shacl_file = bblock.files_path / shacl_file
shacl_files.append(os.path.relpath(shacl_file, registered_items_path))
shacl_files.append(os.path.relpath(shacl_file))
else:
shacl_files.append(shacl_file)
shacl_graph.parse(shacl_file, format='turtle')
Expand Down Expand Up @@ -694,7 +713,7 @@ def validate_test_resources(bblock: BuildingBlock,
})

if all_results:
_report_to_json(bblock, all_results, output_dir / '_report.json')
_report_to_json(bblock, all_results, output_dir / '_report.json', base_url)

return final_result, test_count

Expand Down

0 comments on commit 09b3731

Please sign in to comment.