File: test_docutils.py

package info (click to toggle)
myst-nb 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,452 kB
  • sloc: python: 6,066; xml: 1,434; makefile: 33
file content (84 lines) | stat: -rw-r--r-- 2,711 bytes parent folder | download
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
"""Run parsing tests against the docutils parser."""

from io import StringIO
import json
from pathlib import Path

from docutils.core import publish_doctree, publish_string
import pytest
import sphinx
import yaml

from myst_nb.docutils_ import Parser

FIXTURE_PATH = Path(__file__).parent.joinpath("nb_fixtures")


@pytest.mark.param_file(FIXTURE_PATH / "basic.txt")
def test_basic(file_params):
    """Test basic parsing."""
    if (
        "Footnote definitions defined in different cells" in file_params.title
        and sphinx.version_info[0] < 5
    ):
        pytest.skip("footnote definition ids changes")
    dct = yaml.safe_load(file_params.content)
    dct.update({"nbformat": 4, "nbformat_minor": 4})
    dct.setdefault("metadata", {})
    dct["metadata"].setdefault(
        "kernelspec", {"name": "python3", "display_name": "Python 3", "language": ""}
    )
    report_stream = StringIO()
    doctree = publish_doctree(
        json.dumps(dct),
        parser=Parser(),
        settings_overrides={
            "nb_execution_mode": "off",
            "nb_output_folder": "",
            "myst_all_links_external": True,
            "warning_stream": report_stream,
        },
    )
    assert report_stream.getvalue().rstrip() == ""

    file_params.assert_expected(doctree.pformat(), rstrip=True)


@pytest.mark.param_file(FIXTURE_PATH / "reporter_warnings.txt")
def test_reporting(file_params):
    """Test that warnings and errors are reported as expected."""
    dct = yaml.safe_load(file_params.content)
    dct.update({"metadata": {}, "nbformat": 4, "nbformat_minor": 4})
    report_stream = StringIO()
    publish_doctree(
        json.dumps(dct),
        parser=Parser(),
        settings_overrides={
            "nb_execution_mode": "off",
            "nb_output_folder": "",
            "warning_stream": report_stream,
        },
    )
    file_params.assert_expected(report_stream.getvalue(), rstrip=True)


def test_html_resources(tmp_path):
    """Test HTML resources are correctly output."""
    report_stream = StringIO()
    result = publish_string(
        json.dumps({"cells": [], "metadata": {}, "nbformat": 4, "nbformat_minor": 4}),
        parser=Parser(),
        writer_name="html",
        settings_overrides={
            "nb_execution_mode": "off",
            "nb_output_folder": str(tmp_path),
            "warning_stream": report_stream,
            "output_encoding": "unicode",
            "embed_stylesheet": False,
        },
    )
    assert report_stream.getvalue().rstrip() == ""
    assert "mystnb.css" in result
    assert "pygments.css" in result
    assert tmp_path.joinpath("mystnb.css").is_file()
    assert tmp_path.joinpath("pygments.css").is_file()