~reviewer/hello-hut

9625423548ea198632914da4967098aa06d119af — Felix Brandt 4 months ago 80fead3
Add --count to repeat the greeting

Implements: https://todo.hut.md/~reviewer/hello-hut/2
2 files changed, 31 insertions(+), 1 deletions(-)

M src/hello_hut/cli.py
A tests/test_cli.py
M src/hello_hut/cli.py => src/hello_hut/cli.py +7 -1
@@ 18,6 18,8 @@ def build_parser():
                        help="print the greeting in capitals")
    parser.add_argument("--time-of-day", action="store_true",
                        help="say good morning, afternoon or evening")
    parser.add_argument("--count", type=int, default=1, metavar="N",
                        help="repeat the greeting N times (default: 1)")
    parser.add_argument("--version", action="version",
                        version=f"%(prog)s {__version__}")
    return parser


@@ 28,8 30,12 @@ def main(argv=None):
    config = load_config()
    name = args.name or config.get("name", DEFAULT_NAME)
    shout = args.shout or bool(config.get("shout", False))
    if args.count < 1:
        print("hello-hut: --count must be at least 1", file=sys.stderr)
        return 2

    salutation = time_of_day_greeting() if args.time_of_day else None
    message = greet(name, shout=shout, salutation=salutation)
    print(message)
    for _ in range(args.count):
        print(message)
    return 0

A tests/test_cli.py => tests/test_cli.py +24 -0
@@ 0,0 1,24 @@
import io
import unittest
from contextlib import redirect_stdout

from hello_hut.cli import main


def run(*argv):
    out = io.StringIO()
    with redirect_stdout(out):
        code = main(list(argv))
    return code, out.getvalue()


class CliTest(unittest.TestCase):
    def test_count(self):
        code, out = run("Ada", "--count", "3")
        self.assertEqual(code, 0)
        self.assertEqual(out.splitlines(), ["Hello, Ada!"] * 3)

    def test_count_must_be_positive(self):
        code, out = run("Ada", "--count", "0")
        self.assertEqual(code, 2)
        self.assertEqual(out, "")