Files
claude-howto/scripts/tests/test_check_cross_references.py
Logic769 2b530b7674 fix(scripts): keep combining marks in heading anchors (#226)
## Summary

`heading_to_anchor()` kept only Python `\w` characters plus spaces,
hyphens and U+200C, so combining marks (Unicode categories Mn, Mc, Me)
were dropped from heading anchors. GitHub keeps them in heading ids, so
a Persian heading such as `مجموعهٔ استاندارد ایموجی` (U+0654 ARABIC
HAMZA ABOVE) got an anchor without the mark. A link that matched GitHub
then failed the `cross-references` hook, while a link with the mark
removed passed even though it is broken on github.com.

The step-2 filter now keeps Mn/Mc/Me characters.
`scripts/build_website.py` carries its own copy of the function,
documented as mirroring the checker, and gets the identical change.

## Type of Change

- [x] Bug fix

## Changes

| File | Change |
|------|--------|
| `scripts/check_cross_references.py` | `heading_to_anchor`: the step-2
`re.sub` keeps characters whose category is in
`COMBINING_MARK_CATEGORIES` (Mn/Mc/Me) instead of dropping them, with a
comment explaining why |
| `scripts/build_website.py` | The same change in its own copy of
`heading_to_anchor` |
| `scripts/tests/test_check_cross_references.py` | U+0654 kept (Persian
from the issue); decomposed Latin, Arabic harakat and Devanagari marks
kept; U+FE0F still stripped because step 1 removes it; `main()` passes a
link that keeps the mark and reports a link without it |
| `scripts/tests/test_build_website.py` | U+0654 kept; website and
checker return the same anchor for Persian with a combining mark,
decomposed Latin and Devanagari headings |

## Verification

Before/after on the three headings named in the issue (U+0654 shown as
`\u0654`):

```text
مجموعهٔ استاندارد ایموجی
  old: مجموعه-...   (mark dropped)
  new: مجموعهٔ-...
فرادادهٔ YAML
  old: فراداده-yaml
  new: فرادادهٔ-yaml
پانوشت فرادادهٔ سند
  old: پانوشت-فراداده-سند
  new: پانوشت-فرادادهٔ-سند
```

No existing anchor moves. Comparing the old and new implementation over
every heading in the repo:

```bash
$ python ../backups/scan_anchor_diff.py      # old() copied from the pre-change code
md files: 478 headings: 13647 changed: 0
```

Tests:

```bash
$ python -m pytest scripts/tests/test_check_cross_references.py scripts/tests/test_build_website.py -q
85 passed, 2 warnings in 10.24s

$ python -m pytest scripts/tests/ -q
1 failed, 197 passed, 2 warnings in 13.63s
```

The one failure
(`test_build_epub.py::TestChapterCollector::test_path_mapping`) is
pre-existing and Windows-only: the test asserts the posix key
`01-test-chapter/README.md` while `ChapterCollector` builds
`01-test-chapter\\README.md` on Windows. That file is untouched by this
PR and the CI job runs on `ubuntu-latest`.

Lint, format, types and security:

```bash
$ ruff check scripts/
All checks passed!
$ ruff format --check scripts/
19 files already formatted
$ mypy scripts/ --ignore-missing-imports
scripts\sync_translations.py:84: error: ...  (2 pre-existing errors, file not touched)
$ bandit -c scripts/pyproject.toml -r scripts/ --exclude scripts/tests/
Total issues (by severity): 0
```

No `.md` files change in this PR, so the five Markdown hooks
(`markdown-lint`, `cross-references`, `mermaid-syntax`, `link-check`,
`markdown-rendering`) are unaffected; the `cross-references` logic they
call is what the new tests cover.

## Notes

The markers are kept for Mn, Mc and Me, matching what GitHub's slugger
keeps (`\p{Mark}`). Variation selectors (U+FE00–U+FE0F) are category Mn
but sit in the emoji ranges stripped in step 1, so emoji-prefixed ids
are unchanged — covered by
`test_heading_anchor_still_strips_em_dash_and_zero_width_joiner`.

Closes #223


[![](https://img.shields.io/badge/powered_by-dsh-4D6BFE?style=flat-square&logo=deepseek&logoColor=white)](https://github.com/deepseek-ai/deepseek-harness)
2026-11-03 10:18:42 +01:00

177 lines
5.9 KiB
Python

"""Tests for check_cross_references.py — focus on repo-root boundary."""
from __future__ import annotations
import os
import sys
from pathlib import Path
import pytest
sys.path.insert(0, str(Path(__file__).parent.parent))
import check_cross_references
@pytest.fixture
def repo(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
monkeypatch.chdir(tmp_path)
return tmp_path
def test_links_escaping_repo_root_are_skipped(
repo: Path, capsys: pytest.CaptureFixture[str]
) -> None:
outside = repo.parent / "outside.md"
outside.write_text("# Outside")
rel = os.path.relpath(outside, repo)
(repo / "README.md").write_text(f"# Doc\n\n[escape]({rel})\n")
assert check_cross_references.main() == 0
assert "broken cross-reference" not in capsys.readouterr().out
def test_broken_in_repo_link_is_reported(
repo: Path, capsys: pytest.CaptureFixture[str]
) -> None:
(repo / "README.md").write_text("# Doc\n\n[missing](does-not-exist.md)\n")
assert check_cross_references.main() == 1
assert "broken cross-reference" in capsys.readouterr().out
def test_valid_in_repo_link_passes(
repo: Path, capsys: pytest.CaptureFixture[str]
) -> None:
(repo / "other.md").write_text("# Other")
(repo / "README.md").write_text("# Doc\n\n[ok](other.md)\n")
assert check_cross_references.main() == 0
assert "All cross-references valid" in capsys.readouterr().out
def test_numbered_lesson_dir_missing_readme_is_reported(
repo: Path, capsys: pytest.CaptureFixture[str]
) -> None:
(repo / "README.md").write_text("# Doc")
(repo / "01-intro").mkdir()
assert check_cross_references.main() == 1
assert "01-intro: missing README.md" in capsys.readouterr().out
# U+200C ZERO WIDTH NON-JOINER: Persian uses it inside words, and GitHub keeps
# it in heading ids (e.g. the rendered fa/STYLE_GUIDE.md in PR #208). Spelled as
# escapes so editors can't strip them silently.
ZWNJ = "\u200c"
ZWJ = "\u200d"
PERSIAN_HEADING = f"نام{ZWNJ}گذاری فایل و پوشه"
PERSIAN_ANCHOR = f"نام{ZWNJ}گذاری-فایل-و-پوشه"
def test_heading_anchor_keeps_zero_width_non_joiner() -> None:
assert check_cross_references.heading_to_anchor(PERSIAN_HEADING) == PERSIAN_ANCHOR
def test_heading_anchor_still_strips_em_dash_and_zero_width_joiner() -> None:
assert (
check_cross_references.heading_to_anchor("[v2.1.245] — 2026-08-25")
== "v21245--2026-08-25"
)
assert check_cross_references.heading_to_anchor(f"A{ZWJ}B test") == "ab-test"
# U+FE0F is a combining mark, but it sits in the emoji ranges stripped
# before the mark check, so emoji-prefixed ids are unchanged.
assert (
check_cross_references.heading_to_anchor("Rocket \u2764\ufe0f launch")
== "rocket--launch"
)
# U+0654 ARABIC HAMZA ABOVE is a combining mark (category Mn): GitHub keeps it
# in heading ids, but Python's \w does not match it. Spelled as an escape so
# editors can't normalize it away.
HAMZA_ABOVE = "\u0654"
ARABIC_HEADING = f"مجموعه{HAMZA_ABOVE} استاندارد ایموجی"
ARABIC_ANCHOR = f"مجموعه{HAMZA_ABOVE}-استاندارد-ایموجی"
def test_heading_anchor_keeps_combining_marks() -> None:
assert check_cross_references.heading_to_anchor(ARABIC_HEADING) == ARABIC_ANCHOR
@pytest.mark.parametrize(
("heading", "anchor"),
[
# Decomposed Latin: "cafe" + U+0301 COMBINING ACUTE ACCENT.
("cafe\u0301", "cafe\u0301"),
# Arabic harakat (U+064B-U+0652) and other marks stay put too.
("مُقَدِّمة", "مُقَدِّمة"),
# Devanagari vowel signs (Mc) and virama (Mn).
("हिन्दी", "हिन्दी"),
],
)
def test_heading_anchor_keeps_other_combining_marks(heading: str, anchor: str) -> None:
assert check_cross_references.heading_to_anchor(heading) == anchor
@pytest.mark.parametrize(
("heading", "anchor"),
[
("Hướng dẫn bắt đầu nhanh", "hướng-dẫn-bắt-đầu-nhanh"),
("快速入门 (Quick Start)", "快速入门-quick-start"),
("Швидкий старт: встановлення", "швидкий-старт-встановлення"),
("クイックスタート — 概要", "クイックスタート--概要"),
],
)
def test_heading_anchor_unchanged_for_existing_locales(
heading: str, anchor: str
) -> None:
assert check_cross_references.heading_to_anchor(heading) == anchor
def test_in_page_link_keeping_zwnj_passes(
repo: Path, capsys: pytest.CaptureFixture[str]
) -> None:
(repo / "README.md").write_text(
f"# {PERSIAN_HEADING}\n\n[link](#{PERSIAN_ANCHOR})\n", encoding="utf-8"
)
assert check_cross_references.main() == 0
assert "broken anchor" not in capsys.readouterr().out
def test_in_page_link_missing_zwnj_is_reported(
repo: Path, capsys: pytest.CaptureFixture[str]
) -> None:
anchor_without_zwnj = PERSIAN_ANCHOR.replace(ZWNJ, "")
(repo / "README.md").write_text(
f"# {PERSIAN_HEADING}\n\n[link](#{anchor_without_zwnj})\n", encoding="utf-8"
)
assert check_cross_references.main() == 1
assert "broken anchor" in capsys.readouterr().out
def test_in_page_link_keeping_combining_marks_passes(
repo: Path, capsys: pytest.CaptureFixture[str]
) -> None:
(repo / "README.md").write_text(
f"# {ARABIC_HEADING}\n\n[link](#{ARABIC_ANCHOR})\n", encoding="utf-8"
)
assert check_cross_references.main() == 0
assert "broken anchor" not in capsys.readouterr().out
def test_in_page_link_missing_combining_mark_is_reported(
repo: Path, capsys: pytest.CaptureFixture[str]
) -> None:
anchor_without_mark = ARABIC_ANCHOR.replace(HAMZA_ABOVE, "")
(repo / "README.md").write_text(
f"# {ARABIC_HEADING}\n\n[link](#{anchor_without_mark})\n", encoding="utf-8"
)
assert check_cross_references.main() == 1
assert "broken anchor" in capsys.readouterr().out