-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathtest_docs_use_base_url.py
36 lines (28 loc) · 1.18 KB
/
test_docs_use_base_url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from pathlib import Path
from typing import List, Text
import re
import pytest
DOCS_BASE_DIR = Path("docs/")
MDX_DOCS_FILES = list((DOCS_BASE_DIR / "docs").glob("**/*.mdx"))
# we're matching anchors with href containing strings, but not starting
# with "http". This also exclude local href already configured using `useBaseUrl()`
ANCHOR_RE = re.compile(
r"<(a|Button)[^>]*href=\"(?P<href>(?!http).+?)\"[^>]*>", re.DOTALL
)
@pytest.mark.parametrize("mdx_file_path", MDX_DOCS_FILES)
def test_docs_anchors_base_url(mdx_file_path: Path):
with mdx_file_path.open("r") as handle:
mdx_content = handle.read()
matches = ANCHOR_RE.finditer(mdx_content)
lines_with_errors: List[Text] = []
for match in matches:
href = match.group("href")
start_index = match.span()[0]
line_number = mdx_content.count("\n", 0, start_index) + 1
lines_with_errors.append(f"{line_number} with href={href}")
if lines_with_errors:
plural = "s" if len(lines_with_errors) > 1 else ""
raise AssertionError(
f"({mdx_file_path}): Invalid anchor{plural} not using useBaseUrl() "
f"at line{plural} {', '.join(lines_with_errors)}"
)