diff --git a/tests/data/template/docs/index.md b/tests/data/template/docs/index.md new file mode 100644 index 0000000..a88dcd3 --- /dev/null +++ b/tests/data/template/docs/index.md @@ -0,0 +1,3 @@ +# Test + +$code_block diff --git a/tests/data/template/mkdocs.yml b/tests/data/template/mkdocs.yml new file mode 100644 index 0000000..18e88e6 --- /dev/null +++ b/tests/data/template/mkdocs.yml @@ -0,0 +1,8 @@ +site_name: Test pages for mkdocs-kroki-plugin + +theme: + name: material + +plugins: + - kroki: + FencePrefix: '' diff --git a/tests/test_fences.py b/tests/test_fences.py new file mode 100644 index 0000000..7b1c46e --- /dev/null +++ b/tests/test_fences.py @@ -0,0 +1,186 @@ +import pytest + +from tests.utils import MkDocsTemplateHelper + +TEST_CASES = { + "#35": """ +```` plantuml + stuff containing ``` +```` +""", + "https://spec.commonmark.org/0.31.2/#example-119": """ +``` mermaid +< + > +``` +""", + "https://spec.commonmark.org/0.31.2/#example-120": """ +~~~ mermaid +< + > +~~~ +""", + "https://spec.commonmark.org/0.31.2/#example-122": """ +``` mermaid +aaa +~~~ +``` +""", + "https://spec.commonmark.org/0.31.2/#example-123": """ +~~~ mermaid +aaa +``` +~~~ +""", + "https://spec.commonmark.org/0.31.2/#example-125": """ +~~~~ mermaid +aaa +~~~ +~~~~ +""", + "https://spec.commonmark.org/0.31.2/#example-129": """ +``` mermaid + + +``` +""", + "https://spec.commonmark.org/0.31.2/#example-130": """ +``` mermaid +``` +""", +} + +TEST_CASES_NOT_SUPPORTED = { + "https://spec.commonmark.org/0.31.2/#example-121": """ +`` mermaid +foo +`` +""", + "https://spec.commonmark.org/0.31.2/#example-124": """ +```` mermaid +aaa +``` +`````` +""", + "https://spec.commonmark.org/0.31.2/#example-126": """ +``` mermaid +""", + "https://spec.commonmark.org/0.31.2/#example-127": """ +````` mermaid + +``` +aaa +""", + "https://spec.commonmark.org/0.31.2/#example-128": """ +> ``` mermaid +> aaa + +bbb +""", + "https://spec.commonmark.org/0.31.2/#example-131": """ + ``` mermaid + aaa +aaa +``` +""", + "https://spec.commonmark.org/0.31.2/#example-132": """ + ``` +aaa + aaa +aaa + ``` +""", + "https://spec.commonmark.org/0.31.2/#example-133": """ + ``` + aaa + aaa + aaa + ``` +""", + "https://spec.commonmark.org/0.31.2/#example-135": """ +``` +aaa + ``` +""", + "https://spec.commonmark.org/0.31.2/#example-136": """ + ``` +aaa + ``` +""", + "https://spec.commonmark.org/0.31.2/#example-140": """ +foo +``` +bar +``` +baz +""", + "https://spec.commonmark.org/0.31.2/#example-141": """ +foo +--- +~~~ +bar +~~~ +# baz +""", + "https://spec.commonmark.org/0.31.2/#example-146": """ +~~~ aa ``` ~~~ +foo +~~~ +""", + "https://spec.commonmark.org/0.31.2/#example-147": """ +``` +``` aaa +``` +""", +} + +TEST_CASES_ESCAPED = { + "https://spec.commonmark.org/0.31.2/#example-134": """ + ``` + aaa + ``` +""", +} + + +@pytest.mark.parametrize("test_code_block", [pytest.param(v, id=k) for k, v in TEST_CASES.items()]) +@pytest.mark.usefixtures("kroki_dummy") +def test_fences(test_code_block) -> None: + with MkDocsTemplateHelper(test_code_block) as mkdocs_helper: + mkdocs_helper.set_http_method("POST") + result = mkdocs_helper.invoke_build() + + assert result.exit_code == 0 + image_files = list((mkdocs_helper.test_dir / "site").glob("*.svg")) + assert len(image_files) == 1 + + +@pytest.mark.parametrize( + "test_code_block", + [pytest.param(v, id=k) for k, v in TEST_CASES_NOT_SUPPORTED.items()] + + [pytest.param(v, id=k) for k, v in TEST_CASES_ESCAPED.items()], +) +@pytest.mark.usefixtures("kroki_dummy") +def test_fences_not_supported(test_code_block) -> None: + with MkDocsTemplateHelper(test_code_block) as mkdocs_helper: + mkdocs_helper.set_http_method("POST") + result = mkdocs_helper.invoke_build() + + assert result.exit_code == 0 + image_files = list((mkdocs_helper.test_dir / "site").glob("*.svg")) + assert len(image_files) == 0 + + +@pytest.mark.usefixtures("kroki_dummy") +def test_pandoc_fenced_code_blocks() -> None: + with MkDocsTemplateHelper("""~~~~~~~~~~~~~~~~ mermaid +~~~~~~~~~~ +code including tildes +~~~~~~~~~~ +~~~~~~~~~~~~~~~~""") as mkdocs_helper: + mkdocs_helper.set_http_method("POST") + result = mkdocs_helper.invoke_build() + + assert result.exit_code == 0 + image_files = list((mkdocs_helper.test_dir / "site").glob("*.svg")) + assert len(image_files) == 1 diff --git a/tests/test_whitespace.py b/tests/test_whitespace.py deleted file mode 100644 index c92cb3d..0000000 --- a/tests/test_whitespace.py +++ /dev/null @@ -1,25 +0,0 @@ -from pathlib import Path - -import pytest - -from tests.utils import MkDocsHelper - - -def _set_whitespace_after_fenced_block(test_dir: Path): - with open(test_dir / "docs/index.md") as in_file: - file_content = in_file.read() - with open(test_dir / "docs/index.md", "w") as out_file: - out_file.write(file_content.replace("```c4plantuml", "``` c4plantuml")) - - -@pytest.mark.usefixtures("kroki_dummy") -def test_whitespace_in_fence_prefix() -> None: - with MkDocsHelper("happy_path") as mkdocs_helper: - _set_whitespace_after_fenced_block(mkdocs_helper.test_dir) - mkdocs_helper.set_http_method("POST") - mkdocs_helper.set_fence_prefix(" ") - result = mkdocs_helper.invoke_build() - - assert result.exit_code == 0 - image_files = list((mkdocs_helper.test_dir / "site").glob("*.svg")) - assert len(image_files) == 1 diff --git a/tests/utils.py b/tests/utils.py index ac6ceeb..ff26812 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,10 +1,10 @@ -import logging import os import pathlib import shutil import tempfile from contextlib import AbstractContextManager from pathlib import Path +from string import Template from typing import Literal import yaml @@ -13,8 +13,6 @@ from tests.compat import chdir -logging.basicConfig(level=logging.INFO) - class NoPluginEntryError(ValueError): def __init__(self) -> None: @@ -26,6 +24,7 @@ def __init__(self, test_case: str) -> None: self.config_file = None self.test_case = test_case self.test_dir = Path(tempfile.mkdtemp()) + self._copy_test_case() def _copy_test_case(self) -> None: # equals to `../data`, using current source file as a pin @@ -41,7 +40,6 @@ def _dump_config(self) -> None: yaml.safe_dump(self.config_file, file) def __enter__(self) -> "MkDocsHelper": - self._copy_test_case() self._load_config() return self @@ -69,3 +67,15 @@ def invoke_build(self) -> Result: runner = CliRunner() with chdir(self.test_dir): return runner.invoke(build_command) + + +class MkDocsTemplateHelper(MkDocsHelper): + def _substitute_code_block(self, code_block: str): + with open(self.test_dir / "docs/index.md") as in_file: + file_content = Template(in_file.read()) + with open(self.test_dir / "docs/index.md", "w") as out_file: + out_file.write(file_content.substitute(code_block=code_block)) + + def __init__(self, code_block: str) -> None: + super().__init__("template") + self._substitute_code_block(code_block)