From 5d966b9ee79a7a907be79f3deface8055148c711 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Wed, 18 Jan 2023 07:36:58 -0500 Subject: [PATCH] test: better test how we use pandoc --- tests/faker.py | 9 +++++++-- tests/test_format_rst.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/tests/faker.py b/tests/faker.py index 1675ba8..75106ed 100644 --- a/tests/faker.py +++ b/tests/faker.py @@ -19,8 +19,13 @@ class FakeRunCommand: def __init__(self, mocker): """Make the faker.""" self.handlers: Dict[str, CmdHandler] = {} - mocker.patch("scriv.gitinfo.run_command", self) - mocker.patch("scriv.shell.run_command", self) + self.mocker = mocker + self.patch_module("scriv.gitinfo") + self.patch_module("scriv.shell") + + def patch_module(self, mod_name: str) -> None: + """Replace ``run_command`` in `mod_name` with our fake.""" + self.mocker.patch(f"{mod_name}.run_command", self) def add_handler(self, argv0: str, handler: CmdHandler) -> None: """ diff --git a/tests/test_format_rst.py b/tests/test_format_rst.py index 7e90f5f..a8004cc 100644 --- a/tests/test_format_rst.py +++ b/tests/test_format_rst.py @@ -1,11 +1,13 @@ """Tests for scriv/format_rst.py.""" import collections +import re import textwrap import pytest from scriv.config import Config +from scriv.exceptions import ScrivException from scriv.format_rst import RstTools @@ -333,3 +335,37 @@ def test_format_sections(sections, expected): def test_format_header(config_kwargs, text, fh_kwargs, result): actual = RstTools(Config(**config_kwargs)).format_header(text, **fh_kwargs) assert actual == result + + +def test_fake_pandoc(fake_run_command): + fake_run_command.patch_module("scriv.format_rst") + expected_args = [ + "pandoc", + "-frst", + "-tmarkdown_strict", + "--markdown-headings=atx", + "--wrap=none", + ] + expected_text = "The converted text!\nis multi-line\n" + + def fake_pandoc(argv): + # We got the arguments we expected, plus one more. + assert argv[: len(expected_args)] == expected_args + assert len(argv) == len(expected_args) + 1 + return (True, expected_text) + + fake_run_command.add_handler("pandoc", fake_pandoc) + assert RstTools().convert_to_markdown("Hello") == expected_text + + +def test_fake_pandoc_failing(fake_run_command): + fake_run_command.patch_module("scriv.format_rst") + error_text = "There was a problem!!?!" + + def fake_pandoc(argv): # pylint: disable=unused-argument + return (False, error_text) + + fake_run_command.add_handler("pandoc", fake_pandoc) + expected = f"Couldn't convert ReST to Markdown: {error_text!r}" + with pytest.raises(ScrivException, match=re.escape(expected)): + _ = RstTools().convert_to_markdown("Hello")