File: test_include_directive.py

package info (click to toggle)
myst-parser 4.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,348 kB
  • sloc: python: 7,402; xml: 1,383; makefile: 33; sh: 25
file content (52 lines) | stat: -rw-r--r-- 1,561 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
import os
from io import StringIO
from pathlib import Path

import pytest
from docutils.core import publish_doctree

from myst_parser.parsers.docutils_ import Parser

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


@pytest.mark.param_file(FIXTURE_PATH / "mock_include.md")
def test_render(file_params, tmp_path, monkeypatch):
    monkeypatch.chdir(tmp_path)

    tmp_path.joinpath("other.md").write_text("a\nb\nc")
    tmp_path.joinpath("fmatter.md").write_text("---\na: 1\n---\nb")

    doctree = publish_doctree(
        file_params.content,
        parser=Parser(),
        settings_overrides={"myst_highlight_code_blocks": False},
    )

    doctree["source"] = "tmpdir/test.md"
    output = doctree.pformat().replace(str(tmp_path) + os.sep, "tmpdir/").rstrip()

    file_params.assert_expected(output, rstrip=True)


@pytest.mark.param_file(FIXTURE_PATH / "mock_include_errors.md")
def test_errors(file_params, tmp_path, monkeypatch):
    if file_params.title.startswith("Non-existent path") and os.name == "nt":
        pytest.skip("tmp_path not converted correctly on Windows")

    monkeypatch.chdir(tmp_path)

    tmp_path.joinpath("bad.md").write_text("{a}`b`")

    report_stream = StringIO()
    publish_doctree(
        file_params.content,
        source_path=str(tmp_path / "test.md"),
        parser=Parser(),
        settings_overrides={"halt_level": 6, "warning_stream": report_stream},
    )

    file_params.assert_expected(
        report_stream.getvalue().replace(str(tmp_path) + os.sep, "tmpdir/"),
        rstrip=True,
    )