From 27e67372b298a4db69e3f4f4daf15bf66b70a091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 26 Dec 2020 18:01:29 +0200 Subject: tests/test_sub_class_ofs.py: new sub-class-of sanity check test --- tests/meson.build | 5 +++++ tests/test_sub_class_ofs.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100755 tests/test_sub_class_ofs.py diff --git a/tests/meson.build b/tests/meson.build index 3b01da17..7ee6559c 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -37,6 +37,11 @@ test('No duplicate mime-types', args: freedesktop_org_xml, ) +test('Sanity check sub-class-of', + find_program('test_sub_class_ofs.py'), + args: freedesktop_org_xml, +) + test('xmllint freedesktop.org.xml', xmllint, args: [ '--noout', '--valid', freedesktop_org_xml, ], diff --git a/tests/test_sub_class_ofs.py b/tests/test_sub_class_ofs.py new file mode 100755 index 00000000..2a48aec1 --- /dev/null +++ b/tests/test_sub_class_ofs.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +import sys +import xml.etree.ElementTree as ET + +FDO_XMLNS = "{http://www.freedesktop.org/standards/shared-mime-info}" + + +def test_sub_class_ofs(fdo_xml: str) -> int: + + with open(fdo_xml, "rb") as f: + tree = ET.parse(f) + + errors = 0 + + mime_types = set() + sub_class_ofs = set() + for elem in tree.findall(f"{FDO_XMLNS}mime-type"): + mime_type = elem.attrib["type"] + mime_types.add(mime_type) + for elem in elem.findall(f"{FDO_XMLNS}sub-class-of"): + sub_class_of = elem.attrib["type"] + + if mime_type == sub_class_of: + print(f"{fdo_xml}: type {mime_type} is sub-class-of itself", + file=sys.stderr) + errors += 1 + + sub_class_ofs.add(sub_class_of) + + assert mime_types and sub_class_ofs + + for not_found in sub_class_ofs - mime_types: + print(f"{fdo_xml}: parent sub-class-of type {not_found} not found", + file=sys.stderr) + errors += 1 + + return errors + + +if __name__ == "__main__": + if len(sys.argv) != 2: + print(f"Usage: {__file__} FREEDESKTOP_ORG_XML", file=sys.stderr) + sys.exit(2) + + sys.exit(test_sub_class_ofs(sys.argv[1]) and 1 or 0) -- cgit v1.2.1