From 5a5f1e665ee2d2316699327e87cdf92d99f9a779 Mon Sep 17 00:00:00 2001 From: John Pennycook Date: Tue, 23 Jan 2024 07:07:50 -0800 Subject: [PATCH 1/3] Fix incorrect URL in coverage schema Signed-off-by: John Pennycook --- p3/data/coverage-0.1.0.schema | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p3/data/coverage-0.1.0.schema b/p3/data/coverage-0.1.0.schema index 4d96910..6eccb1e 100644 --- a/p3/data/coverage-0.1.0.schema +++ b/p3/data/coverage-0.1.0.schema @@ -1,6 +1,6 @@ { "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://raw.githubusercontent.com/intel/p3-analysis-library/p3/schema/coverage-0.1.0.schema", + "$id": "https://raw.githubusercontent.com/intel/p3-analysis-library/main/p3/data/coverage-0.1.0.schema", "title": "Coverage", "description": "Lines of code used in each file of a code base.", "type": "array", From 71ec07cce5d66275c4f0473c6ae433f691f21feb Mon Sep 17 00:00:00 2001 From: John Pennycook Date: Tue, 23 Jan 2024 07:22:01 -0800 Subject: [PATCH 2/3] Load schema from file instead of using an object This simplifies the code by removing duplication, and will ensure that the coverage schema file will always match the schema being used for validation. Signed-off-by: John Pennycook --- p3/data/_validation.py | 41 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 32 deletions(-) diff --git a/p3/data/_validation.py b/p3/data/_validation.py index f9521fd..d8a1f5a 100644 --- a/p3/data/_validation.py +++ b/p3/data/_validation.py @@ -3,37 +3,7 @@ import json import jsonschema - -_coverage_schema_id = ( - "https://raw.githubusercontent.com/intel/" - "p3-analysis-library/p3/schema/coverage-0.1.0.schema" -) -_coverage_schema = { - "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": _coverage_schema_id, - "title": "Coverage", - "description": "Lines of code used in each file of a code base.", - "type": "array", - "items": { - "type": "object", - "properties": { - "file": {"type": "string"}, - "regions": { - "type": "array", - "items": { - "type": "array", - "prefixItems": [ - {"type": "integer"}, - {"type": "integer"}, - {"type": "integer"}, - ], - "items": False, - }, - }, - }, - "required": ["file", "regions"], - }, -} +import pkgutil def _validate_coverage_json(json_string: str) -> object: @@ -63,8 +33,15 @@ def _validate_coverage_json(json_string: str) -> object: instance = json.loads(json_string) + schema_string = pkgutil.get_data(__name__, "coverage-0.1.0.schema") + if not schema_string: + msg = "Could not locate coverage schema file" + raise RuntimeError(msg) + + schema = json.loads(schema_string) + try: - jsonschema.validate(instance=instance, schema=_coverage_schema) + jsonschema.validate(instance=instance, schema=schema) except Exception: msg = "Coverage string failed schema validation" raise ValueError(msg) From 276f6ee4582d83435504c8bc11649bbf83dfe093 Mon Sep 17 00:00:00 2001 From: John Pennycook Date: Tue, 23 Jan 2024 14:02:28 -0800 Subject: [PATCH 3/3] Explicitly specify expected exception types - ValidationError if the instance is invalid - SchemaError if the schema is invalid Signed-off-by: John Pennycook --- p3/data/_validation.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/p3/data/_validation.py b/p3/data/_validation.py index d8a1f5a..2e0a35a 100644 --- a/p3/data/_validation.py +++ b/p3/data/_validation.py @@ -42,8 +42,11 @@ def _validate_coverage_json(json_string: str) -> object: try: jsonschema.validate(instance=instance, schema=schema) - except Exception: + except jsonschema.exceptions.ValidationError: msg = "Coverage string failed schema validation" raise ValueError(msg) + except jsonschema.exceptions.SchemaError: + msg = "coverage-0.1.0.schema is not a valid schema" + raise RuntimeError(msg) return instance