[PATCH v2 4/4] docs: kernel-doc: import sys for YAML failure diagnostics

From: 闲逛

Date: Sat Nov 14 2026 - 04:07:18 EST


From: yuehua-meng <2368492046@xxxxxx>

Missing PyYAML and nonexistent output directories reach sys.exit()
without importing sys. This raises NameError instead of reporting
the intended diagnostic.

Import sys and add CLI tests for both failures and successful YAML
generation. The missing-dependency test disables site packages.

Fixes: b37b3cbbb1f1 ("docs: add a new file to write kernel-doc output to a YAML file")

Signed-off-by: yuehua-meng <2368492046@xxxxxx>
---
Changes in v2:
Rebase onto docs-next; no functional change from the signed v1.

tools/lib/python/kdoc/kdoc_yaml_file.py | 1 +
tools/unittests/test_kdoc_yaml.py | 37 +++++++++++++++++++++++++
2 files changed, 38 insertions(+)
create mode 100644 tools/unittests/test_kdoc_yaml.py

diff --git a/tools/lib/python/kdoc/kdoc_yaml_file.py b/tools/lib/python/kdoc/kdoc_yaml_file.py
index 0be020d50..eb5e51c36 100644
--- a/tools/lib/python/kdoc/kdoc_yaml_file.py
+++ b/tools/lib/python/kdoc/kdoc_yaml_file.py
@@ -3,6 +3,7 @@
# Copyright(c) 2026: Mauro Carvalho Chehab <mchehab@xxxxxxxxxx>.

import os
+import sys

from kdoc.kdoc_output import ManFormat, RestFormat

diff --git a/tools/unittests/test_kdoc_yaml.py b/tools/unittests/test_kdoc_yaml.py
new file mode 100644
index 000000000..e38ddd131
--- /dev/null
+++ b/tools/unittests/test_kdoc_yaml.py
@@ -0,0 +1,37 @@
+# SPDX-License-Identifier: GPL-2.0
+"""Regression tests for kernel-doc YAML generation and error handling."""
+
+import yaml
+
+from kdoc_cli_helper import KernelDocCliTest
+
+
+class TestKernelDocYaml(KernelDocCliTest):
+ def setUp(self):
+ super().setUp()
+ self.input = self.source("example.c", (
+ "/**\n * example - documented function\n */\n"
+ "void example(void) {}\n"
+ ))
+
+ def test_missing_dependency(self):
+ result = self.run_kdoc("--yaml", self.directory / "test.yaml",
+ self.input, no_site=True)
+ self.assertEqual(result.returncode, 1)
+ self.assertIn("yaml package not available", result.stderr)
+ self.assertNotIn("Traceback", result.stderr)
+
+ def test_missing_directory(self):
+ result = self.run_kdoc("--yaml", self.directory / "missing/test.yaml",
+ self.input)
+ self.assertEqual(result.returncode, 1)
+ self.assertIn("doesn't exist", result.stderr)
+ self.assertNotIn("Traceback", result.stderr)
+
+ def test_valid_output(self):
+ output = self.directory / "test.yaml"
+ result = self.run_kdoc("--yaml", output, self.input)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ data = yaml.safe_load(output.read_text(encoding="utf-8"))
+ self.assertEqual(len(data["tests"]), 1)
+ self.assertEqual(data["tests"][0]["name"], "example")
--
2.53.0.windows.2