Skip to content

Commit

Permalink
Use VersionParts in Executor
Browse files Browse the repository at this point in the history
  • Loading branch information
vemel committed Nov 6, 2024
1 parent 0279640 commit 99f2b4e
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 161 deletions.
7 changes: 4 additions & 3 deletions newversion/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from typing import Any, Optional, Union

from newversion.constants import PACKAGE_NAME, Commands, VersionParts
from newversion.type_defs import ReleaseNonLocalTypeDef
from newversion.version import Version


Expand Down Expand Up @@ -52,7 +51,7 @@ class CLINamespace:

version: Version
command: Commands
release: ReleaseNonLocalTypeDef
release: VersionParts
increment: int
other: Version
value: int
Expand Down Expand Up @@ -299,7 +298,9 @@ def parse_args(args: Sequence[str]) -> CLINamespace:
return CLINamespace(
version=result.version,
command=Commands(result.command) if result.command else Commands.UNKNOWN,
release=result.release if getattr(result, "release", "") else VersionParts.MICRO.value,
release=VersionParts(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),
Expand Down
77 changes: 31 additions & 46 deletions newversion/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from newversion.constants import Commands, VersionParts
from newversion.exceptions import ExecutorError, PackageVersionError
from newversion.package_version import PackageVersion
from newversion.type_defs import OperatorTypeDef, ReleaseNonLocalTypeDef, ReleaseTypeDef
from newversion.type_defs import OperatorTypeDef
from newversion.version import Version


Expand All @@ -26,7 +26,7 @@ def __init__(

def command_get(
self,
release: ReleaseTypeDef,
release: VersionParts,
) -> str:
"""
Get version part.
Expand All @@ -37,58 +37,53 @@ def command_get(
Returns:
Part as a string.
"""
try:
release_part = VersionParts(release)
except ValueError:
raise ExecutorError(f"Unknown release name: {release}") from None

if release_part == VersionParts.LOCAL:
if release == VersionParts.LOCAL:
return self.version.local or ""

if release_part == VersionParts.PRE:
if release == VersionParts.PRE:
return f"{self.version.pre[0]}{self.version.pre[1]}" if self.version.pre else ""

if release_part == VersionParts.POST:
if release == VersionParts.POST:
return str(self.version.post) if self.version.post else "0"

if release_part == VersionParts.DEV:
if release == VersionParts.DEV:
return str(self.version.dev) if self.version.dev else "0"

if release_part == VersionParts.ALPHA:
if release == VersionParts.ALPHA:
return (
str(self.version.pre[-1])
if self.version.pre and self.version.pre[0] == "a"
else "0"
)

if release_part == VersionParts.BETA:
if release == VersionParts.BETA:
return (
str(self.version.pre[-1])
if self.version.pre and self.version.pre[0] == "b"
else "0"
)

if release_part == VersionParts.RC:
if release == VersionParts.RC:
return (
str(self.version.pre[-1])
if self.version.pre and self.version.pre[0] == "rc"
else "0"
)

if release_part == VersionParts.EPOCH:
if release == VersionParts.EPOCH:
return str(self.version.epoch) if self.version.epoch else "0"

if release_part == VersionParts.FULL:
if release == VersionParts.FULL:
return str(self.version)

result = {
VersionParts.MAJOR: self.version.major,
VersionParts.MINOR: self.version.minor,
VersionParts.MICRO: self.version.micro,
}[release_part]
}[release]
return str(result)

def command_bump(self, release: ReleaseNonLocalTypeDef, increment: int) -> Version:
def command_bump(self, release: VersionParts, increment: int) -> Version:
"""
Bump release.
Expand All @@ -99,37 +94,32 @@ def command_bump(self, release: ReleaseNonLocalTypeDef, increment: int) -> Versi
Returns:
A new Version.
"""
try:
release_part = VersionParts(release)
except ValueError:
raise ExecutorError(f"Unknown release name: {release}") from None

if release_part in (
if release in (
VersionParts.MAJOR,
VersionParts.MINOR,
VersionParts.MICRO,
):
return self.version.bump_release(release_part.value, increment)
return self.version.bump_release(release.value, increment)

if release_part == VersionParts.PRE:
if release == VersionParts.PRE:
return self.version.bump_prerelease(increment)

if release_part == VersionParts.POST:
if release == VersionParts.POST:
return self.version.bump_postrelease(increment)

if release_part in (
if release in (
VersionParts.RC,
VersionParts.ALPHA,
VersionParts.BETA,
):
return self.version.bump_prerelease(increment, release_part.value)
return self.version.bump_prerelease(increment, release.value)

if release_part == VersionParts.DEV:
if release == VersionParts.DEV:
return self.version.bump_dev(increment)

raise ExecutorError(f"Unknown release name: {release}")

def command_set(self, release: ReleaseNonLocalTypeDef, value: int) -> Version:
def command_set(self, release: VersionParts, value: int) -> Version:
"""
Set version part.
Expand All @@ -140,12 +130,7 @@ def command_set(self, release: ReleaseNonLocalTypeDef, value: int) -> Version:
Returns:
A new Version.
"""
try:
release_part = VersionParts(release)
except ValueError:
raise ExecutorError(f"Unknown release name: {release}") from None

if release_part == VersionParts.PRE:
if release == VersionParts.PRE:
if self.version.prerelease_type == VersionParts.ALPHA.value:
return self.version.replace(alpha=value)
if self.version.prerelease_type == VersionParts.BETA.value:
Expand All @@ -155,23 +140,23 @@ def command_set(self, release: ReleaseNonLocalTypeDef, value: int) -> Version:

return self.version.replace(rc=value)

if release_part == VersionParts.POST:
if release == VersionParts.POST:
return self.version.replace(post=value)
if release_part == VersionParts.EPOCH:
if release == VersionParts.EPOCH:
return self.version.replace(epoch=value)
if release_part == VersionParts.MAJOR:
if release == VersionParts.MAJOR:
return self.version.replace(major=value)
if release_part == VersionParts.MINOR:
if release == VersionParts.MINOR:
return self.version.replace(minor=value)
if release_part == VersionParts.MICRO:
if release == VersionParts.MICRO:
return self.version.replace(micro=value)
if release_part == VersionParts.ALPHA:
if release == VersionParts.ALPHA:
return self.version.replace(alpha=value)
if release_part == VersionParts.BETA:
if release == VersionParts.BETA:
return self.version.replace(beta=value)
if release_part == VersionParts.RC:
if release == VersionParts.RC:
return self.version.replace(rc=value)
if release_part == VersionParts.DEV:
if release == VersionParts.DEV:
return self.version.replace(dev=value)

raise ExecutorError(f"Unknown release name: {release}") from None
Expand Down
6 changes: 3 additions & 3 deletions newversion/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _replace(self, base: BaseVersion) -> Self:

def bump_release(
self,
release_type: ReleaseMainTypeDef = "micro",
release_type: ReleaseMainTypeDef = VersionParts.MICRO.value,
inc: int = 1,
) -> Self:
"""
Expand Down Expand Up @@ -251,7 +251,7 @@ def bump_dev(
dev_version = self.dev or 0
return self.replace(dev=(dev_version + inc))

if bump_release == "post" or self.is_postrelease:
if bump_release == VersionParts.POST.value or self.is_postrelease:
# this is a postrelease or we want to create one
return self.bump_postrelease().replace(dev=(inc - 1))

Expand All @@ -265,7 +265,7 @@ def bump_prerelease(
self,
inc: int = 1,
release_type: Optional[PrereleaseLooseTypeDef] = None,
bump_release: ReleaseMainTypeDef = "micro",
bump_release: ReleaseMainTypeDef = VersionParts.MICRO.value,
) -> Self:
"""
Get next prerelease version.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ classifiers = [
"Topic :: Software Development :: Libraries :: Python Modules",
]
keywords = ["version", "pep440"]
dependencies = ["packaging>=20.0"]
dependencies = ["packaging>=20.0", "typing_extensions>=4.1.0"]

[project.optional-dependencies]
build = ["setuptools"]
Expand Down
96 changes: 57 additions & 39 deletions tests/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from newversion.constants import VersionParts
from newversion.exceptions import ExecutorError, PackageVersionError
from newversion.executor import Executor
from newversion.package_version import PackageVersion
Expand All @@ -11,22 +12,25 @@
class TestVersion:
def test_command_get(self) -> None:
executor = Executor(Version("1.2.3rc4"))
assert executor.command_get("major") == "1"
assert executor.command_get("minor") == "2"
assert executor.command_get("micro") == "3"
assert executor.command_get("rc") == "4"
assert executor.command_get("pre") == "rc4"
assert executor.command_get("alpha") == "0"
assert executor.command_get("beta") == "0"
assert executor.command_get("post") == "0"
assert executor.command_get("epoch") == "0"
assert executor.command_get("dev") == "0"
assert Executor(Version("1.2.3a4")).command_get("alpha") == "4"
assert Executor(Version("1.2.3b5")).command_get("beta") == "5"
assert Executor(Version("1.2.3.post7")).command_get("post") == "7"
assert Executor(Version("1.2.3.post6.dev2")).command_get("dev") == "2"
assert Executor(Version("1234!1.2.3.post7")).command_get("epoch") == "1234"
assert Executor(Version("1234!1.2.3.post7+localver")).command_get("local") == "localver"
assert executor.command_get(VersionParts.MAJOR) == "1"
assert executor.command_get(VersionParts.MINOR) == "2"
assert executor.command_get(VersionParts.MICRO) == "3"
assert executor.command_get(VersionParts.RC) == "4"
assert executor.command_get(VersionParts.PRE) == "rc4"
assert executor.command_get(VersionParts.ALPHA) == "0"
assert executor.command_get(VersionParts.BETA) == "0"
assert executor.command_get(VersionParts.POST) == "0"
assert executor.command_get(VersionParts.EPOCH) == "0"
assert executor.command_get(VersionParts.DEV) == "0"
assert Executor(Version("1.2.3a4")).command_get(VersionParts.ALPHA) == "4"
assert Executor(Version("1.2.3b5")).command_get(VersionParts.BETA) == "5"
assert Executor(Version("1.2.3.post7")).command_get(VersionParts.POST) == "7"
assert Executor(Version("1.2.3.post6.dev2")).command_get(VersionParts.DEV) == "2"
assert Executor(Version("1234!1.2.3.post7")).command_get(VersionParts.EPOCH) == "1234"
assert (
Executor(Version("1234!1.2.3.post7+localver")).command_get(VersionParts.LOCAL)
== "localver"
)

def test_command_stable(self) -> None:
assert Executor(Version("1.2.3a4")).command_stable() == Version("1.2.3")
Expand All @@ -52,31 +56,45 @@ 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("major", 3) == Version("3.2.3")
assert Executor(Version("1.2.3")).command_set("minor", 3) == Version("1.3.3")
assert Executor(Version("1.2.3")).command_set("micro", 4) == Version("1.2.4")
assert Executor(Version("1.2.3")).command_set("pre", 4) == Version("1.2.3rc4")
assert Executor(Version("1.2.3rc2")).command_set("pre", 4) == Version("1.2.3rc4")
assert Executor(Version("1.2.3a2")).command_set("pre", 4) == Version("1.2.3a4")
assert Executor(Version("1.2.3b2")).command_set("pre", 4) == Version("1.2.3b4")
assert Executor(Version("1.2.3")).command_set("epoch", 1234) == Version("1234!1.2.3")
assert Executor(Version("1.2.3")).command_set("alpha", 4) == Version("1.2.3a4")
assert Executor(Version("1.2.3")).command_set("beta", 4) == Version("1.2.3b4")
assert Executor(Version("1.2.3")).command_set("rc", 4) == Version("1.2.3rc4")
assert Executor(Version("1.2.3")).command_set("post", 5) == Version("1.2.3.post5")
assert Executor(Version("1.2.3+local")).command_set("dev", 0) == Version("1.2.3.dev0+local")
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(
"1.2.3.post5"
)
assert Executor(Version("1.2.3+local")).command_set(VersionParts.DEV, 0) == Version(
"1.2.3.dev0+local"
)

def test_command_bump(self) -> None:
assert Executor(Version("1.2.3")).command_bump("major", 3) == Version("4.0.0")
assert Executor(Version("1.2.3")).command_bump("minor", 3) == Version("1.5.0")
assert Executor(Version("1.2.3")).command_bump("micro", 3) == Version("1.2.6")
assert Executor(Version("1.2.3")).command_bump("pre", 3) == Version("1.2.4rc3")
assert Executor(Version("1.2.3rc1")).command_bump("pre", 3) == Version("1.2.3rc4")
assert Executor(Version("1.2.3rc2")).command_bump("rc", 3) == Version("1.2.3rc5")
assert Executor(Version("1.2.3rc2")).command_bump("alpha", 3) == Version("1.2.4a3")
assert Executor(Version("1.2.3")).command_bump("post", 1) == Version("1.2.3.post1")
assert Executor(Version("1.2.3.post5")).command_bump("post", 1) == Version("1.2.3.post6")
assert Executor(Version("1.2.3")).command_bump("dev", 1) == Version("1.2.4.dev0")
assert Executor(Version("1.2.3")).command_bump(VersionParts.MAJOR, 3) == Version("4.0.0")
assert Executor(Version("1.2.3")).command_bump(VersionParts.MINOR, 3) == Version("1.5.0")
assert Executor(Version("1.2.3")).command_bump(VersionParts.MICRO, 3) == Version("1.2.6")
assert Executor(Version("1.2.3")).command_bump(VersionParts.PRE, 3) == Version("1.2.4rc3")
assert Executor(Version("1.2.3rc1")).command_bump(VersionParts.PRE, 3) == Version(
"1.2.3rc4"
)
assert Executor(Version("1.2.3rc2")).command_bump(VersionParts.RC, 3) == Version("1.2.3rc5")
assert Executor(Version("1.2.3rc2")).command_bump(VersionParts.ALPHA, 3) == Version(
"1.2.4a3"
)
assert Executor(Version("1.2.3")).command_bump(VersionParts.POST, 1) == Version(
"1.2.3.post1"
)
assert Executor(Version("1.2.3.post5")).command_bump(VersionParts.POST, 1) == Version(
"1.2.3.post6"
)
assert Executor(Version("1.2.3")).command_bump(VersionParts.DEV, 1) == Version("1.2.4.dev0")
with pytest.raises(ExecutorError):
Executor(Version("1.2.3.post5")).command_bump("unknown", 1) # type: ignore

Expand Down
Loading

0 comments on commit 99f2b4e

Please sign in to comment.