Skip to content

Commit

Permalink
test: better test how we use pandoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Ned Batchelder committed Jan 18, 2023
1 parent bb242c7 commit 5d966b9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tests/faker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
36 changes: 36 additions & 0 deletions tests/test_format_rst.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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")

0 comments on commit 5d966b9

Please sign in to comment.