Skip to contentSkip to conversation

docs: kernel-doc: import sys for YAML failure diagnostics

PATCH In review 闲逛 1 participant 1 message Nov 14

Subsystems (2)
lore
Share & tools
lore

闲逛

author
#0 lore raw

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

From: yuehua-meng <[email protected]>

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")

---
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
tools/lib/python/kdoc/kdoc_yaml_file.py +1 -0
index 0be020d50..eb5e51c36 100644
@@ -3,6 +3,7 @@
 # Copyright(c) 2026: Mauro Carvalho Chehab <[email protected]>.
 
 import os
+import sys
 
 from kdoc.kdoc_output import ManFormat, RestFormat
 
tools/unittests/test_kdoc_yaml.py new file +37 -0
new file mode 100644
index 000000000..e38ddd131
@@ -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")
Signature · 1 line
2.53.0.windows.2