Verified Commit d53ae4d9 authored by David Runge's avatar David Runge
Browse files

argparse: Extend to show version and making project optional

arch_release_promotion/argparse.py:
Change `ArgParseFactory.__init__()` to allow overriding the program name
add a `-V`/`--version` argument which may be used to trigger showing the
version of the program.
Change `ArgParseFactory.promote()` to replace the required `name`
argument with the optional `-p`/`--project` argument and adding an
optional `-r`/`--release` argument which may be used to override the
release version to promote. Print program name and version information
and exit early if version information is requested.

tests/test_argparse.py:
Change tests to accomodate for the new optional arguments.

arch_release_promotion/cli.py:
Replace the use of `args.name` with `args.project`.
parent 51631209
import argparse
from importlib import metadata
from sys import exit
class ArgParseFactory:
......@@ -11,14 +13,20 @@ class ArgParseFactory:
"""
def __init__(self, description: str = "default") -> None:
self.parser = argparse.ArgumentParser(description=description)
def __init__(self, prog: str = "program", description: str = "default") -> None:
self.parser = argparse.ArgumentParser(prog=prog, description=description)
self.parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="verbose output",
)
self.parser.add_argument(
"-V",
"--version",
action="store_true",
help="version information",
)
@classmethod
def promote(self) -> argparse.ArgumentParser:
......@@ -30,12 +38,32 @@ class ArgParseFactory:
An ArgumentParser instance specific for promotion
"""
instance = self(description="Download release artifacts from a project and promote them")
instance = self(
prog="arch-release-promotion",
description="Download release artifacts from a project and promote them",
)
instance.parser.add_argument(
"-p",
"--project",
type=self.non_zero_string,
help=(
"the project on a remote to sign (e.g. 'group/project'). "
f"By default {instance.parser.prog} attempts to promote releases for "
"all projects specified in its config"
),
)
instance.parser.add_argument(
"name",
"-r",
"--release",
type=self.non_zero_string,
help="the project on a remote to sign (e.g. 'group/project')",
help=(
"the release of a project to sign (e.g. '0.1.0'). "
f"By default {instance.parser.prog} requires user input to select a release."
),
)
if instance.parser.parse_args().version:
print(f"{instance.parser.prog} {metadata.version('arch_release_promotion')}")
exit(0)
return instance.parser
......@@ -60,5 +88,5 @@ class ArgParseFactory:
"""
if len(input_) < 1:
raise argparse.ArgumentTypeError("the provided project name can not be zero")
raise argparse.ArgumentTypeError("the provided string can not be empty")
return input_
......@@ -14,7 +14,7 @@ from arch_release_promotion import (
def main() -> None:
args = argparse.ArgParseFactory.promote().parse_args()
project = config.Projects().get_project(name=args.name)
project = config.Projects().get_project(name=args.project)
settings = config.Settings()
upstream = gitlab.Upstream(
url=settings.GITLAB_URL,
......
from argparse import ArgumentParser, ArgumentTypeError
from contextlib import nullcontext as does_not_raise
from typing import ContextManager
from unittest.mock import Mock, call, patch
from pytest import mark, raises
......@@ -11,8 +12,16 @@ def test_argparse_argparsefactory() -> None:
assert isinstance(argparse.ArgParseFactory(), argparse.ArgParseFactory)
def test_argparse_promote() -> None:
assert isinstance(argparse.ArgParseFactory().promote(), ArgumentParser)
@patch("argparse.ArgumentParser.parse_args")
@patch("arch_release_promotion.argparse.exit")
@patch("arch_release_promotion.argparse.metadata")
def test_argparse_promote(metadata_mock: Mock, exit_mock: Mock, parse_args_mock: Mock) -> None:
assert isinstance(argparse.ArgParseFactory.promote(), ArgumentParser)
assert call.version("arch_release_promotion") in metadata_mock.mock_calls
exit_mock.assert_called_once()
parse_args_mock.return_value = Mock(version=False)
assert isinstance(argparse.ArgParseFactory.promote(), ArgumentParser)
@mark.parametrize(
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment