Skip to content

Commit 404a903

Browse files
authored
[clang-tidy] Show descriptive error message when check_alphabetical_order.py fails (#170975)
Now we give clearer instructions on how to interpret the output of the script to fix issues it reported
1 parent 830ef4e commit 404a903

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

clang-tools-extra/clang-tidy/tool/check_alphabetical_order.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ def _scan_bullet_blocks(lines: Sequence[str], start: int, end: int) -> ScannedBl
126126
return ScannedBlocks(blocks_with_pos, i)
127127

128128

129-
def read_text(path: str) -> List[str]:
129+
def read_text(path: str) -> str:
130130
with io.open(path, "r", encoding="utf-8") as f:
131-
return f.read().splitlines(True)
131+
return f.read()
132132

133133

134134
def write_text(path: str, content: str) -> None:
@@ -364,14 +364,16 @@ def _emit_duplicate_report(lines: Sequence[str], title: str) -> Optional[str]:
364364

365365

366366
def process_release_notes(out_path: str, rn_doc: str) -> int:
367-
lines = read_text(rn_doc)
367+
text = read_text(rn_doc)
368+
lines = text.splitlines(True)
368369
normalized = normalize_release_notes(lines)
369370
write_text(out_path, normalized)
370371

371372
# Prefer reporting ordering issues first; let diff fail the test.
372-
if "".join(lines) != normalized:
373+
if text != normalized:
373374
sys.stderr.write(
374-
"Note: 'ReleaseNotes.rst' is not normalized; Please fix ordering first.\n"
375+
"\nEntries in 'clang-tools-extra/docs/ReleaseNotes.rst' are not alphabetically sorted.\n"
376+
"Fix the ordering by applying diff printed below.\n\n"
375377
)
376378
return 0
377379

@@ -383,8 +385,15 @@ def process_release_notes(out_path: str, rn_doc: str) -> int:
383385

384386

385387
def process_checks_list(out_path: str, list_doc: str) -> int:
386-
lines = read_text(list_doc)
387-
normalized = normalize_list_rst("".join(lines))
388+
text = read_text(list_doc)
389+
normalized = normalize_list_rst(text)
390+
391+
if text != normalized:
392+
sys.stderr.write(
393+
"\nChecks in 'clang-tools-extra/docs/clang-tidy/checks/list.rst' csv-table are not alphabetically sorted.\n"
394+
"Fix the ordering by applying diff printed below.\n\n"
395+
)
396+
388397
write_text(out_path, normalized)
389398
return 0
390399

clang-tools-extra/clang-tidy/tool/check_alphabetical_order_test.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_process_release_notes_with_unsorted_content(self) -> None:
164164
)
165165

166166
self.assertEqual(out, expected_out)
167-
self.assertIn("not normalized", buf.getvalue())
167+
self.assertIn("not alphabetically sorted", buf.getvalue())
168168

169169
def test_process_release_notes_prioritizes_sorting_over_duplicates(self) -> None:
170170
# Sorting is incorrect and duplicates exist, should report ordering issues first.
@@ -201,7 +201,7 @@ def test_process_release_notes_prioritizes_sorting_over_duplicates(self) -> None
201201
rc = _mod.process_release_notes(out_path, rn_doc)
202202
self.assertEqual(rc, 0)
203203
self.assertIn(
204-
"Note: 'ReleaseNotes.rst' is not normalized; Please fix ordering first.",
204+
"Entries in 'clang-tools-extra/docs/ReleaseNotes.rst' are not alphabetically sorted.",
205205
buf.getvalue(),
206206
)
207207

@@ -371,7 +371,14 @@ def test_process_checks_list_normalizes_output(self) -> None:
371371
out_doc = os.path.join(td, "out.rst")
372372
with open(in_doc, "w", encoding="utf-8") as f:
373373
f.write(list_text)
374-
rc = _mod.process_checks_list(out_doc, in_doc)
374+
buf = io.StringIO()
375+
with redirect_stderr(buf):
376+
rc = _mod.process_checks_list(out_doc, in_doc)
377+
self.assertEqual(rc, 0)
378+
self.assertIn(
379+
"Checks in 'clang-tools-extra/docs/clang-tidy/checks/list.rst' csv-table are not alphabetically sorted.",
380+
buf.getvalue(),
381+
)
375382
self.assertEqual(rc, 0)
376383
with open(out_doc, "r", encoding="utf-8") as f:
377384
out = f.read()

0 commit comments

Comments
 (0)