[clang-tools-extra] [clang-tidy] Implement alphabetical order test (PR #166072)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 9 01:29:44 PST 2025
================
@@ -0,0 +1,283 @@
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+# To run these tests:
+# python3 check-alphabetical-order_test.py -v
+
+import io
+import os
+import tempfile
+import unittest
+from contextlib import redirect_stderr
+import importlib.util
+import importlib.machinery
+from typing import Any, cast
+
+
+def _load_script_module():
+ here = os.path.dirname(cast(str, __file__))
+ script_path = os.path.normpath(os.path.join(here, "check-alphabetical-order.py"))
+ loader = importlib.machinery.SourceFileLoader(
+ "check_alphabetical_order", cast(str, script_path)
+ )
+ spec = importlib.util.spec_from_loader(loader.name, loader)
+ if spec is None or spec.loader is None:
+ raise ImportError(f"Failed to load spec for {script_path}")
+ mod = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(mod)
+ return mod
+
+
+_mod = cast(Any, _load_script_module())
+
+
+class TestAlphabeticalOrderCheck(unittest.TestCase):
+ def test_normalize_list_rst_sorts_rows(self):
+ lines = [
+ "Header\n",
+ "------" "\n",
+ ".. csv-table:: Clang-Tidy checks\n",
+ ' :header: "Name", "Offers fixes"\n',
+ "\n",
+ ' :doc:`bugprone-virtual-near-miss <bugprone/virtual-near-miss>`, "Yes"\n',
+ " :doc:`cert-flp30-c <cert/flp30-c>`,\n",
+ ' :doc:`abseil-cleanup-ctad <abseil/cleanup-ctad>`, "Yes"\n',
+ " A non-doc row that should stay after docs\n",
+ "\n",
+ "Footer\n",
+ ]
+
+ out = _mod.normalize_list_rst(lines)
+ pos_abseil = out.find("abseil-cleanup-ctad")
+ pos_bugprone = out.find("bugprone-virtual-near-miss")
+ pos_cert = out.find("cert-flp30-c")
+ self.assertTrue(all(p != -1 for p in [pos_abseil, pos_bugprone, pos_cert]))
+ self.assertLess(pos_abseil, pos_bugprone)
+ self.assertLess(pos_bugprone, pos_cert)
+ # Non-doc row should remain after doc rows within the table region.
+ self.assertGreater(out.find("A non-doc row"), out.find("cert-flp30-c"))
+
+ def test_find_heading(self):
+ lines = [
+ "- Deprecated the :program:`clang-tidy` ``zircon`` module. All checks have been\n",
+ " moved to the ``fuchsia`` module instead. The ``zircon`` module will be removed\n",
+ " in the 24th release.\n",
+ "\n",
+ "New checks\n",
+ "^^^^^^^^^^\n",
+ "- New :doc:`bugprone-derived-method-shadowing-base-method\n",
+ " <clang-tidy/checks/bugprone/derived-method-shadowing-base-method>` check.\n",
+ ]
+ idx = _mod.find_heading(lines, "New checks")
+ self.assertEqual(idx, 4)
+
+ def test_duplicate_detection_and_report(self):
+ # Ensure duplicate detection works properly when sorting is incorrect.
+ lines = [
+ "Changes in existing checks\n",
+ "^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
+ "\n",
+ "- Improved :doc:`bugprone-easily-swappable-parameters\n",
+ " <clang-tidy/checks/bugprone/easily-swappable-parameters>` check by\n",
+ " correcting a spelling mistake on its option\n",
+ " ``NamePrefixSuffixSilenceDissimilarityTreshold``.\n",
+ "\n",
+ "- Improved :doc:`bugprone-exception-escape\n",
+ " <clang-tidy/checks/bugprone/exception-escape>` check's handling of lambdas:\n",
+ " exceptions from captures are now diagnosed, exceptions in the bodies of\n",
+ " lambdas that aren't actually invoked are not.\n",
+ "\n",
+ "- Improved :doc:`bugprone-easily-swappable-parameters\n",
+ " <clang-tidy/checks/bugprone/easily-swappable-parameters>` check by\n",
+ " correcting a spelling mistake on its option\n",
+ " ``NamePrefixSuffixSilenceDissimilarityTreshold``.\n",
+ "\n",
+ ]
+ dups = _mod.find_duplicate_entries(lines, "Changes in existing checks")
+ # Expect one duplicate group for 'bugprone-easily-swappable-parameters' with two occurrences.
+ self.assertEqual(len(dups), 1)
+ key, occs = dups[0]
+ self.assertEqual(
+ key.strip(), "- Improved :doc:`bugprone-easily-swappable-parameters"
+ )
+ self.assertEqual(len(occs), 2)
+
+ report = _mod._emit_duplicate_report(lines, "Changes in existing checks")
+ self.assertIsInstance(report, str)
+ self.assertIn("Duplicate entries in 'Changes in existing checks':", report)
+ self.assertIn(
+ "-- Duplicate: - Improved :doc:`bugprone-easily-swappable-parameters",
+ report,
+ )
+ self.assertEqual(report.count("- At line "), 2)
+ self.assertIn("- At line 4:", report)
+ self.assertIn("- At line 14:", report)
----------------
vbvictor wrote:
Here also, match report as a whole chunk of text
https://github.com/llvm/llvm-project/pull/166072
More information about the cfe-commits
mailing list