diff --git a/python/tank/util/version.py b/python/tank/util/version.py index 158cdece7..e21c88ac3 100644 --- a/python/tank/util/version.py +++ b/python/tank/util/version.py @@ -183,6 +183,29 @@ def normalize_version_format(version: str) -> str: return version +def format_version_range(min_version, max_version=None): + """ + Format a version range as a human-readable string. + + :param str min_version: The minimum version of the range. + :param str max_version: The maximum version of the range, or None for open-ended. + :returns: A formatted string such as ">=1.2.3" or ">=1.2.3, <2.0.0". + :rtype: str + """ + min_version = str(min_version) + + if max_version is None: + return ">=%s" % min_version + + if is_version_newer_or_equal(min_version, max_version): + raise ValueError( + "min_version (%s) must be less than max_version (%s)" + % (min_version, max_version) + ) + + return ">=%s, <%s" % (min_version, max_version) + + def _compare_versions(a, b): """ Tests if version a is newer than version b. diff --git a/tests/util_tests/test_format_version_range.py b/tests/util_tests/test_format_version_range.py new file mode 100644 index 000000000..145415efd --- /dev/null +++ b/tests/util_tests/test_format_version_range.py @@ -0,0 +1,24 @@ +# Copyright (c) 2024 Shotgun Software Inc. +# +# CONFIDENTIAL AND PROPRIETARY +# +# This work is provided "AS IS" and subject to the Shotgun Pipeline Toolkit +# Source Code License included in this distribution package. See LICENSE. +# By accessing, using, copying or modifying this work you indicate your +# agreement to the Shotgun Pipeline Toolkit Source Code License. All rights +# not expressly granted therein are reserved by Shotgun Software Inc. + +from tank_test.tank_test_base import setUpModule # noqa +from tank_test.tank_test_base import ShotgunTestBase +from tank.util.version import format_version_range + + +class TestFormatVersionRange(ShotgunTestBase): + def setUp(self): + super().setUp() + + def test_open_ended_range(self): + self.assertEqual(format_version_range("1.2.3"), ">=1.2.3") + + def test_bounded_range(self): + self.assertEqual(format_version_range("1.2.3", "2.0.0"), ">=1.2.3, <2.0.0")