Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions clang-tools-extra/clang-tidy/tool/check_alphabetical_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ def _scan_bullet_blocks(lines: Sequence[str], start: int, end: int) -> ScannedBl
return ScannedBlocks(blocks_with_pos, i)


def read_text(path: str) -> List[str]:
def read_text(path: str) -> str:
with io.open(path, "r", encoding="utf-8") as f:
return f.read().splitlines(True)
return f.read()
Comment on lines +129 to +131
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought to make it consistent with write_text because it accepts content: str



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


def process_release_notes(out_path: str, rn_doc: str) -> int:
lines = read_text(rn_doc)
text = read_text(rn_doc)
lines = text.splitlines(True)
normalized = normalize_release_notes(lines)
write_text(out_path, normalized)

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

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


def process_checks_list(out_path: str, list_doc: str) -> int:
lines = read_text(list_doc)
normalized = normalize_list_rst("".join(lines))
text = read_text(list_doc)
normalized = normalize_list_rst(text)

if text != normalized:
sys.stderr.write(
"\nChecks in 'clang-tools-extra/docs/clang-tidy/checks/list.rst' csv-table are not alphabetically sorted.\n"
"Fix the ordering by applying diff printed below.\n\n"
)

write_text(out_path, normalized)
return 0

Expand Down
13 changes: 10 additions & 3 deletions clang-tools-extra/clang-tidy/tool/check_alphabetical_order_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def test_process_release_notes_with_unsorted_content(self) -> None:
)

self.assertEqual(out, expected_out)
self.assertIn("not normalized", buf.getvalue())
self.assertIn("not alphabetically sorted", buf.getvalue())

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

Expand Down Expand Up @@ -371,7 +371,14 @@ def test_process_checks_list_normalizes_output(self) -> None:
out_doc = os.path.join(td, "out.rst")
with open(in_doc, "w", encoding="utf-8") as f:
f.write(list_text)
rc = _mod.process_checks_list(out_doc, in_doc)
buf = io.StringIO()
with redirect_stderr(buf):
rc = _mod.process_checks_list(out_doc, in_doc)
self.assertEqual(rc, 0)
self.assertIn(
"Checks in 'clang-tools-extra/docs/clang-tidy/checks/list.rst' csv-table are not alphabetically sorted.",
buf.getvalue(),
)
self.assertEqual(rc, 0)
with open(out_doc, "r", encoding="utf-8") as f:
out = f.read()
Expand Down
Loading