forked from executablebooks/markdown-it-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
97 lines (73 loc) · 2.96 KB
/
test_cli.py
File metadata and controls
97 lines (73 loc) · 2.96 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from contextlib import redirect_stdout
import io
import pathlib
import tempfile
from unittest.mock import patch
import pytest
from markdown_it.cli import parse
def test_parse():
with tempfile.TemporaryDirectory() as tempdir:
path = pathlib.Path(tempdir).joinpath("test.md")
path.write_text("a b c")
assert parse.main([str(path)]) == 0
def test_parse_fail():
with pytest.raises(SystemExit) as exc_info:
parse.main(["/tmp/nonexistant_path/for_cli_test.md"])
assert exc_info.value.code == 1
def test_non_utf8():
with tempfile.TemporaryDirectory() as tempdir:
path = pathlib.Path(tempdir).joinpath("test.md")
path.write_bytes(b"\x80abc")
assert parse.main([str(path)]) == 0
def test_print_heading():
with patch("builtins.print") as patched:
parse.print_heading()
patched.assert_called()
def test_interactive():
def mock_input(prompt):
raise KeyboardInterrupt
with patch("builtins.print") as patched, patch("builtins.input", mock_input):
parse.interactive()
patched.assert_called()
def test_main_no_args_is_interactive():
with patch("markdown_it.cli.parse.interactive") as mock_interactive:
assert parse.main([]) == 0
mock_interactive.assert_called_once()
def test_parse_output():
with tempfile.TemporaryDirectory() as tempdir:
path = pathlib.Path(tempdir).joinpath("test.md")
path.write_text("# a b c")
string_io = io.StringIO()
with redirect_stdout(string_io):
assert parse.main([str(path)]) == 0
assert string_io.getvalue() == "<h1>a b c</h1>\n"
def test_stdin():
with patch("sys.stdin", io.StringIO("# a b c")):
string_io = io.StringIO()
with redirect_stdout(string_io):
assert parse.main(["--stdin"]) == 0
assert string_io.getvalue() == "<h1>a b c</h1>\n"
def test_multiple_files():
with tempfile.TemporaryDirectory() as tempdir:
path1 = pathlib.Path(tempdir).joinpath("test1.md")
path1.write_text("# file 1")
path2 = pathlib.Path(tempdir).joinpath("test2.md")
path2.write_text("* file 2")
string_io = io.StringIO()
with redirect_stdout(string_io):
assert parse.main([str(path1), str(path2)]) == 0
assert string_io.getvalue() == "<h1>file 1</h1>\n<ul>\n<li>file 2</li>\n</ul>\n"
def test_interactive_render():
# Simulate user typing '# hello', pressing Ctrl-D (renders), then Ctrl-C (exits)
# This is needed to break the infinite loop in interactive mode on EOF.
mock_input = patch(
"builtins.input", side_effect=["# hello", EOFError, KeyboardInterrupt]
)
string_io = io.StringIO()
with redirect_stdout(string_io), mock_input:
parse.interactive()
output = string_io.getvalue()
assert "markdown-it-py" in output # from print_heading
# The rendered output is prefixed by a newline
assert "\n<h1>hello</h1>\n" in output
assert "Exiting" in output