| 1 |
"""Test the documented snippets run correctly, and are the same for both RST and MyST.""" |
| 2 |
|
| 3 |
from collections.abc import Callable |
| 4 |
from pathlib import Path |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from .conftest import SphinxBuilder |
| 9 |
|
| 10 |
try: |
| 11 |
import myst_parser # noqa: F401 |
| 12 |
|
| 13 |
MYST_INSTALLED = True |
| 14 |
except ImportError: |
| 15 |
MYST_INSTALLED = False |
| 16 |
|
| 17 |
SNIPPETS_PATH = Path(__file__).parent.parent / "docs" / "snippets" |
| 18 |
SNIPPETS_GLOB_RST = list((SNIPPETS_PATH / "rst").glob("[!_]*")) |
| 19 |
SNIPPETS_GLOB_MYST = list((SNIPPETS_PATH / "myst").glob("[!_]*")) |
| 20 |
|
| 21 |
|
| 22 |
def write_assets(src_path: Path): |
| 23 |
"""Write additional assets to the src directory.""" |
| 24 |
src_path.joinpath("snippet.py").write_text("a = 1") |
| 25 |
src_path.joinpath("images").mkdir() |
| 26 |
src_path.joinpath("images", "ebp-logo.png").touch() |
| 27 |
src_path.joinpath("images", "particle_background.jpg").touch() |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.parametrize( |
| 31 |
"path", |
| 32 |
SNIPPETS_GLOB_RST, |
| 33 |
ids=[path.name[: -len(path.suffix)] for path in SNIPPETS_GLOB_RST], |
| 34 |
) |
| 35 |
def test_snippets_rst( |
| 36 |
sphinx_builder: Callable[..., SphinxBuilder], |
| 37 |
path: Path, |
| 38 |
file_regression, |
| 39 |
normalize_doctree_xml, |
| 40 |
): |
| 41 |
"""Test snippets written in RestructuredText (before post-transforms).""" |
| 42 |
builder = sphinx_builder(conf_kwargs={"extensions": ["sphinx_design"]}) |
| 43 |
content = "Heading\n-------" + "\n\n" + path.read_text(encoding="utf8") |
| 44 |
builder.src_path.joinpath("index.rst").write_text(content, encoding="utf8") |
| 45 |
write_assets(builder.src_path) |
| 46 |
builder.build() |
| 47 |
doctree = builder.get_doctree("index", post_transforms=False) |
| 48 |
doctree.attributes.pop("translation_progress", None) # added in sphinx 7.1 |
| 49 |
file_regression.check( |
| 50 |
normalize_doctree_xml(doctree.pformat()), |
| 51 |
basename=f"snippet_pre_{path.name[: -len(path.suffix)]}", |
| 52 |
extension=".xml", |
| 53 |
encoding="utf8", |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
@pytest.mark.parametrize( |
| 58 |
"path", |
| 59 |
SNIPPETS_GLOB_MYST, |
| 60 |
ids=[path.name[: -len(path.suffix)] for path in SNIPPETS_GLOB_MYST], |
| 61 |
) |
| 62 |
@pytest.mark.skipif(not MYST_INSTALLED, reason="myst-parser not installed") |
| 63 |
def test_snippets_myst( |
| 64 |
sphinx_builder: Callable[..., SphinxBuilder], |
| 65 |
path: Path, |
| 66 |
file_regression, |
| 67 |
normalize_doctree_xml, |
| 68 |
): |
| 69 |
"""Test snippets written in MyST Markdown (before post-transforms).""" |
| 70 |
builder = sphinx_builder() |
| 71 |
content = "# Heading" + "\n\n\n" + path.read_text(encoding="utf8") |
| 72 |
builder.src_path.joinpath("index.md").write_text(content, encoding="utf8") |
| 73 |
write_assets(builder.src_path) |
| 74 |
builder.build() |
| 75 |
doctree = builder.get_doctree("index", post_transforms=False) |
| 76 |
doctree.attributes.pop("translation_progress", None) # added in sphinx 7.1 |
| 77 |
file_regression.check( |
| 78 |
normalize_doctree_xml(doctree.pformat()), |
| 79 |
basename=f"snippet_pre_{path.name[: -len(path.suffix)]}", |
| 80 |
extension=".xml", |
| 81 |
encoding="utf8", |
| 82 |
) |
| 83 |
|
| 84 |
|
| 85 |
@pytest.mark.parametrize( |
| 86 |
"path", |
| 87 |
SNIPPETS_GLOB_RST, |
| 88 |
ids=[path.name[: -len(path.suffix)] for path in SNIPPETS_GLOB_RST], |
| 89 |
) |
| 90 |
def test_snippets_rst_post( |
| 91 |
sphinx_builder: Callable[..., SphinxBuilder], |
| 92 |
path: Path, |
| 93 |
file_regression, |
| 94 |
normalize_doctree_xml, |
| 95 |
): |
| 96 |
"""Test snippets written in RestructuredText (after HTML post-transforms).""" |
| 97 |
builder = sphinx_builder(conf_kwargs={"extensions": ["sphinx_design"]}) |
| 98 |
content = "Heading\n-------" + "\n\n" + path.read_text(encoding="utf8") |
| 99 |
builder.src_path.joinpath("index.rst").write_text(content, encoding="utf8") |
| 100 |
write_assets(builder.src_path) |
| 101 |
builder.build() |
| 102 |
doctree = builder.get_doctree("index", post_transforms=True) |
| 103 |
doctree.attributes.pop("translation_progress", None) # added in sphinx 7.1 |
| 104 |
file_regression.check( |
| 105 |
normalize_doctree_xml(doctree.pformat()), |
| 106 |
basename=f"snippet_post_{path.name[: -len(path.suffix)]}", |
| 107 |
extension=".xml", |
| 108 |
encoding="utf8", |
| 109 |
) |
| 110 |
|
| 111 |
|
| 112 |
@pytest.mark.parametrize( |
| 113 |
"path", |
| 114 |
SNIPPETS_GLOB_MYST, |
| 115 |
ids=[path.name[: -len(path.suffix)] for path in SNIPPETS_GLOB_MYST], |
| 116 |
) |
| 117 |
@pytest.mark.skipif(not MYST_INSTALLED, reason="myst-parser not installed") |
| 118 |
def test_snippets_myst_post( |
| 119 |
sphinx_builder: Callable[..., SphinxBuilder], |
| 120 |
path: Path, |
| 121 |
file_regression, |
| 122 |
normalize_doctree_xml, |
| 123 |
): |
| 124 |
"""Test snippets written in MyST Markdown (after HTML post-transforms).""" |
| 125 |
builder = sphinx_builder() |
| 126 |
content = "# Heading" + "\n\n\n" + path.read_text(encoding="utf8") |
| 127 |
builder.src_path.joinpath("index.md").write_text(content, encoding="utf8") |
| 128 |
write_assets(builder.src_path) |
| 129 |
builder.build() |
| 130 |
doctree = builder.get_doctree("index", post_transforms=True) |
| 131 |
doctree.attributes.pop("translation_progress", None) # added in sphinx 7.1 |
| 132 |
file_regression.check( |
| 133 |
normalize_doctree_xml(doctree.pformat()), |
| 134 |
basename=f"snippet_post_{path.name[: -len(path.suffix)]}", |
| 135 |
extension=".xml", |
| 136 |
encoding="utf8", |
| 137 |
) |
| 138 |
|
| 139 |
|
| 140 |
def test_sd_hide_title_rst( |
| 141 |
sphinx_builder: Callable[..., SphinxBuilder], file_regression |
| 142 |
): |
| 143 |
"""Test that the root title is hidden.""" |
| 144 |
builder = sphinx_builder(conf_kwargs={"extensions": ["sphinx_design"]}) |
| 145 |
content = ":sd_hide_title:\n\nHeading\n-------\n\ncontent" |
| 146 |
builder.src_path.joinpath("index.rst").write_text(content, encoding="utf8") |
| 147 |
builder.build() |
| 148 |
doctree = builder.get_doctree("index", post_transforms=False) |
| 149 |
doctree.attributes.pop("translation_progress", None) # added in sphinx 7.1 |
| 150 |
file_regression.check( |
| 151 |
doctree.pformat(), |
| 152 |
basename="sd_hide_title", |
| 153 |
extension=".xml", |
| 154 |
encoding="utf8", |
| 155 |
) |
| 156 |
|
| 157 |
|
| 158 |
@pytest.mark.skipif(not MYST_INSTALLED, reason="myst-parser not installed") |
| 159 |
def test_sd_hide_title_myst( |
| 160 |
sphinx_builder: Callable[..., SphinxBuilder], file_regression |
| 161 |
): |
| 162 |
"""Test that the root title is hidden.""" |
| 163 |
builder = sphinx_builder() |
| 164 |
content = "---\nsd_hide_title: true\n---\n\n# Heading\n\ncontent" |
| 165 |
builder.src_path.joinpath("index.md").write_text(content, encoding="utf8") |
| 166 |
builder.build() |
| 167 |
doctree = builder.get_doctree("index", post_transforms=False) |
| 168 |
doctree.attributes.pop("translation_progress", None) # added in sphinx 7.1 |
| 169 |
file_regression.check( |
| 170 |
doctree.pformat(), |
| 171 |
basename="sd_hide_title", |
| 172 |
extension=".xml", |
| 173 |
encoding="utf8", |
| 174 |
) |
| 175 |
|
| 176 |
|
| 177 |
@pytest.mark.skipif(not MYST_INSTALLED, reason="myst-parser not installed") |
| 178 |
def test_sd_custom_directives( |
| 179 |
sphinx_builder: Callable[..., SphinxBuilder], file_regression, normalize_doctree_xml |
| 180 |
): |
| 181 |
"""Test that the defaults are used.""" |
| 182 |
builder = sphinx_builder( |
| 183 |
conf_kwargs={ |
| 184 |
"extensions": ["myst_parser", "sphinx_design"], |
| 185 |
"sd_custom_directives": { |
| 186 |
"dropdown-syntax": { |
| 187 |
"inherit": "dropdown", |
| 188 |
"argument": "Syntax", |
| 189 |
"options": { |
| 190 |
"color": "primary", |
| 191 |
"icon": "code", |
| 192 |
}, |
| 193 |
} |
| 194 |
}, |
| 195 |
} |
| 196 |
) |
| 197 |
content = "# Heading\n\n```{dropdown-syntax}\ncontent\n```" |
| 198 |
builder.src_path.joinpath("index.md").write_text(content, encoding="utf8") |
| 199 |
builder.build() |
| 200 |
doctree = builder.get_doctree("index", post_transforms=False) |
| 201 |
doctree.attributes.pop("translation_progress", None) # added in sphinx 7.1 |
| 202 |
file_regression.check( |
| 203 |
normalize_doctree_xml(doctree.pformat()), |
| 204 |
basename="sd_custom_directives", |
| 205 |
extension=".xml", |
| 206 |
encoding="utf8", |
| 207 |
) |