From 741dd8c23a6e1945684780baf1599c4d2b589e91 Mon Sep 17 00:00:00 2001 From: ftnext Date: Wed, 11 Oct 2023 08:45:54 +0900 Subject: [PATCH] [refactor] Parametrize builder to make DRY --- .../sphinx_new_tab_link/test_build_dirhtml.py | 13 --------- tests/sphinx_new_tab_link/test_build_html.py | 13 --------- .../test_build_singlehtml.py | 13 --------- tests/test_sphinx_new_tab_link.py | 27 +++++++++++++++++++ 4 files changed, 27 insertions(+), 39 deletions(-) create mode 100644 tests/test_sphinx_new_tab_link.py diff --git a/tests/sphinx_new_tab_link/test_build_dirhtml.py b/tests/sphinx_new_tab_link/test_build_dirhtml.py index f052a01..09c7385 100644 --- a/tests/sphinx_new_tab_link/test_build_dirhtml.py +++ b/tests/sphinx_new_tab_link/test_build_dirhtml.py @@ -42,16 +42,3 @@ def test_should_open_new_tab( assert "external" in ref["class"] assert ref["target"] == "_blank" assert ref["rel"] == ["noopener", "noreferrer"] - - -@pytest.mark.sphinx("dirhtml", testroot="default") -def test_internal_link_should_not_open_new_tab(app: SphinxTestApp): - 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 diff --git a/tests/sphinx_new_tab_link/test_build_html.py b/tests/sphinx_new_tab_link/test_build_html.py index 0892cc2..e87e010 100644 --- a/tests/sphinx_new_tab_link/test_build_html.py +++ b/tests/sphinx_new_tab_link/test_build_html.py @@ -42,16 +42,3 @@ def test_should_open_new_tab( 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): - 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 diff --git a/tests/sphinx_new_tab_link/test_build_singlehtml.py b/tests/sphinx_new_tab_link/test_build_singlehtml.py index 556c078..27fc601 100644 --- a/tests/sphinx_new_tab_link/test_build_singlehtml.py +++ b/tests/sphinx_new_tab_link/test_build_singlehtml.py @@ -42,16 +42,3 @@ def test_should_open_new_tab( assert "external" in ref["class"] assert ref["target"] == "_blank" assert ref["rel"] == ["noopener", "noreferrer"] - - -@pytest.mark.sphinx("singlehtml", testroot="default") -def test_internal_link_should_not_open_new_tab(app: SphinxTestApp): - 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 diff --git a/tests/test_sphinx_new_tab_link.py b/tests/test_sphinx_new_tab_link.py new file mode 100644 index 0000000..761494f --- /dev/null +++ b/tests/test_sphinx_new_tab_link.py @@ -0,0 +1,27 @@ +import shutil + +import pytest +from bs4 import BeautifulSoup + + +@pytest.mark.parametrize("builder", ["html", "singlehtml", "dirhtml"]) +def test_internal_link_should_not_open_new_tab( + make_app, sphinx_test_tempdir: str, rootdir: str, builder: str +): + testroot = "default" + srcdir = sphinx_test_tempdir / testroot + if not srcdir.exists(): + testroot_path = rootdir / f"test-{testroot}" + shutil.copytree(testroot_path, srcdir) + + app = make_app(builder, srcdir=srcdir) + 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