meta: add tests for invalid python json/schema parsing

Based on the feedback from Tomáš in [0] this commit adds tests
that ensure consistent behavior between the python and the json
loader.

It's not 100% because the python is extremly leaniant and does
not even check if the required pieces of the json are there.
I.e. it will load a module without a SCHEMA or SCHEMA_2 variable
and the json loader code will warn about the issue but not
raise an error.

Fwiw, I have no strong opinion here but I do lean slightly towards
staying close to the original code (but both approaches of failing
with an exectption and continue with a warning have good arguments).

[0] https://github.com/osbuild/osbuild/pull/1618#discussion_r1521141148
This commit is contained in:
Michael Vogt 2024-03-14 10:11:35 +01:00 committed by Simon de Vlieger
parent 9af7c9b279
commit a7b4565445
2 changed files with 69 additions and 18 deletions

View file

@ -472,12 +472,20 @@ class ModuleInfo:
def _load_from_json(cls, path, klass, name) -> Optional["ModuleInfo"]:
meta_json_suffix = ".meta.json"
with open(path + meta_json_suffix, encoding="utf-8") as fp:
meta = json.load(fp)
try:
meta = json.load(fp)
except json.decoder.JSONDecodeError as e:
raise SyntaxError("Invalid schema: " + str(e)) from e
schema = Schema(META_JSON_SCHEMA, "meta.json validator")
res = schema.validate(meta)
if not res.valid:
# XXX: should we raise an exception instead?
# the python code is very leaniant with invalid schemas
# so just print a warning here for now to stay close to
# what the old code was doing
errs = res.as_dict()["errors"]
# it would be nice to have a proper logger here
print(f"WARNING: schema for {path} is invalid: {errs}", file=sys.stderr)
return None
long_description = meta.get("description", "no description provided")