Path: blob/master/tools/unittests/test_kdoc_test_schema.py
170831 views
#!/usr/bin/env python31# SPDX-License-Identifier: GPL-2.02"""3Unit‑test driver for kernel‑doc YAML tests.45Two kinds of tests are defined:67* **Schema‑validation tests** – if ``jsonschema`` is available, the8YAML files in this directory are validated against the JSON‑Schema9described in ``kdoc-test-schema.yaml``. When the library is not10present, a warning is emitted and the validation step is simply11skipped – the dynamic kernel‑doc tests still run.1213* **Kernel‑doc tests** – dynamically generate one test method per14scenario in ``kdoc-test.yaml``. Each method simply forwards15the data to ``self.run_test`` – you only need to implement that16helper in your own code.1718File names are kept as module‑level constants so that the19implementation stays completely independent of ``pathlib``.20"""2122import os23import sys24import warnings25import yaml26import unittest27from typing import Any, Dict, List2829SRC_DIR = os.path.dirname(os.path.realpath(__file__))30sys.path.insert(0, os.path.join(SRC_DIR, "../lib/python"))3132from unittest_helper import run_unittest333435#36# Files to read37#38BASE = os.path.realpath(os.path.dirname(__file__))3940SCHEMA_FILE = os.path.join(BASE, "kdoc-test-schema.yaml")41TEST_FILE = os.path.join(BASE, "kdoc-test.yaml")4243#44# Schema‑validation test45#46class TestYAMLSchemaValidation(unittest.TestCase):47"""48Checks if TEST_FILE matches SCHEMA_FILE.49"""5051@classmethod52def setUpClass(cls):53"""54Import jsonschema if available.55"""5657try:58from jsonschema import Draft7Validator59except ImportError:60print("Warning: jsonschema package not available. Skipping schema validation")61cls.validator = None62return6364with open(SCHEMA_FILE, encoding="utf-8") as fp:65cls.schema = yaml.safe_load(fp)6667cls.validator = Draft7Validator(cls.schema)6869def test_kdoc_test_yaml_followsschema(self):70"""71Run jsonschema validation if the validator is available.72If not, emit a warning and return without failing.73"""74if self.validator is None:75return7677with open(TEST_FILE, encoding="utf-8") as fp:78data = yaml.safe_load(fp)7980errors = self.validator.iter_errors(data)8182msgs = []83for error in errors:84msgs.append(error.message)8586if msgs:87self.fail("Schema validation failed:\n\t" + "\n\t".join(msgs))8889# --------------------------------------------------------------------90# Entry point91# --------------------------------------------------------------------92if __name__ == "__main__":93run_unittest(__file__)949596