Add tests for html links

This commit is contained in:
Yurii Motov
2026-01-06 09:42:58 +01:00
parent a8bf5871d7
commit f5112778d0
8 changed files with 152 additions and 1 deletions

View File

@@ -441,7 +441,7 @@ def replace_html_links(
if len(links) != len(original_links):
raise ValueError(
"Number of HTML links does not match the number of HTML links in the "
"Number of HTML links does not match the number in the "
"original document "
f"({len(links)} vs {len(original_links)})"
)

View File

@@ -188,6 +188,18 @@ This is an <a href="https://fastapi.tiangolo.com">link to the main FastAPI site<
This is an <a href="https://fastapi.tiangolo.com/how-to/">link to one of the pages on FastAPI site</a> - tool should add language code to the URL.
Link to test wrong attribute: <a href="project-generation.md" class="internal-link">**FastAPI** Project Generators</a> - tool should fix the attribute.
### HTML links to static assets { #html-links-to-static-assets }
These are links to static assets:
* <a href="https://fastapi.tiangolo.com/img/fastapi-logo.png">FastAPI Logo</a>
* <a href="https://fastapi.tiangolo.com/css/fastapi.css">FastAPI CSS</a>
* <a href="https://fastapi.tiangolo.com/js/fastapi.js">FastAPI JS</a>
Tool should NOT add language code to their URLs.
# Header (with HTML link to <a href="https://tiangolo.com">tiangolo.com</a>) { #header-with-html-link-to-tiangolo-com }
#Not a header

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,19 @@
# Заголовок 1 { #header-1 }
Немного текста со ссылкой на <a href="https://fastapi.tiangolo.com">FastAPI</a>.
## Заголовок 2 { #header-2 }
Две ссылки здесь: <a href="https://fastapi.tiangolo.com/how-to/">How to</a> и <a href="project-generation.md" class="internal-link" target="_blank">Project Generators</a>.
### Заголовок 3 { #header-3 }
Ещё ссылка: <a href="project-generation.md" class="internal-link" target="_blank" title="Тайтл">**FastAPI** Генераторы Проектов</a> с тайтлом.
# Заголовок 4 { #header-4 }
Ссылка на якорь: <a href="#header-2">Заголовок 2</a>
# Заголовок с потерянной ссылкой { #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_html_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 HTML 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 HTML links does not match the number "
"in the original document (5 vs 6)"
) in result.output