Skip to content

Commit

Permalink
Rework set release, allow setting local as well
Browse files Browse the repository at this point in the history
  • Loading branch information
vemel committed Nov 10, 2024
1 parent da3cccd commit f830a5a
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 37 deletions.
14 changes: 6 additions & 8 deletions newversion/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CLINamespace:
release: VersionParts
increment: int
other: Version
value: int
value: str
log_level: int
package: bool
save: bool
Expand All @@ -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 = "?"

Expand Down Expand Up @@ -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,
Expand All @@ -216,7 +216,7 @@ def parse_args(args: Sequence[str]) -> CLINamespace:
)
parser_set.add_argument(
"value",
type=int,
type=str,
help="Release number",
)

Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions newversion/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
38 changes: 25 additions & 13 deletions newversion/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ExecutorError,
PackageVersionError,
ReleaseCannotBeBumpedError,
ValueMustBeIntError,
VersionIsNotStableError,
)
from newversion.package_version import PackageVersion
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions newversion/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ select = [
"PL", # pylint
]
ignore = [

"D107", # undocumented-public-init
"D200", # fits-on-one-line
"D203", # one-blank-line-before-class
Expand Down
41 changes: 41 additions & 0 deletions scripts/check.sh
Original file line number Diff line number Diff line change
@@ -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"
34 changes: 21 additions & 13 deletions tests/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit f830a5a

Please sign in to comment.