/
test_build_html.py
65 lines (58 loc) · 1.89 KB
/
test_build_html.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from io import StringIO
import pytest
from bs4 import BeautifulSoup
from sphinx.testing.util import SphinxTestApp
@pytest.mark.parametrize(
"index,expected_url",
[
(0, "http://abehiroshi.la.coocan.jp"),
(1, "https://pypi.org/project/sphinx-new-tab-link/"),
(
2,
"https://www.sphinx-doc.org/en/master/usage/restructuredtext/"
"basics.html",
),
(3, "https://example.com/"),
(4, "https://httpbin.org/"),
(5, "https://example.com/"),
(6, "https://github.com/ftnext/sphinx-new-tab-link"),
],
ids=[
"url_only_line",
"inline_external_link",
"refer_external_link_target",
"embed_external_link_with_target_definition",
"embed_anonymous_external_link",
"refer_embedded_external_link_target_again",
"refer_anonymous_external_link_target",
],
)
@pytest.mark.sphinx("html", testroot="default")
def test_should_open_new_tab(
app: SphinxTestApp,
status: StringIO,
warning: StringIO,
index: int,
expected_url: str,
):
app.build()
html = (app.outdir / "index.html").read_text()
soup = BeautifulSoup(html, "html.parser")
references = soup.find_all("a", {"class": "reference"})
ref = references[index]
assert ref["href"] == expected_url
assert "external" in ref["class"]
assert ref["target"] == "_blank"
assert ref["rel"] == ["noopener", "noreferrer"]
@pytest.mark.sphinx("html", testroot="default")
def test_internal_link_should_not_open_new_tab(
app: SphinxTestApp, status: StringIO, warning: StringIO
):
app.build()
html = (app.outdir / "index.html").read_text()
soup = BeautifulSoup(html, "html.parser")
references = soup.find_all("a", {"class": "reference"})
ref = references[-1]
assert "internal" in ref["class"]
assert "target" not in ref.attrs
assert "rel" not in ref.attrs