From 086449bcb112c7e5ce522aefa6e93627c4b1af0f Mon Sep 17 00:00:00 2001 From: Amethyst Reese Date: Thu, 18 Apr 2024 20:44:08 -0700 Subject: [PATCH] Add new smoke tests to run converter and mdinclude directive with docutils (#48) --- makefile | 3 +- sphinx_mdinclude/tests/__init__.py | 1 + sphinx_mdinclude/tests/__main__.py | 2 +- sphinx_mdinclude/tests/test.md | 2 +- sphinx_mdinclude/tests/test.rst | 6 +-- sphinx_mdinclude/tests/test_smoke.py | 78 ++++++++++++++++++++++++++++ 6 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 sphinx_mdinclude/tests/test_smoke.py diff --git a/makefile b/makefile index 0aa0790..9b17eb7 100644 --- a/makefile +++ b/makefile @@ -29,8 +29,9 @@ test: deps: python -m pessimist --requirements= -c 'python -m sphinx_mdinclude.tests' . +.PHONY: html html: .venv README.md docs/*.md docs/conf.py - source .venv/bin/activate && sphinx-build -b html docs html + source .venv/bin/activate && sphinx-build -ab html docs html clean: rm -rf build dist html *.egg-info .mypy_cache diff --git a/sphinx_mdinclude/tests/__init__.py b/sphinx_mdinclude/tests/__init__.py index 26e7eea..b27f936 100644 --- a/sphinx_mdinclude/tests/__init__.py +++ b/sphinx_mdinclude/tests/__init__.py @@ -12,3 +12,4 @@ TestRestCode, TestTable, ) +from .test_smoke import SmokeTest diff --git a/sphinx_mdinclude/tests/__main__.py b/sphinx_mdinclude/tests/__main__.py index 31adf5c..ebc15e6 100644 --- a/sphinx_mdinclude/tests/__main__.py +++ b/sphinx_mdinclude/tests/__main__.py @@ -4,4 +4,4 @@ import unittest if __name__ == "__main__": - unittest.main("sphinx_mdinclude.tests", verbosity=1) + unittest.main("sphinx_mdinclude.tests", verbosity=2) diff --git a/sphinx_mdinclude/tests/test.md b/sphinx_mdinclude/tests/test.md index da66e08..21aaf51 100644 --- a/sphinx_mdinclude/tests/test.md +++ b/sphinx_mdinclude/tests/test.md @@ -2,7 +2,7 @@ ## SubTitle -__content__ +**content** ## サブタイトル diff --git a/sphinx_mdinclude/tests/test.rst b/sphinx_mdinclude/tests/test.rst index e394821..e2fb935 100644 --- a/sphinx_mdinclude/tests/test.rst +++ b/sphinx_mdinclude/tests/test.rst @@ -20,6 +20,6 @@ Also within parentheses (:math:`x^2`) and (:math:`z^3`). .. code-block:: python - class Foo: - def bar(self, arg: str = "") -> int: - return len(arg) + class Foo: + def bar(self, arg: str = "") -> int: + return len(arg) diff --git a/sphinx_mdinclude/tests/test_smoke.py b/sphinx_mdinclude/tests/test_smoke.py new file mode 100644 index 0000000..d4d5039 --- /dev/null +++ b/sphinx_mdinclude/tests/test_smoke.py @@ -0,0 +1,78 @@ +# Copyright Amethyst Reese +# Licensed under the MIT License + +import platform +import unittest +from dataclasses import dataclass, field +from pathlib import Path +from textwrap import dedent + +from docutils.frontend import get_default_settings +from docutils.parsers.rst import directives, Parser +from docutils.utils import new_document + +from ..render import convert +from ..sphinx import MdInclude + +TEST_MD = Path(__file__).parent / "test.md" +TEST_RST = Path(__file__).parent / "test.rst" + + +@dataclass +class FakeConfig: + no_underscore_emphasis: bool = False + md_parse_relative_links: bool = False + md_anonymous_references: bool = False + md_disable_inline_math: bool = False + + +@dataclass +class FakeEnv: + config: FakeConfig = field(default_factory=FakeConfig) + + +class SmokeTest(unittest.TestCase): + maxDiff = None + + @unittest.skipIf( + platform.system() == "Windows", + "inconsistent column widths on Windows", + ) + def test_convert(self): + content = TEST_MD.read_text() + expected = TEST_RST.read_text() + + result = convert(content) + self.assertEqual(expected, result) + + def test_mdinclude_basic(self): + content = dedent( + f""" + .. mdinclude:: {TEST_MD} + + """ + ) + expected = dedent( + """\ + +
+ + Title + <section ids="subtitle" names="subtitle"> + <title> + SubTitle + <paragraph> + <strong> + content + """ + ) + + directives.register_directive("mdinclude", MdInclude) + parser = Parser() + settings = get_default_settings(Parser) + settings.env = FakeEnv() + document = new_document("smoke.rst", settings.copy()) + parser.parse(content, document) + + result = document.pformat() + self.assertEqual(expected, result[: len(expected)])