Skip to content
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

feat: add --version #43

Merged
merged 1 commit into from
Jun 17, 2024
Merged
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
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())
Loading