diff --git a/chart_review/cli.py b/chart_review/cli.py index bcac523..88a71de 100644 --- a/chart_review/cli.py +++ b/chart_review/cli.py @@ -3,14 +3,21 @@ import argparse import sys +import chart_review from chart_review.commands import accuracy, info def define_parser() -> argparse.ArgumentParser: """Fills out an argument parser with all the CLI options.""" parser = argparse.ArgumentParser() - subparsers = parser.add_subparsers(required=True) + parser.add_argument( + "--version", + action="version", + version=f"chart-review {chart_review.__version__}", + ) + + subparsers = parser.add_subparsers(required=True) accuracy.make_subparser(subparsers.add_parser("accuracy")) info.make_subparser(subparsers.add_parser("info")) diff --git a/tests/base.py b/tests/base.py index 640f10c..28927fa 100644 --- a/tests/base.py +++ b/tests/base.py @@ -19,7 +19,13 @@ def setUp(self): @staticmethod def run_cli(*args) -> str: - stdout = io.StringIO() - with contextlib.redirect_stdout(stdout): + with TestCase.capture_stdout() as stdout: cli.main_cli(list(args)) return stdout.getvalue() + + @staticmethod + @contextlib.contextmanager + def capture_stdout() -> io.StringIO: + stdout = io.StringIO() + with contextlib.redirect_stdout(stdout): + yield stdout diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..f8fab0a --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,22 @@ +"""Tests for cli.py""" + +import chart_review +from chart_review import cli +from tests import base + + +class TestCommandLine(base.TestCase): + """Test case for the CLI entry point""" + + def test_version(self): + # Manually capture stdout (rather than helper self.run_cli) because --version actually + # exits the program, and we have to handle the exception rather than just grabbing the + # return value which would be more convenient. + with self.capture_stdout() as stdout: + with self.assertRaises(SystemExit) as cm: + cli.main_cli(["--version"]) + + self.assertEqual(0, cm.exception.code) + + version = chart_review.__version__ + self.assertEqual(f"chart-review {version}\n", stdout.getvalue())