| 1 | import re |
| 2 | from pathlib import Path |
| 3 | |
| 4 | |
| 5 | ROOT = Path(__file__).resolve().parents[1] |
| 6 | READMES = { |
| 7 | "English": ROOT / "README.md", |
| 8 | "Français": ROOT / "README.fr.md", |
| 9 | "Deutsch": ROOT / "README.de.md", |
| 10 | "Español": ROOT / "README.es.md", |
| 11 | "Português (Brasil)": ROOT / "README.pt-BR.md", |
| 12 | "日本語": ROOT / "README.ja.md", |
| 13 | "简体中文": ROOT / "README.zh-CN.md", |
| 14 | "العربية": ROOT / "README.ar.md", |
| 15 | "فارسی": ROOT / "README.fa.md", |
| 16 | "Türkçe": ROOT / "README.tr.md", |
| 17 | "Tiếng Việt": ROOT / "README.vi.md", |
| 18 | "Русский": ROOT / "README.ru.md", |
| 19 | "Українська": ROOT / "README.uk.md", |
| 20 | } |
| 21 | |
| 22 | |
| 23 | def _navigation(current: str) -> str: |
| 24 | return " | ".join( |
| 25 | label if label == current else f"[{label}]({path.name})" |
| 26 | for label, path in READMES.items() |
| 27 | ) |
| 28 | |
| 29 | |
| 30 | def _code_blocks(text: str) -> list[str]: |
| 31 | return re.findall(r"```[^\n]*\n(.*?)```", text, flags=re.DOTALL) |
| 32 | |
| 33 | |
| 34 | def _code_commands(text: str) -> list[str]: |
| 35 | return [ |
| 36 | line |
| 37 | for block in _code_blocks(text) |
| 38 | for line in block.splitlines() |
| 39 | if line and not line.lstrip().startswith("#") |
| 40 | ] |
| 41 | |
| 42 | |
| 43 | def _relative_link_targets(text: str) -> set[str]: |
| 44 | destinations = re.findall(r"\]\(([^)]+)\)", text) |
| 45 | return { |
| 46 | destination.split("#", 1)[0] |
| 47 | for destination in destinations |
| 48 | if destination |
| 49 | and not destination.startswith(("#", "http://", "https://", "mailto:")) |
| 50 | } |
| 51 | |
| 52 | |
| 53 | def _external_link_targets(text: str) -> set[str]: |
| 54 | return { |
| 55 | destination |
| 56 | for destination in re.findall(r"\]\(([^)]+)\)", text) |
| 57 | if destination.startswith(("http://", "https://")) |
| 58 | } |
| 59 | |
| 60 | |
| 61 | def test_readme_translations_preserve_structure_and_commands() -> None: |
| 62 | english = READMES["English"].read_text(encoding="utf-8") |
| 63 | expected_code_commands = _code_commands(english) |
| 64 | expected_code_fences = english.count("```") |
| 65 | expected_external_links = _external_link_targets(english) |
| 66 | expected_table_structure = [ |
| 67 | line.count("|") for line in english.splitlines() if line.startswith("|") |
| 68 | ] |
| 69 | expected_ordered_items = len(re.findall(r"^\d+\. ", english, flags=re.MULTILINE)) |
| 70 | |
| 71 | for label, path in READMES.items(): |
| 72 | text = path.read_text(encoding="utf-8") |
| 73 | lines = text.splitlines() |
| 74 | assert lines[0] == "# /last30days" |
| 75 | assert lines[2] == _navigation(label) |
| 76 | assert text.count("```") == expected_code_fences |
| 77 | assert _code_commands(text) == expected_code_commands |
| 78 | assert _external_link_targets(text) == expected_external_links |
| 79 | assert [line.count("|") for line in lines if line.startswith("|")] == ( |
| 80 | expected_table_structure |
| 81 | ) |
| 82 | assert len(re.findall(r"^\d+\. ", text, flags=re.MULTILINE)) == expected_ordered_items |
| 83 | assert not re.search(r"(?:ZXQ|XXQ|ZZQ|ZZZ)\d", text) |
| 84 | assert not re.search(r"^#", text, flags=re.MULTILINE) |
| 85 | assert not re.search(r"\]\([^)]+\)\]", text) |
| 86 | |
| 87 | |
| 88 | def test_readme_translation_relative_links_exist() -> None: |
| 89 | for path in READMES.values(): |
| 90 | for target in _relative_link_targets(path.read_text(encoding="utf-8")): |
| 91 | assert (ROOT / target).exists(), f"{path.name}: missing relative link target {target}" |
| 92 | |
| 93 | |
| 94 | def test_every_readme_lists_yahoo_finance_as_a_source() -> None: |
| 95 | for path in READMES.values(): |
| 96 | rows = [ |
| 97 | line |
| 98 | for line in path.read_text(encoding="utf-8").splitlines() |
| 99 | if line.startswith("| **Yahoo Finance** |") |
| 100 | ] |
| 101 | assert len(rows) == 1, path.name |
| 102 | assert "`yahoo-finance-pp-cli`" in rows[0], path.name |
| 103 | assert rows[0].count("|") == 3, path.name |
| 104 | |
| 105 | |
| 106 | def test_every_readme_lists_the_chinese_developer_sources() -> None: |
| 107 | for path in READMES.values(): |
| 108 | text = path.read_text(encoding="utf-8") |
| 109 | juejin_rows = [ |
| 110 | line for line in text.splitlines() if line.startswith("| **Juejin** |") |
| 111 | ] |
| 112 | v2ex_rows = [ |
| 113 | line for line in text.splitlines() if line.startswith("| **V2EX** |") |
| 114 | ] |
| 115 | assert len(juejin_rows) == 1, f"{path.name}: expected 1 Juejin row, got {len(juejin_rows)}" |
| 116 | assert len(v2ex_rows) == 1, f"{path.name}: expected 1 V2EX row, got {len(v2ex_rows)}" |
| 117 | jr = juejin_rows[0] |
| 118 | vr = v2ex_rows[0] |
| 119 | assert "`--search juejin`" in jr and "INCLUDE_SOURCES=juejin" in jr, path.name |
| 120 | assert "`--search v2ex`" in vr and "INCLUDE_SOURCES=v2ex" in vr, path.name |
| 121 | assert jr.count("|") == 3, path.name |
| 122 | assert vr.count("|") == 3, path.name |
| 123 |