Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ignore option to get_version and add shortcut #39

Merged
merged 5 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions dunamai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,37 @@ def __eq__(self, other: Any) -> bool:
and self.epoch == other.epoch
)

def equals_ignored_version(self, ignored_version: "Version") -> bool:
"""
Compare this version to another version but ignore None values in the ignored version.
:param ignored_version: The version to compare to.
:return: True if this version equals the ignored version.
"""
if self.base != ignored_version.base:
return False
if ignored_version.stage is not None:
if self.stage != ignored_version.stage:
return False
if ignored_version.revision is not None:
if self.revision != ignored_version.revision:
return False
if ignored_version.distance is not None:
if self.distance != ignored_version.distance:
return False
if ignored_version.commit is not None:
if self.commit != ignored_version.commit:
return False
if ignored_version.dirty is not None:
if self.dirty != ignored_version.dirty:
return False
if ignored_version.tagged_metadata is not None:
if self.tagged_metadata != ignored_version.tagged_metadata:
return False
if ignored_version.epoch is not None:
if self.epoch != ignored_version.epoch:
return False
return True

def __lt__(self, other: Any) -> bool:
if not isinstance(other, Version):
raise TypeError(
Expand Down Expand Up @@ -978,6 +1009,7 @@ def get_version(
first_choice: Callable[[], Optional[Version]] = None,
third_choice: Callable[[], Optional[Version]] = None,
fallback: Version = Version("0.0.0"),
ignore: Sequence[Version] = None,
) -> Version:
"""
Check pkg_resources info or a fallback function to determine the version.
Expand All @@ -990,24 +1022,29 @@ def get_version(
:param third_choice: Callback to determine a version if the installed
package cannot be found by name.
:param fallback: If no other matches found, use this version.
:param ignore: Ignore a found version if it is part of this list. When
comparing the found version to an ignored one, fields with None in the ignored
version are not taken into account.
"""
if first_choice:
first_ver = first_choice()
if first_ver:
if first_ver and not _version_in_list_of_ignored_versions(first_ver, ignore):
return first_ver

try:
import importlib.metadata as ilm
except ImportError:
import importlib_metadata as ilm # type: ignore
try:
return Version(ilm.version(name))
ilm_version = Version(ilm.version(name))
if not _version_in_list_of_ignored_versions(ilm_version, ignore):
return ilm_version
except ilm.PackageNotFoundError:
pass

if third_choice:
third_ver = third_choice()
if third_ver:
if third_ver and not _version_in_list_of_ignored_versions(third_ver, ignore):
return third_ver

return fallback
Expand Down Expand Up @@ -1149,4 +1186,12 @@ def _parse_git_timestamp_iso_strict(raw: str) -> dt.datetime:
return dt.datetime.strptime(compat, "%Y-%m-%dT%H:%M:%S%z")


def _version_in_list_of_ignored_versions(version: Version, ignore: Optional[Sequence[Version]]):
if ignore:
for ignored_version in ignore:
if version.equals_ignored_version(ignored_version):
return True
return False


__version__ = get_version("dunamai").serialize()
61 changes: 60 additions & 1 deletion tests/unit/test_dunamai.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import pkg_resources
import pkg_resources # type: ignore
import re
from contextlib import contextmanager
from pathlib import Path
Expand Down Expand Up @@ -442,6 +442,65 @@ def test__get_version__fallback() -> None:
assert get_version("dunamai_nonexistent_test") == Version("0.0.0")


def test__get_version__from_name__ignore() -> None:
assert (
get_version(
"dunamai",
ignore=[Version(pkg_resources.get_distribution("dunamai").version)],
fallback=Version("2"),
)
== Version("2")
)


def test__get_version__first_choice__ignore() -> None:
assert (
get_version(
"dunamai_nonexistent_test",
first_choice=lambda: Version("1"),
ignore=[Version("1")],
fallback=Version("2"),
)
== Version("2")
)


def test__get_version__first_choice__ignore__with_commit() -> None:
assert (
get_version(
"dunamai_nonexistent_test",
first_choice=lambda: Version("1", commit="aaaa"),
ignore=[Version("1")],
fallback=Version("2"),
)
== Version("2")
)


def test__get_version__first_choice__ignore__without_commit() -> None:
assert (
get_version(
"dunamai_nonexistent_test",
first_choice=lambda: Version("1"),
ignore=[Version("1", commit="aaaa")],
fallback=Version("2"),
)
== Version("1")
)


def test__get_version__third_choice__ignore() -> None:
assert (
get_version(
"dunamai_nonexistent_test",
third_choice=lambda: Version("3"),
ignore=[Version("3")],
fallback=Version("2"),
)
== Version("2")
)


def test__version__from_any_vcs(tmp_path) -> None:
with chdir(tmp_path):
with pytest.raises(RuntimeError):
Expand Down