Add tests for markdown links

This commit is contained in:
Yurii Motov 2026-01-06 08:33:43 +01:00
parent 5ca9472d8a
commit a8bf5871d7
8 changed files with 176 additions and 0 deletions

View File

@ -166,6 +166,20 @@ This is a link to the main FastAPI site: [FastAPI](https://fastapi.tiangolo.com)
This is a link to one of the pages on FastAPI site: [How to](https://fastapi.tiangolo.com/how-to/) - tool should add language code to the URL.
Link to test wrong attribute: [**FastAPI** Project Generators](project-generation.md){.internal-link} - tool should fix the attribute.
Link with a title: [Example](https://example.com "Example site") - URL will be fixed, title preserved.
### Markdown link to static assets { #markdown-link-to-static-assets }
These are links to static assets:
* [FastAPI Logo](https://fastapi.tiangolo.com/img/fastapi-logo.png)
* [FastAPI CSS](https://fastapi.tiangolo.com/css/fastapi.css)
* [FastAPI JS](https://fastapi.tiangolo.com/js/fastapi.js)
Tool should NOT add language code to their URLs.
## HTML-style links { #html-style-links }
This is an <a href="https://example.com" target="_blank" class="external-link">HTML link</a> to an external site.

View File

@ -161,6 +161,20 @@ def hello_world():// Печать приветствия
Это ссылка на одну из страниц на сайте FastAPI: [How to](https://fastapi.tiangolo.com/how-to) — инструмент должен добавить код языка в URL.
Ссылка для тестирования неправильного атрибута: [**FastAPI** генераторы проектов](project-generation.md){.external-link} - инструмент должен исправить атрибут.
Ссылка с заголовком: [Пример](http://example.com/ "Сайт для примера") - URL будет исправлен инструментом, заголовок сохранится.
### Markdown ссылки на статические ресурсы { #markdown-link-to-static-assets }
Это ссылки на статические ресурсы:
* [FastAPI Logo](https://fastapi.tiangolo.com/img/fastapi-logo.png)
* [FastAPI CSS](https://fastapi.tiangolo.com/css/fastapi.css)
* [FastAPI JS](https://fastapi.tiangolo.com/js/fastapi.js)
Инструмент НЕ должен добавлять код языка в их URL.
## Ссылки в стиле HTML { #html-style-links }
Это <a href="https://example.com" target="_blank" class="external-link">HTML-ссылка</a> на внешний сайт.

View File

@ -161,6 +161,20 @@ def hello_world():// Print greeting
Это ссылка на одну из страниц на сайте FastAPI: [How to](https://fastapi.tiangolo.com/lang/how-to/) — инструмент должен добавить код языка в URL.
Ссылка для тестирования неправильного атрибута: [**FastAPI** генераторы проектов](project-generation.md){.internal-link} - инструмент должен исправить атрибут.
Ссылка с заголовком: [Пример](https://example.com "Сайт для примера") - URL будет исправлен инструментом, заголовок сохранится.
### Markdown ссылки на статические ресурсы { #markdown-link-to-static-assets }
Это ссылки на статические ресурсы:
* [FastAPI Logo](https://fastapi.tiangolo.com/img/fastapi-logo.png)
* [FastAPI CSS](https://fastapi.tiangolo.com/css/fastapi.css)
* [FastAPI JS](https://fastapi.tiangolo.com/js/fastapi.js)
Инструмент НЕ должен добавлять код языка в их URL.
## Ссылки в стиле HTML { #html-style-links }
Это <a href="https://example.com" target="_blank" class="external-link">HTML-ссылка</a> на внешний сайт.

View File

@ -0,0 +1,19 @@
# Header 1 { #header-1 }
Some text with a link to [FastAPI](https://fastapi.tiangolo.com).
## Header 2 { #header-2 }
Two links here: [How to](https://fastapi.tiangolo.com/how-to/) and [Project Generators](project-generation.md){.internal-link target=_blank}.
### Header 3 { #header-3 }
Another link: [**FastAPI** Project Generators](project-generation.md "Link title"){.internal-link target=_blank} with title.
# Header 4 { #header-4 }
Link to anchor: [Header 2](#header-2)
# Header with [link](http://example.com) { #header-with-link }
Some text

View File

@ -0,0 +1,19 @@
# Заголовок 1 { #header-1 }
Немного текста со ссылкой на [FastAPI](https://fastapi.tiangolo.com).
## Заголовок 2 { #header-2 }
Две ссылки здесь: [How to](https://fastapi.tiangolo.com/how-to/) и [Project Generators](project-generation.md){.internal-link target=_blank}.
### Заголовок 3 { #header-3 }
Ещё ссылка: [**FastAPI** Генераторы Проектов](project-generation.md "Тайтл"){.internal-link target=_blank} с тайтлом.
# Заголовок 4 { #header-4 }
Ссылка на якорь: [Заголовок 2](#header-2)
# Заголовок со [ссылкой](http://example.com) { #header-with-link }
Немного текста

View File

@ -0,0 +1,21 @@
# Заголовок 1 { #header-1 }
Немного текста со ссылкой на [FastAPI](https://fastapi.tiangolo.com).
## Заголовок 2 { #header-2 }
Две ссылки здесь: [How to](https://fastapi.tiangolo.com/how-to/) и [Project Generators](project-generation.md){.internal-link target=_blank}.
### Заголовок 3 { #header-3 }
Ещё ссылка: [**FastAPI** Генераторы Проектов](project-generation.md "Тайтл"){.internal-link target=_blank} с тайтлом.
И ещё одна [экстра ссылка](https://github.com).
# Заголовок 4 { #header-4 }
Ссылка на якорь: [Заголовок 2](#header-2)
# Заголовок со [ссылкой](http://example.com) { #header-with-link }
Немного текста

View File

@ -0,0 +1,19 @@
# Заголовок 1 { #header-1 }
Немного текста со ссылкой на [FastAPI](https://fastapi.tiangolo.com).
## Заголовок 2 { #header-2 }
Две ссылки здесь: [How to](https://fastapi.tiangolo.com/how-to/) и [Project Generators](project-generation.md){.internal-link target=_blank}.
### Заголовок 3 { #header-3 }
Ещё ссылка: [**FastAPI** Генераторы Проектов](project-generation.md "Тайтл"){.internal-link target=_blank} с тайтлом.
# Заголовок 4 { #header-4 }
Ссылка на якорь: [Заголовок 2](#header-2)
# Заголовок с потерянной ссылкой { #header-with-link }
Немного текста

View File

@ -0,0 +1,56 @@
from pathlib import Path
import pytest
from typer.testing import CliRunner
from scripts.translation_fixer import cli
data_path = Path(
"scripts/tests/test_translation_fixer/test_markdown_links/data"
).absolute()
@pytest.mark.parametrize(
"copy_test_files",
[(f"{data_path}/en_doc.md", f"{data_path}/translated_doc_number_gt.md")],
indirect=True,
)
def test_gt(runner: CliRunner, root_dir: Path, copy_test_files):
result = runner.invoke(
cli,
["fix-pages", "docs/lang/docs/doc.md"],
)
assert result.exit_code == 1, result.output
fixed_content = (root_dir / "docs" / "lang" / "docs" / "doc.md").read_text()
expected_content = Path(f"{data_path}/translated_doc_number_gt.md").read_text()
assert fixed_content == expected_content # Translated doc remains unchanged
assert "Error processing docs/lang/docs/doc.md" in result.output
assert (
"Number of markdown links does not match the number "
"in the original document (7 vs 6)"
) in result.output
@pytest.mark.parametrize(
"copy_test_files",
[(f"{data_path}/en_doc.md", f"{data_path}/translated_doc_number_lt.md")],
indirect=True,
)
def test_lt(runner: CliRunner, root_dir: Path, copy_test_files):
result = runner.invoke(
cli,
["fix-pages", "docs/lang/docs/doc.md"],
)
# assert result.exit_code == 1, result.output
fixed_content = (root_dir / "docs" / "lang" / "docs" / "doc.md").read_text()
expected_content = Path(f"{data_path}/translated_doc_number_lt.md").read_text()
assert fixed_content == expected_content # Translated doc remains unchanged
assert "Error processing docs/lang/docs/doc.md" in result.output
assert (
"Number of markdown links does not match the number "
"in the original document (5 vs 6)"
) in result.output