File: test_cli.py

package info (click to toggle)
python-readme-renderer 44.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 728 kB
  • sloc: python: 414; sh: 23; makefile: 6
file content (69 lines) | stat: -rw-r--r-- 2,355 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import pathlib
import pytest
from readme_renderer.__main__ import main
import tempfile
from unittest import mock


@pytest.fixture(params=["test_CommonMark_001.md", "test_rst_003.rst",
                        "test_GFM_001.md", "test_txt_001.txt"])
def input_file(request):
    path = pathlib.Path("tests/fixtures", request.param)
    # Skip markdown tests if the cmarkgfm optional dependency is not installed.
    if path.suffix == ".md":
        pytest.importorskip("cmarkgfm")
    return path


@pytest.mark.parametrize("output_file", [False, True])
def test_cli_input_file(input_file, output_file):
    with mock.patch("builtins.print") as print_:
        if output_file:
            with tempfile.TemporaryDirectory() as tmpdir:
                output = pathlib.Path(tmpdir) / "output.html"
                main(["-o", str(output), str(input_file)])
                with output.open() as fp:
                    result = fp.read()
        else:
            main([str(input_file)])

    print_.assert_called_once()
    (result,), kwargs = print_.call_args

    with input_file.with_suffix(".html").open() as fp:
        expected = fp.read()
    assert result.strip() == expected.strip()

    if output_file:
        assert kwargs["file"].name == str(output)


def test_cli_invalid_format():
    with mock.patch("pathlib.Path.open"), \
            pytest.raises(ValueError, match="invalid README format: invalid"):
        main(["no-file.invalid"])


def test_cli_explicit_format(input_file):
    fmt = input_file.suffix.lstrip(".")
    with input_file.open() as fp, \
            mock.patch("pathlib.Path.open", return_value=fp), \
            mock.patch("builtins.print") as print_:
        main(["-f", fmt, "no-file.invalid"])
        print_.assert_called_once()
        (result,), _ = print_.call_args

    with input_file.with_suffix(".html").open() as fp:
        assert result.strip() == fp.read().strip()


@pytest.mark.parametrize("package, contains", [
    ("readme_renderer", "Readme Renderer is a library that will safely render"),
    ("docutils", "Docutils is a modular system for processing documentation"),
])
def test_cli_package(package, contains):
    with mock.patch("builtins.print") as print_:
        main(["-p", package])
        print_.assert_called_once()
        (result,), _ = print_.call_args
    assert contains in result