From f830a5a1559cf1259accf4cc32ab869b3e40a883 Mon Sep 17 00:00:00 2001 From: Vlad Emelianov Date: Sun, 10 Nov 2024 03:41:32 +0300 Subject: [PATCH] Rework set release, allow setting local as well --- newversion/cli_parser.py | 14 ++++++-------- newversion/exceptions.py | 10 ++++++++++ newversion/executor.py | 38 ++++++++++++++++++++++++------------- newversion/main.py | 4 ++-- pyproject.toml | 1 - scripts/check.sh | 41 ++++++++++++++++++++++++++++++++++++++++ tests/test_executor.py | 34 ++++++++++++++++++++------------- 7 files changed, 105 insertions(+), 37 deletions(-) create mode 100755 scripts/check.sh diff --git a/newversion/cli_parser.py b/newversion/cli_parser.py index e952df2..0357761 100644 --- a/newversion/cli_parser.py +++ b/newversion/cli_parser.py @@ -55,7 +55,7 @@ class CLINamespace: release: VersionParts increment: int other: Version - value: int + value: str log_level: int package: bool save: bool @@ -81,7 +81,7 @@ def __init__( ) -> None: self._enum_class = type super_choices = choices if choices is not None else list(self._enum_class) - self._is_singular = default is not None and nargs is None + self._is_singular = nargs in (None, "?") if self._is_singular: nargs = "?" @@ -207,7 +207,7 @@ def parse_args(args: Sequence[str]) -> CLINamespace: help="Release type", ) - parser_set = subparsers.add_parser(Commands.SET.value, help="Set release number") + parser_set = subparsers.add_parser(Commands.SET.value, help="Set release value") parser_set.add_argument( "release", type=VersionParts, @@ -216,7 +216,7 @@ def parse_args(args: Sequence[str]) -> CLINamespace: ) parser_set.add_argument( "value", - type=int, + type=str, help="Release number", ) @@ -309,12 +309,10 @@ def parse_args(args: Sequence[str]) -> CLINamespace: return CLINamespace( version=result.version, command=Commands(result.command) if result.command else Commands.UNKNOWN, - release=VersionParts(result.release) - if getattr(result, "release", "") - else VersionParts.MICRO, + release=result.release if getattr(result, "release", "") else VersionParts.MICRO, increment=getattr(result, "increment", 1), other=getattr(result, "other", Version.zero()), - value=getattr(result, "value", 1), + value=getattr(result, "value", ""), log_level=log_level, package=result.package, save=result.save, diff --git a/newversion/exceptions.py b/newversion/exceptions.py index 7b9471c..8e243d9 100644 --- a/newversion/exceptions.py +++ b/newversion/exceptions.py @@ -65,3 +65,13 @@ class ReleaseCannotBeBumpedError(ExecutorError): def __init__(self, release: VersionParts) -> None: self.message = f"Unsupported release {release.value} for bump operation" super().__init__(self.message) + + +class ValueMustBeIntError(ExecutorError): + """ + Value must be integer error. + """ + + def __init__(self, release: VersionParts, value: str) -> None: + self.message = f"Value for {release.value} must be a number, got: {value}" + super().__init__(self.message) diff --git a/newversion/executor.py b/newversion/executor.py index 7f3f83f..69705b6 100644 --- a/newversion/executor.py +++ b/newversion/executor.py @@ -12,6 +12,7 @@ ExecutorError, PackageVersionError, ReleaseCannotBeBumpedError, + ValueMustBeIntError, VersionIsNotStableError, ) from newversion.package_version import PackageVersion @@ -127,7 +128,7 @@ def command_bump(self, release: VersionParts, increment: int) -> Version: raise ReleaseCannotBeBumpedError(release) - def command_set(self, release: VersionParts, value: int) -> Version: + def command_set(self, release: VersionParts, value: str) -> Version: """ Set version part. @@ -138,23 +139,34 @@ def command_set(self, release: VersionParts, value: int) -> Version: Returns: A new Version. """ + if release == VersionParts.LOCAL: + return self.version.replace(local=value) + + if not value.isdigit(): + raise ValueMustBeIntError(release, value) + + value_int = int(value) if release == VersionParts.PRE: return self.version.replace( - alpha=value if self.version.prerelease_type == VersionParts.ALPHA.value else None, - beta=value if self.version.prerelease_type == VersionParts.BETA.value else None, - rc=value if self.version.prerelease_type in {VersionParts.RC.value, None} else None, + alpha=value_int + if self.version.prerelease_type == VersionParts.ALPHA.value + else None, + beta=value_int if self.version.prerelease_type == VersionParts.BETA.value else None, + rc=value_int + if self.version.prerelease_type in {VersionParts.RC.value, None} + else None, ) return self.version.replace( - post=value if release == VersionParts.POST else None, - epoch=value if release == VersionParts.EPOCH else None, - major=value if release == VersionParts.MAJOR else None, - minor=value if release == VersionParts.MINOR else None, - micro=value if release == VersionParts.MICRO else None, - alpha=value if release == VersionParts.ALPHA else None, - beta=value if release == VersionParts.BETA else None, - rc=value if release == VersionParts.RC else None, - dev=value if release == VersionParts.DEV else None, + post=value_int if release == VersionParts.POST else None, + epoch=value_int if release == VersionParts.EPOCH else None, + major=value_int if release == VersionParts.MAJOR else None, + minor=value_int if release == VersionParts.MINOR else None, + micro=value_int if release == VersionParts.MICRO else None, + alpha=value_int if release == VersionParts.ALPHA else None, + beta=value_int if release == VersionParts.BETA else None, + rc=value_int if release == VersionParts.RC else None, + dev=value_int if release == VersionParts.DEV else None, ) def command_stable(self) -> Version: diff --git a/newversion/main.py b/newversion/main.py index 30caad5..71309d0 100644 --- a/newversion/main.py +++ b/newversion/main.py @@ -74,8 +74,8 @@ def main_cli() -> None: logger = setup_logging(config.log_level) try: output = main_api(config) - except CLIError: - logger.exception("Error") + except CLIError as e: + logger.error(e) # noqa: TRY400 sys.exit(1) if output: diff --git a/pyproject.toml b/pyproject.toml index 0f1f267..a598b7b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -131,7 +131,6 @@ select = [ "PL", # pylint ] ignore = [ - "D107", # undocumented-public-init "D200", # fits-on-one-line "D203", # one-blank-line-before-class diff --git a/scripts/check.sh b/scripts/check.sh new file mode 100755 index 0000000..0b0a6f0 --- /dev/null +++ b/scripts/check.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +set -e + +newversion # 0.0.0 +newversion bump major # 1.0.0 + +# get package version from pyproject.toml, setup.cfg or setup.py +newversion -p # 1.2.3 +newversion -p bump # 1.2.4 +newversion -p bump pre # 1.2.4rc1 +newversion -p get minor # 2 + +echo "1.2.3rc1" | newversion bump micro # 1.2.3 +echo "1.2.3rc1" | newversion bump minor # 1.3.0 +echo "1.2.3rc1" | newversion bump major # 2.0.0 +echo "1.2.3rc1" | newversion bump pre # 1.2.3rc2 +echo "1.2.3rc1" | newversion bump rc # 1.2.3rc2 +echo "1.2.3rc1" | newversion bump alpha # 1.2.4a1 + +echo "1.2.3rc1" | newversion set micro 5 # 1.2.5rc1 +echo "1.2.3rc1" | newversion set minor 5 # 1.5.3rc1 +echo "1.2.3rc1" | newversion set major 5 # 5.2.3rc1 +echo "1.2.3rc1" | newversion set pre 5 # 1.2.3rc5 +echo "1.2.3rc1" | newversion set rc 5 # 1.2.3rc5 +echo "1.2.3rc1" | newversion set alpha 5 # 1.2.3a5 + +echo "1.2.3rc1" | newversion get micro # 1 +echo "1.2.3rc1" | newversion get minor # 2 +echo "1.2.3rc1" | newversion get major # 3 +echo "1.2.3rc1" | newversion get pre # rc1 +echo "1.2.3rc1" | newversion get rc # 1 +echo "1.2.3rc1" | newversion get alpha # 0 + +echo "1.2.3rc1" | newversion stable # 1.2.3 + +echo "1.2.3rc1" | newversion is_stable || echo "error!" +echo "1.2.3" | newversion is_stable # 1.2.3 +echo "1.2.3" | newversion is_stable || echo "error!" + +echo "1.2.3rc1" | newversion gt "1.2.3" || echo "error!" +echo "1.2.3rc1" | newversion lte "1.2.3" # "1.2.3rc1" \ No newline at end of file diff --git a/tests/test_executor.py b/tests/test_executor.py index b6f583f..7acb4ec 100644 --- a/tests/test_executor.py +++ b/tests/test_executor.py @@ -62,25 +62,33 @@ def test_command_compare(self) -> None: Executor(Version("1.2.3")).command_compare("ne", Version("1.2.3")) def test_command_set(self) -> None: - assert Executor(Version("1.2.3")).command_set(VersionParts.MAJOR, 3) == Version("3.2.3") - assert Executor(Version("1.2.3")).command_set(VersionParts.MINOR, 3) == Version("1.3.3") - assert Executor(Version("1.2.3")).command_set(VersionParts.MICRO, 4) == Version("1.2.4") - assert Executor(Version("1.2.3")).command_set(VersionParts.PRE, 4) == Version("1.2.3rc4") - assert Executor(Version("1.2.3rc2")).command_set(VersionParts.PRE, 4) == Version("1.2.3rc4") - assert Executor(Version("1.2.3a2")).command_set(VersionParts.PRE, 4) == Version("1.2.3a4") - assert Executor(Version("1.2.3b2")).command_set(VersionParts.PRE, 4) == Version("1.2.3b4") - assert Executor(Version("1.2.3")).command_set(VersionParts.EPOCH, 1234) == Version( + assert Executor(Version("1.2.3")).command_set(VersionParts.MAJOR, "3") == Version("3.2.3") + assert Executor(Version("1.2.3")).command_set(VersionParts.MINOR, "3") == Version("1.3.3") + assert Executor(Version("1.2.3")).command_set(VersionParts.MICRO, "4") == Version("1.2.4") + assert Executor(Version("1.2.3")).command_set(VersionParts.PRE, "4") == Version("1.2.3rc4") + assert Executor(Version("1.2.3rc2")).command_set(VersionParts.PRE, "4") == Version( + "1.2.3rc4" + ) + assert Executor(Version("1.2.3a2")).command_set(VersionParts.PRE, "4") == Version("1.2.3a4") + assert Executor(Version("1.2.3b2")).command_set(VersionParts.PRE, "4") == Version("1.2.3b4") + assert Executor(Version("1.2.3")).command_set(VersionParts.EPOCH, "1234") == Version( "1234!1.2.3" ) - assert Executor(Version("1.2.3")).command_set(VersionParts.ALPHA, 4) == Version("1.2.3a4") - assert Executor(Version("1.2.3")).command_set(VersionParts.BETA, 4) == Version("1.2.3b4") - assert Executor(Version("1.2.3")).command_set(VersionParts.RC, 4) == Version("1.2.3rc4") - assert Executor(Version("1.2.3")).command_set(VersionParts.POST, 5) == Version( + assert Executor(Version("1.2.3")).command_set(VersionParts.ALPHA, "4") == Version("1.2.3a4") + assert Executor(Version("1.2.3")).command_set(VersionParts.BETA, "4") == Version("1.2.3b4") + assert Executor(Version("1.2.3")).command_set(VersionParts.RC, "4") == Version("1.2.3rc4") + assert Executor(Version("1.2.3")).command_set(VersionParts.POST, "5") == Version( "1.2.3.post5" ) - assert Executor(Version("1.2.3+local")).command_set(VersionParts.DEV, 0) == Version( + assert Executor(Version("1.2.3+local")).command_set(VersionParts.DEV, "0") == Version( "1.2.3.dev0+local" ) + assert Executor(Version("1.2.3")).command_set(VersionParts.LOCAL, "newloc") == Version( + "1.2.3+newloc" + ) + assert Executor(Version("1.2.3.dev0+local")).command_set( + VersionParts.LOCAL, "newloc" + ) == Version("1.2.3.dev0+newloc") def test_command_bump(self) -> None: assert Executor(Version("1.2.3")).command_bump(VersionParts.MAJOR, 3) == Version("4.0.0")