diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1bdb9cf7..089f8bb8 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -83,7 +83,7 @@ jobs: run: | python -m pip install --upgrade pip pip install . - pip install docutils==${{ matrix.docutils-version }} + pip install pytest~=6.2 docutils==${{ matrix.docutils-version }} - name: ensure sphinx is not installed run: | python -c "\ @@ -93,6 +93,8 @@ jobs: pass else: raise AssertionError()" + - name: Run pytest for docutils-only tests + run: pytest tests/test_docutils.py tests/test_renderers/test_fixtures_docutils.py - name: Run docutils CLI run: echo "test" | myst-docutils-html diff --git a/myst_parser/docutils_renderer.py b/myst_parser/docutils_renderer.py index ae77d4d9..33d1b97f 100644 --- a/myst_parser/docutils_renderer.py +++ b/myst_parser/docutils_renderer.py @@ -730,6 +730,7 @@ def dict_to_fm_field_list( """ field_list = nodes.field_list() + field_list.source, field_list.line = self.document["source"], line bibliofields = get_language(language_code).bibliographic_fields state_machine = MockStateMachine(self, line) @@ -741,9 +742,12 @@ def dict_to_fm_field_list( value = str(value) if key in bibliofields: para_nodes, _ = state.inline_text(value, line) - body_children = [nodes.paragraph("", "", *para_nodes)] else: - body_children = [nodes.Text(value, value)] + para_nodes = [nodes.Text(value, value)] + + body_children = [nodes.paragraph("", "", *para_nodes)] + body_children[0].source = self.document["source"] + body_children[0].line = 0 field_node = nodes.field() field_node.source = value diff --git a/tests/conftest.py b/tests/conftest.py index 970578a0..11b20c47 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1 +1,8 @@ -pytest_plugins = "sphinx.testing.fixtures" +"""Top-level configuration for pytest.""" +try: + import sphinx # noqa: F401 +except ImportError: + pass +else: + # only use when Sphinx is installed, to allow testing myst-docutils + pytest_plugins = "sphinx.testing.fixtures" diff --git a/tests/test_renderers/fixtures/syntax_elements.md b/tests/test_renderers/fixtures/syntax_elements.md index 4e0ebf9d..a20a7f5a 100644 --- a/tests/test_renderers/fixtures/syntax_elements.md +++ b/tests/test_renderers/fixtures/syntax_elements.md @@ -631,17 +631,20 @@ c: a - 1 + + 1 b - foo + + foo c - {"d": 2} + + {"d": 2} . -------------------------- @@ -758,7 +761,8 @@ other: Something else other - Something else + + Something else . -------------------------- @@ -859,7 +863,8 @@ a = 1 a - 1 + + 1
diff --git a/tests/test_renderers/test_fixtures_docutils.py b/tests/test_renderers/test_fixtures_docutils.py new file mode 100644 index 00000000..3bfaf085 --- /dev/null +++ b/tests/test_renderers/test_fixtures_docutils.py @@ -0,0 +1,58 @@ +"""Test fixture files, using the ``DocutilsRenderer``. + +Note, the output AST is before any transforms are applied. +""" +from pathlib import Path + +import pytest +from markdown_it.utils import read_fixture_file + +from myst_parser.docutils_renderer import DocutilsRenderer, make_document +from myst_parser.main import MdParserConfig, create_md_parser + +FIXTURE_PATH = Path(__file__).parent.joinpath("fixtures") + + +@pytest.mark.parametrize( + "line,title,input,expected", + read_fixture_file(FIXTURE_PATH.joinpath("docutil_roles.md")), + ids=[ + f"{i[0]}-{i[1]}" for i in read_fixture_file(FIXTURE_PATH / "docutil_roles.md") + ], +) +def test_docutils_roles(line, title, input, expected): + """Test output of docutils roles.""" + parser = create_md_parser(MdParserConfig(), DocutilsRenderer) + parser.options["document"] = document = make_document() + parser.render(input) + try: + assert "\n".join( + [ll.rstrip() for ll in document.pformat().splitlines()] + ) == "\n".join([ll.rstrip() for ll in expected.splitlines()]) + except AssertionError: + print(document.pformat()) + raise + + +@pytest.mark.parametrize( + "line,title,input,expected", + read_fixture_file(FIXTURE_PATH.joinpath("docutil_directives.md")), + ids=[ + f"{i[0]}-{i[1]}" + for i in read_fixture_file(FIXTURE_PATH / "docutil_directives.md") + ], +) +def test_docutils_directives(line, title, input, expected): + """Test output of docutils directives.""" + if title.startswith("SKIP"): # line-block directive not yet supported + pytest.skip(title) + parser = create_md_parser(MdParserConfig(), DocutilsRenderer) + parser.options["document"] = document = make_document() + parser.render(input) + try: + assert "\n".join( + [ll.rstrip() for ll in document.pformat().splitlines()] + ) == "\n".join([ll.rstrip() for ll in expected.splitlines()]) + except AssertionError: + print(document.pformat()) + raise diff --git a/tests/test_renderers/test_fixtures.py b/tests/test_renderers/test_fixtures_sphinx.py similarity index 84% rename from tests/test_renderers/test_fixtures.py rename to tests/test_renderers/test_fixtures_sphinx.py index 7771a552..c14d0b85 100644 --- a/tests/test_renderers/test_fixtures.py +++ b/tests/test_renderers/test_fixtures_sphinx.py @@ -1,3 +1,7 @@ +"""Test fixture files, using the ``SphinxRenderer``. + +Note, the output AST is before any transforms are applied. +""" import re from pathlib import Path @@ -60,41 +64,6 @@ def test_directive_options(line, title, input, expected): ) == "\n".join([ll.rstrip() for ll in expected.splitlines()]) -@pytest.mark.parametrize( - "line,title,input,expected", - read_fixture_file(FIXTURE_PATH.joinpath("docutil_roles.md")), - ids=[ - f"{i[0]}-{i[1]}" for i in read_fixture_file(FIXTURE_PATH / "docutil_roles.md") - ], -) -def test_docutils_roles(line, title, input, expected): - document = to_docutils(input) - print(document.pformat()) - assert "\n".join( - [ll.rstrip() for ll in document.pformat().splitlines()] - ) == "\n".join([ll.rstrip() for ll in expected.splitlines()]) - - -@pytest.mark.parametrize( - "line,title,input,expected", - read_fixture_file(FIXTURE_PATH.joinpath("docutil_directives.md")), - ids=[ - f"{i[0]}-{i[1]}" - for i in read_fixture_file(FIXTURE_PATH / "docutil_directives.md") - ], -) -def test_docutils_directives(line, title, input, expected): - # TODO fix skipped directives - # TODO test domain directives - if title.startswith("SKIP"): - pytest.skip(title) - document = to_docutils(input) - print(document.pformat()) - assert "\n".join( - [ll.rstrip() for ll in document.pformat().splitlines()] - ) == "\n".join([ll.rstrip() for ll in expected.splitlines()]) - - @pytest.mark.parametrize( "line,title,input,expected", read_fixture_file(FIXTURE_PATH.joinpath("sphinx_directives.md")),