From 6972272fc4095967ef8117dd7014a9189f2b46c0 Mon Sep 17 00:00:00 2001 From: Wes Turner Date: Thu, 23 Jan 2025 14:24:52 -0500 Subject: [PATCH 1/2] TST,BLD,BUG: if __name__=='__main__': __main__.main(argv) and tests fix asciifx console_script so that `pip -e .` works, tests for main(argv=["--help"]) --- asciifx/__main__.py | 13 ++++++++----- asciifx/tests/__init__.py | 0 asciifx/tests/test_main.py | 37 +++++++++++++++++++++++++++++++++++++ pyproject.toml | 2 +- 4 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 asciifx/tests/__init__.py create mode 100644 asciifx/tests/test_main.py diff --git a/asciifx/__main__.py b/asciifx/__main__.py index f222c90..a0a142d 100644 --- a/asciifx/__main__.py +++ b/asciifx/__main__.py @@ -10,7 +10,7 @@ def create_parser() -> ArgumentParser: parser = ArgumentParser( - prog="ascii-fx", + prog="asciifx", description="Turn a Python script into a simulated interactive session. The " "resulting asciicast is written to the current working directory by default.", ) @@ -54,16 +54,16 @@ def create_parser() -> ArgumentParser: return parser -def main() -> None: +def main(argv=None) -> int: parser = create_parser() - options = parser.parse_args() + options = parser.parse_args(argv) if options.verbose: konsole.config(level=konsole.DEBUG) if not options.title: now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - options.title = f'Created by ascii-fx on {now} from "{options.input}"' + options.title = f'Created by asciifx on {now} from "{options.input}"' try: input_path = Path(options.input).resolve() @@ -100,6 +100,9 @@ def main() -> None: effective_height, output_path, ) + return 0 -main() +if __name__ == "__main__": + import sys + main(argv=sys.argv) diff --git a/asciifx/tests/__init__.py b/asciifx/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/asciifx/tests/test_main.py b/asciifx/tests/test_main.py new file mode 100644 index 0000000..430b081 --- /dev/null +++ b/asciifx/tests/test_main.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +import contextlib +import io +import unittest + +from asciifx.__main__ import main + +class TestMain(unittest.TestCase): + main_args = [ + ['--help'], + ] + def test_main_help(self): + for argv in self.main_args: + with self.subTest(argv=argv): + _stderr = io.StringIO() + _stdout = io.StringIO() + with contextlib.redirect_stderr(_stderr): + with contextlib.redirect_stdout(_stdout): + with self.assertRaises(SystemExit) as exc: + _ = main(argv=argv) + stdout = _stdout.getvalue() + stderr = _stderr.getvalue() + assert '--help' in stdout, (stdout, stderr) + assert not stderr, (stderr) + assert exc.exception.code == 0, (exc.exception.code) + + +import pytest + +@pytest.mark.parametrize('argv', TestMain.main_args) +def test_main_help(argv, capsys): + with pytest.raises(SystemExit) as exc: + _ = main(argv=['asciifx', *argv]) + captured = capsys.readouterr() + assert exc.value.code == 0 + assert '--help' in captured.out, captured + assert not captured.err, captured \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 3911802..8ea2c15 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,7 @@ dev = [ ] [project.scripts] -asciifx = "asciifx" +asciifx = "asciifx.__main__:main" [project.urls] Home = "https://github.com/apparebit/asciifx" From f9483c80582a7ec560760edc108779f2efe1e163 Mon Sep 17 00:00:00 2001 From: Wes Turner Date: Thu, 23 Jan 2025 14:37:58 -0500 Subject: [PATCH 2/2] BUG,CLN,DOC: sys.exit(main(sys.argv[1:])), sort imports, module docstring --- asciifx/__main__.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/asciifx/__main__.py b/asciifx/__main__.py index a0a142d..257bc97 100644 --- a/asciifx/__main__.py +++ b/asciifx/__main__.py @@ -1,9 +1,22 @@ -import konsole +#!/usr/bin/env python3 +""" +The main(argv) function for asciifx. + +Usage:: + + python -m asciifx --help + asciifx --help + python ./__main__.py --help + +""" +import sys from argparse import ArgumentParser from datetime import datetime from pathlib import Path +import konsole + from .animator import InvalidPragma from .perform import perform @@ -104,5 +117,4 @@ def main(argv=None) -> int: if __name__ == "__main__": - import sys - main(argv=sys.argv) + sys.exit(main(argv=sys.argv[1:]))