Skip to content

Commit

Permalink
feat: add --version
Browse files Browse the repository at this point in the history
  • Loading branch information
mikix committed Jun 17, 2024
1 parent e31ceaf commit 264e409
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
9 changes: 8 additions & 1 deletion chart_review/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down
10 changes: 8 additions & 2 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 22 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 264e409

Please sign in to comment.