Skip to content

Commit

Permalink
[refactor] Make DRY by overriding fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
ftnext committed Aug 11, 2024
1 parent e643af2 commit 9857ee5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 69 deletions.
32 changes: 32 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,42 @@

import pytest

from .helpers import prepare_files

pytest_plugins = "sphinx.testing.fixtures"
collect_ignore = ["roots"]


@pytest.fixture(scope="session")
def rootdir() -> Path:
return Path(__file__).parent / "roots"


@pytest.fixture
def builder() -> str:
raise NotImplementedError("Define `builder` fixture in each test module")


@pytest.fixture()
def directory_name() -> str:
raise NotImplementedError(
"Define `directory_name` fixture in each test module"
)


@pytest.fixture
def prepared_srcdir(
sphinx_test_tempdir: Path, rootdir: Path, directory_name: str
) -> Path:
srcdir = sphinx_test_tempdir / directory_name
prepare_files(rootdir / f"test-{directory_name}", srcdir)

return srcdir


@pytest.fixture
def built_html_path(make_app, builder: str, prepared_srcdir: Path) -> Path:
app = make_app(builder, srcdir=prepared_srcdir)
app.build()

return app.outdir / "index.html"
24 changes: 1 addition & 23 deletions tests/test_custom_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import pytest

from .helpers import (
assert_reference_attributes,
extract_references,
prepare_files,
)
from .helpers import assert_reference_attributes, extract_references


@pytest.fixture
Expand All @@ -19,24 +15,6 @@ def directory_name(request) -> str:
return request.param


@pytest.fixture
def prepared_srcdir(
sphinx_test_tempdir: Path, rootdir: Path, directory_name: str
) -> Path:
srcdir = sphinx_test_tempdir / directory_name
prepare_files(rootdir / f"test-{directory_name}", srcdir)

return srcdir


@pytest.fixture
def built_html_path(make_app, builder: str, prepared_srcdir: Path) -> Path:
app = make_app(builder, srcdir=prepared_srcdir)
app.build()

return app.outdir / "index.html"


def test_should_open_new_tab(built_html_path: Path) -> None:
references = extract_references(built_html_path)

Expand Down
24 changes: 1 addition & 23 deletions tests/test_show_external_link_icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import pytest

from .helpers import (
assert_reference_is_external,
extract_references,
prepare_files,
)
from .helpers import assert_reference_is_external, extract_references


@pytest.fixture
Expand All @@ -19,24 +15,6 @@ def directory_name() -> str:
return "external-link-icon"


@pytest.fixture
def prepared_srcdir(
sphinx_test_tempdir: Path, rootdir: Path, directory_name: str
) -> Path:
srcdir = sphinx_test_tempdir / directory_name
prepare_files(rootdir / f"test-{directory_name}", srcdir)

return srcdir


@pytest.fixture
def built_html_path(make_app, builder: str, prepared_srcdir: Path) -> Path:
app = make_app(builder, srcdir=prepared_srcdir)
app.build()

return app.outdir / "index.html"


def test_see_external_link_icon(built_html_path: Path) -> None:
references = extract_references(built_html_path)

Expand Down
24 changes: 1 addition & 23 deletions tests/test_sphinx_new_tab_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import pytest

from .helpers import (
assert_reference_is_external,
extract_references,
prepare_files,
)
from .helpers import assert_reference_is_external, extract_references


@pytest.fixture(params=["html", "singlehtml", "dirhtml"])
Expand All @@ -19,24 +15,6 @@ def directory_name() -> str:
return "default"


@pytest.fixture
def prepared_srcdir(
sphinx_test_tempdir: Path, rootdir: Path, directory_name: str
) -> Path:
srcdir = sphinx_test_tempdir / directory_name
prepare_files(rootdir / f"test-{directory_name}", srcdir)

return srcdir


@pytest.fixture
def built_html_path(make_app, builder: str, prepared_srcdir: Path) -> Path:
app = make_app(builder, srcdir=prepared_srcdir)
app.build()

return app.outdir / "index.html"


@pytest.mark.parametrize(
"index,expected_url",
[
Expand Down

0 comments on commit 9857ee5

Please sign in to comment.