diff --git a/asciifx/__main__.py b/asciifx/__main__.py index f222c90..257bc97 100644 --- a/asciifx/__main__.py +++ b/asciifx/__main__.py @@ -1,16 +1,29 @@ -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 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 +67,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 +113,8 @@ def main() -> None: effective_height, output_path, ) + return 0 -main() +if __name__ == "__main__": + sys.exit(main(argv=sys.argv[1:])) 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"