Files
scst/scripts/tests/test_checkpatch.py
T
Gleb Chesnokov 704c44dabe scripts: Add whole-file Checkpatch analysis
Expose make checkpatch through the common C source/header policy,
including userspace and KUnit sources. DIFFBR selects whole changed
files. Use the kernel's Checkpatch and companion data with --strict,
without adding diagnostic exceptions or loading local configuration.

Retain raw output and require native totals and a completion record
for every selected file. Distinguish source findings from unreadable
inputs, inconsistent reports and tool failures, with isolated fixtures.
Keep native caches in a temporary directory and serialize only this
analyzer's reports so it can run alongside the Kbuild analyzers.

Make checkpatch_scan use this target's implementation and exit status
instead of suppressing errors. Preserve the separate existing policy
of patch/commit helpers and the kernel regression runner.
2026-10-20 14:16:08 +03:00

59 lines
2.8 KiB
Python

"""Checkpatch protocol fixtures without a kernel build or source rewrites."""
from pathlib import Path
import sys
import unittest
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from style.checkpatch import native_report
class CheckpatchTests(unittest.TestCase):
def setUp(self):
self.first = Path("/tmp/scst/first.c")
self.second = Path("/tmp/scst/second.c")
self.clean = ("total: 0 errors, 0 warnings, 0 checks, 1 lines checked\n"
f"{self.first} has no obvious style problems and is ready for submission.\n")
self.warning = ("WARNING:SPDX_LICENSE_TAG: Missing SPDX tag\n"
"total: 0 errors, 1 warnings, 0 checks, 1 lines checked\n"
f"{self.first} has style problems, please review.\n")
def test_completed_clean_file(self):
report = native_report(self.clean, "", 0, [self.first])
self.assertTrue(report["complete"])
self.assertEqual(report["remaining"], 0)
def test_source_warning_is_retained(self):
report = native_report(self.warning, "", 1, [self.first])
self.assertTrue(report["complete"])
self.assertEqual(report["remaining"], 1)
self.assertEqual(report["counts"], {"WARNING": 1})
def test_unreadable_second_file_cannot_hide_behind_first_file(self):
for status, text in ((0, self.clean), (1, self.warning)):
with self.subTest(status=status):
report = native_report(text, "diff: second.c: Permission denied", status,
[self.first, self.second])
self.assertFalse(report["complete"])
self.assertTrue(any("native stderr" in item for item in report["failures"]))
self.assertTrue(any("completion" in item for item in report["failures"]))
def test_missing_duplicate_and_unknown_completions_fail(self):
for text, files in ((self.clean, [self.first, self.second]),
(self.clean + self.clean, [self.first, self.second]),
(self.clean, [self.second]), ("", [self.first])):
with self.subTest(text=text, files=files):
self.assertFalse(native_report(text, "", 0, files)["complete"])
def test_native_failure_and_unaccounted_diagnostics_fail(self):
for text, stderr, status in ((self.clean, "Perl failed", 0), (self.clean, "", 2),
(self.warning, "", 0), (self.clean, "", 1),
(self.warning.replace("1 warnings", "0 warnings"), "", 1),
(self.warning.split("\n", 1)[1], "", 1)):
with self.subTest(text=text, stderr=stderr, status=status):
self.assertFalse(native_report(text, stderr, status, [self.first])["complete"])
if __name__ == "__main__":
unittest.main()