From d6e739da09b0add93460464a84ba144757a8d195 Mon Sep 17 00:00:00 2001 From: Wes Turner Date: Thu, 1 Oct 2020 11:27:29 -0400 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20cli.parse:=20return?= =?UTF-8?q?=200=20for=20non-error=20#53?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- markdown_it/cli/parse.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/markdown_it/cli/parse.py b/markdown_it/cli/parse.py index 7d3ebb01..6774be95 100644 --- a/markdown_it/cli/parse.py +++ b/markdown_it/cli/parse.py @@ -21,7 +21,8 @@ def main(args: Optional[Sequence[str]] = None) -> bool: convert(namespace.filenames) else: interactive() - return True + RET_OK = 0 + return RET_OK def convert(filenames: Iterable[str]) -> None: From b0bc12eab93c7581cdeb8d1cbaf81bb86f4eeadd Mon Sep 17 00:00:00 2001 From: Wes Turner Date: Thu, 1 Oct 2020 11:40:04 -0400 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=A7=AA=20TEST:=20fix=20and=20add=20a?= =?UTF-8?q?=20CLI=20returncode=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_cli.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index af0dcd58..1761f68e 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2,6 +2,8 @@ import tempfile from unittest.mock import patch +import pytest + from markdown_it.cli import parse @@ -9,7 +11,12 @@ 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): + parse.main(["/tmp/nonexistant_path/for_cli_test.md"]) def test_print_heading(): From 3900ae1c5007bcf78041d3874d1c82256ce90aec Mon Sep 17 00:00:00 2001 From: Wes Turner <50891+westurner@users.noreply.github.com> Date: Sat, 24 Oct 2020 11:52:56 -0400 Subject: [PATCH 3/5] TEST: tests/test_cli.py: check SysyemExit exc_info return code Co-authored-by: Taneli Hukkinen --- tests/test_cli.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 1761f68e..57d6b938 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -15,8 +15,9 @@ def test_parse(): def test_parse_fail(): - with pytest.raises(SystemExit): + 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(): From 797347ce7b621957e8300ebb4a2836b7944f7d75 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Mon, 15 Feb 2021 12:49:06 +0200 Subject: [PATCH 4/5] Fix test_parse_fail --- markdown_it/cli/parse.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/markdown_it/cli/parse.py b/markdown_it/cli/parse.py index 6774be95..270c9841 100644 --- a/markdown_it/cli/parse.py +++ b/markdown_it/cli/parse.py @@ -15,14 +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() - RET_OK = 0 - return RET_OK + return 0 def convert(filenames: Iterable[str]) -> None: @@ -39,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: From 3ecbddc83b14ddf736ac5a70b872dab03918df9c Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Mon, 15 Feb 2021 12:52:21 +0200 Subject: [PATCH 5/5] Fix exit code when running "python markdown_it/cli/parse.py" --- markdown_it/cli/parse.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/markdown_it/cli/parse.py b/markdown_it/cli/parse.py index 270c9841..31d88d74 100644 --- a/markdown_it/cli/parse.py +++ b/markdown_it/cli/parse.py @@ -104,4 +104,5 @@ def print_heading() -> None: if __name__ == "__main__": - main(sys.argv[1:]) + exit_code = main(sys.argv[1:]) + sys.exit(exit_code)