Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions markdown_it/cli/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
version_str = "markdown-it-py [version {}]".format(__version__)


def main(args: Optional[Sequence[str]] = None) -> bool:
def main(args: Optional[Sequence[str]] = None) -> int:
namespace = parse_args(args)
if namespace.filenames:
convert(namespace.filenames)
else:
interactive()
return True
return 0


def convert(filenames: Iterable[str]) -> None:
Expand All @@ -38,7 +38,8 @@ def convert_file(filename: str) -> None:
rendered = MarkdownIt().render(fin.read())
print(rendered, end="")
except OSError:
sys.exit('Cannot open file "{}".'.format(filename))
sys.stderr.write(f'Cannot open file "{filename}".\n')
sys.exit(1)


def interactive() -> None:
Expand Down Expand Up @@ -103,4 +104,5 @@ def print_heading() -> None:


if __name__ == "__main__":
main(sys.argv[1:])
exit_code = main(sys.argv[1:])
sys.exit(exit_code)
10 changes: 9 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@
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)])
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_print_heading():
Expand Down