docs: add a simple kdoc-test.yaml together with a validation tool

Create a simple kdoc-test.yaml to be used to create unit tests for
kernel-doc parser and output classes.

For now, all we want is a simple function mapped on a yaml test
using the defined schema.

To be sure that the schema is followed, add an unittest for
the file, which will also validate that the schema is properly
parsed.

It should be noticed that the .TH definition for the man format
contains a timestamp. We'll need to handle that when dealing with
the actual implementation for the ManOutput class unit tests.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Message-ID: <74883976348c964f00161696d525c33ddd8c7641.1773823995.git.mchehab+huawei@kernel.org>
This commit is contained in:
Mauro Carvalho Chehab 2026-03-18 10:11:11 +01:00 committed by Jonathan Corbet
parent f1cdbd824c
commit 8b69f5225a
2 changed files with 248 additions and 0 deletions

View File

@ -0,0 +1,154 @@
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2026: Mauro Carvalho Chehab <mchehab@kernel.org>
# Test cases for the dynamic tests.
# Useful to test if kernel-doc classes are doing what it is expected.
#
tests:
- name: func1
fname: mock_functions.c
description: "Simplest function test: do nothing, just rst output"
source: |
/**
* func1 - Not exported function
* @arg1: @arg1 does nothing
*
* Does nothing
*
* return:
* always return 0.
*/
int func1(char *arg1) { return 0; };
expected:
- rst: |
.. c:function:: int func1 (char *arg1)
Not exported function
.. container:: kernelindent
**Parameters**
``char *arg1``
**arg1** does nothing
**Description**
Does nothing
**Return**
always return 0.
# TODO: how to handle timestamps at .TH?
man: |
.TH "func1" 9 "February 2026" "" "Kernel API Manual"
.SH NAME
func1 \- Not exported function
.SH SYNOPSIS
.B "int" func1
.BI "(char *arg1 " ");"
.SH ARGUMENTS
.IP "arg1" 12
\fIarg1\fP does nothing
.SH "DESCRIPTION"
Does nothing
.SH "RETURN"
always return 0.
.SH "SEE ALSO"
.PP
Kernel file \fBmock_functions.c\fR
- name: func2
fname: func2.c
description: Simple test with exports
source: |
/**
* func2() - Exported function
* @arg1: @arg1 does nothing
*
* Does nothing
*
* return:
* always return 0.
*/
int func2(char *arg1) { return 0; };
EXPORT_SYMBOL(func2);
exports: func2
expected:
- kdoc_item:
name: func2
type: function
declaration_start_line: 1
sections:
Description: |
Does nothing
Return: |
always return 0.
sections_start_lines:
Description: 3
Return: 6
parameterdescs:
arg1: |
@arg1 does nothing
parameterlist:
- arg1
parameterdesc_start_lines:
arg1: 2
parametertypes:
arg1: char *arg1
other_stuff:
func_macro: false
functiontype: int
purpose: "Exported function"
typedef: false
rst: |
.. c:function:: int func2 (char *arg1)
Exported function
.. container:: kernelindent
**Parameters**
``char *arg1``
**arg1** does nothing
**Description**
Does nothing
**Return**
always return 0.
# TODO: how to handle timestamps at .TH?
man: |
.TH "func2" 9 "February 2026" "" "Kernel API Manual"
.SH NAME
func2 \- Exported function
.SH SYNOPSIS
.B "int" func2
.BI "(char *arg1 " ");"
.SH ARGUMENTS
.IP "arg1" 12
\fIarg1\fP does nothing
.SH "DESCRIPTION"
Does nothing
.SH "RETURN"
always return 0.
.SH "SEE ALSO"
.PP
Kernel file \fBfunc2.c\fR

View File

@ -0,0 +1,94 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0
"""
Unittest driver for kerneldoc YAML tests.
Two kinds of tests are defined:
* **Schemavalidation tests** if ``jsonschema`` is available, the
YAML files in this directory are validated against the JSONSchema
described in ``kdoc-test-schema.yaml``. When the library is not
present, a warning is emitted and the validation step is simply
skipped the dynamic kerneldoc tests still run.
* **Kerneldoc tests** dynamically generate one test method per
scenario in ``kdoc-test.yaml``. Each method simply forwards
the data to ``self.run_test`` you only need to implement that
helper in your own code.
File names are kept as modulelevel constants so that the
implementation stays completely independent of ``pathlib``.
"""
import os
import sys
import warnings
import yaml
import unittest
from typing import Any, Dict, List
SRC_DIR = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.join(SRC_DIR, "../lib/python"))
from unittest_helper import run_unittest
#
# Files to read
#
BASE = os.path.realpath(os.path.dirname(__file__))
SCHEMA_FILE = os.path.join(BASE, "kdoc-test-schema.yaml")
TEST_FILE = os.path.join(BASE, "kdoc-test.yaml")
#
# Schemavalidation test
#
class TestYAMLSchemaValidation(unittest.TestCase):
"""
Checks if TEST_FILE matches SCHEMA_FILE.
"""
@classmethod
def setUpClass(cls):
"""
Import jsonschema if available.
"""
try:
from jsonschema import Draft7Validator
except ImportError:
print("Warning: jsonschema package not available. Skipping schema validation")
cls.validator = None
return
with open(SCHEMA_FILE, encoding="utf-8") as fp:
cls.schema = yaml.safe_load(fp)
cls.validator = Draft7Validator(cls.schema)
def test_kdoc_test_yaml_followsschema(self):
"""
Run jsonschema validation if the validator is available.
If not, emit a warning and return without failing.
"""
if self.validator is None:
return
with open(TEST_FILE, encoding="utf-8") as fp:
data = yaml.safe_load(fp)
errors = self.validator.iter_errors(data)
msgs = []
for error in errors:
msgs.append(error.message)
if msgs:
self.fail("Schema validation failed:\n\t" + "\n\t".join(msgs))
# --------------------------------------------------------------------
# Entry point
# --------------------------------------------------------------------
if __name__ == "__main__":
run_unittest(__file__)