From 9f0e25c893fd1a16176f103e48f86013b87ca90b Mon Sep 17 00:00:00 2001 From: ftnext Date: Wed, 11 Oct 2023 08:51:44 +0900 Subject: [PATCH] [refactor] Parametrize builder to make tests DRY (close #9) --- tests/sphinx_new_tab_link/__init__.py | 0 .../sphinx_new_tab_link/test_build_dirhtml.py | 44 --------------- tests/sphinx_new_tab_link/test_build_html.py | 44 --------------- .../test_build_singlehtml.py | 44 --------------- tests/test_sphinx_new_tab_link.py | 54 +++++++++++++++++++ 5 files changed, 54 insertions(+), 132 deletions(-) delete mode 100644 tests/sphinx_new_tab_link/__init__.py delete mode 100644 tests/sphinx_new_tab_link/test_build_dirhtml.py delete mode 100644 tests/sphinx_new_tab_link/test_build_html.py delete mode 100644 tests/sphinx_new_tab_link/test_build_singlehtml.py diff --git a/tests/sphinx_new_tab_link/__init__.py b/tests/sphinx_new_tab_link/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/sphinx_new_tab_link/test_build_dirhtml.py b/tests/sphinx_new_tab_link/test_build_dirhtml.py deleted file mode 100644 index 09c7385..0000000 --- a/tests/sphinx_new_tab_link/test_build_dirhtml.py +++ /dev/null @@ -1,44 +0,0 @@ -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("dirhtml", testroot="default") -def test_should_open_new_tab( - app: SphinxTestApp, 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"] diff --git a/tests/sphinx_new_tab_link/test_build_html.py b/tests/sphinx_new_tab_link/test_build_html.py deleted file mode 100644 index e87e010..0000000 --- a/tests/sphinx_new_tab_link/test_build_html.py +++ /dev/null @@ -1,44 +0,0 @@ -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, 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"] diff --git a/tests/sphinx_new_tab_link/test_build_singlehtml.py b/tests/sphinx_new_tab_link/test_build_singlehtml.py deleted file mode 100644 index 27fc601..0000000 --- a/tests/sphinx_new_tab_link/test_build_singlehtml.py +++ /dev/null @@ -1,44 +0,0 @@ -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("singlehtml", testroot="default") -def test_should_open_new_tab( - app: SphinxTestApp, 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"] diff --git a/tests/test_sphinx_new_tab_link.py b/tests/test_sphinx_new_tab_link.py index 761494f..bbb5542 100644 --- a/tests/test_sphinx_new_tab_link.py +++ b/tests/test_sphinx_new_tab_link.py @@ -4,6 +4,60 @@ from bs4 import BeautifulSoup +@pytest.mark.parametrize("builder", ["html", "singlehtml", "dirhtml"]) +@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", + ], +) +def test_should_open_new_tab( + make_app, + sphinx_test_tempdir: str, + rootdir: str, + builder: str, + index: int, + expected_url: 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[index] + assert ref["href"] == expected_url + assert "external" in ref["class"] + assert ref["target"] == "_blank" + assert ref["rel"] == ["noopener", "noreferrer"] + + @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