Skip to content

Commit

Permalink
add helper to compare collection versions with prerelease awareness
Browse files Browse the repository at this point in the history
  • Loading branch information
briantist committed Sep 29, 2023
1 parent 52c4744 commit 09d3b93
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions galactory/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ def discover_collections(repo, namespace=None, name=None, version=None, fast_det
yield coldata


def _latest_collection_version(reference_collection: dict, difference_collection: dict, *, property: str = "semver"):
"""Returns the latest of two collections."""
if (reference_collection[property].prerelease is None) == (difference_collection[property].prerelease is None):
return max(reference_collection, difference_collection, key=lambda x: x[property])

return reference_collection if reference_collection[property].prerelease is None else difference_collection


def collected_collections(repo, namespace=None, name=None, scheme=None):
collections = {}

Expand Down
38 changes: 38 additions & 0 deletions tests/unit/utilities/test_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
# (c) 2023 Brian Scholer (@briantist)

import pytest
from unittest import mock

from datetime import datetime, timedelta
from types import GeneratorType
import semver

from galactory.utilities import _latest_collection_version


@pytest.mark.parametrize(["ref", "dif", "exp"], [
("0.0.0-dev0", "0.0.0-dev1", "0.0.0-dev1"),
("0.0.0", "0.0.0-dev1", "0.0.0"),
("1.2.3", "1.2.4", "1.2.4"),
("0.0.0-dev0", "0.0.0-dev1", "0.0.0-dev1"),
("0.0.0", "99.99.99-dev1", "0.0.0"),
])
@pytest.mark.parametrize("prop", [None, "semver", "other"])
def test_latest_collection_version(ref, dif, exp, prop):
reference = semver.VersionInfo.parse(ref)
difference = semver.VersionInfo.parse(dif)
expected = ref if exp == ref else dif

opprop = {}
if prop is None:
expprop = "semver"
else:
opprop["property"] = expprop = prop

# ensure we didn't mess up the test cases
assert expected in [reference, difference]

result = _latest_collection_version({expprop: reference}, {expprop: difference}, **opprop)

assert result[expprop] == expected

0 comments on commit 09d3b93

Please sign in to comment.