-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathtest_fixtures_docutils.py
More file actions
138 lines (108 loc) · 4.61 KB
/
test_fixtures_docutils.py
File metadata and controls
138 lines (108 loc) · 4.61 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""Test fixture files, using the ``DocutilsRenderer``.
Note, the output AST is before any transforms are applied.
"""
from __future__ import annotations
import shlex
from io import StringIO
from pathlib import Path
from typing import Any
import pytest
from docutils import __version_info__ as docutils_version
from docutils.core import Publisher, publish_doctree
from pytest_param_files import ParamTestData
from myst_parser.parsers.docutils_ import Parser
FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures")
@pytest.mark.param_file(FIXTURE_PATH / "docutil_syntax_elements.md")
def test_syntax_elements(file_params: ParamTestData, monkeypatch, normalize_doctree_xml):
"""Test conversion of Markdown to docutils AST (before transforms are applied)."""
def _apply_transforms(self):
pass
if "[APPLY TRANSFORMS]" not in file_params.title:
monkeypatch.setattr(Publisher, "apply_transforms", _apply_transforms)
doctree = publish_doctree(
file_params.content,
source_path="notset",
parser=Parser(),
settings_overrides={"myst_highlight_code_blocks": False},
)
# in docutils 0.18 footnote ids have changed
outcome = normalize_doctree_xml(doctree.pformat()).replace(
'"footnote-reference-1"', '"id1"'
)
outcome = outcome.replace(' language=""', "")
file_params.assert_expected(outcome, rstrip_lines=True)
@pytest.mark.param_file(FIXTURE_PATH / "docutil_link_resolution.md")
def test_link_resolution(file_params: ParamTestData, normalize_doctree_xml):
"""Test that Markdown links resolve to the correct target, or give the correct warning."""
settings = settings_from_cmdline(file_params.description)
report_stream = StringIO()
settings["warning_stream"] = report_stream
if file_params.title == "explicit>implicit":
if docutils_version < (0, 22):
# reporting changed in docutils 0.22
pytest.skip("different in docutils>=0.22")
settings["report_level"] = 0
doctree = publish_doctree(
file_params.content,
source_path="<src>/index.md",
parser=Parser(),
settings_overrides=settings,
)
outcome = normalize_doctree_xml(doctree.pformat())
if report_stream.getvalue().strip():
outcome += "\n\n" + report_stream.getvalue().strip()
file_params.assert_expected(outcome, rstrip_lines=True)
@pytest.mark.param_file(FIXTURE_PATH / "docutil_roles.md")
def test_docutils_roles(file_params: ParamTestData, monkeypatch, normalize_doctree_xml):
"""Test conversion of Markdown to docutils AST (before transforms are applied)."""
def _apply_transforms(self):
pass
monkeypatch.setattr(Publisher, "apply_transforms", _apply_transforms)
doctree = publish_doctree(
file_params.content,
source_path="notset",
parser=Parser(),
)
file_params.assert_expected(
normalize_doctree_xml(doctree.pformat()), rstrip_lines=True
)
@pytest.mark.param_file(FIXTURE_PATH / "docutil_directives.md")
def test_docutils_directives(file_params: ParamTestData, monkeypatch, normalize_doctree_xml):
"""Test output of docutils directives."""
if "SKIP" in file_params.description: # line-block directive not yet supported
pytest.skip(file_params.description)
def _apply_transforms(self):
pass
monkeypatch.setattr(Publisher, "apply_transforms", _apply_transforms)
doctree = publish_doctree(
file_params.content,
source_path="notset",
parser=Parser(),
)
file_params.assert_expected(
normalize_doctree_xml(doctree.pformat()), rstrip_lines=True
)
@pytest.mark.param_file(FIXTURE_PATH / "docutil_syntax_extensions.txt")
def test_syntax_extensions(file_params: ParamTestData, normalize_doctree_xml):
"""The description is parsed as a docutils commandline"""
settings = settings_from_cmdline(file_params.description)
report_stream = StringIO()
settings["warning_stream"] = report_stream
doctree = publish_doctree(
file_params.content,
parser=Parser(),
settings_overrides=settings,
)
file_params.assert_expected(
normalize_doctree_xml(doctree.pformat()), rstrip_lines=True
)
def settings_from_cmdline(cmdline: str | None) -> dict[str, Any]:
"""Parse a docutils commandline into a settings dictionary"""
if cmdline is None or not cmdline.strip():
return {}
pub = Publisher(parser=Parser())
try:
pub.process_command_line(shlex.split(cmdline))
except Exception as err:
raise AssertionError(f"Failed to parse commandline: {cmdline}\n{err}") from err
return vars(pub.settings)