Skip to content

TST,BLD,BUG: if __name__=='__main__': __main__.main(argv) and tests #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: boss
Choose a base branch
from
Open
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
27 changes: 21 additions & 6 deletions asciifx/__main__.py
Original file line number Diff line number Diff line change
@@ -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.",
)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -100,6 +113,8 @@ def main() -> None:
effective_height,
output_path,
)
return 0


main()
if __name__ == "__main__":
sys.exit(main(argv=sys.argv[1:]))
Empty file added asciifx/tests/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions asciifx/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dev = [
]

[project.scripts]
asciifx = "asciifx"
asciifx = "asciifx.__main__:main"

[project.urls]
Home = "https://github.com/apparebit/asciifx"
Expand Down