diff --git a/galactory/utilities.py b/galactory/utilities.py index 9d9af38..8a993a1 100644 --- a/galactory/utilities.py +++ b/galactory/utilities.py @@ -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 = {} diff --git a/tests/unit/utilities/test_utilities.py b/tests/unit/utilities/test_utilities.py new file mode 100644 index 0000000..acbfa26 --- /dev/null +++ b/tests/unit/utilities/test_utilities.py @@ -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