-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #287 from Cray-HPE/CRAYSAT-1551
CRAYSAT-1551: Fix sorting of product versions in sat showrev --products
- Loading branch information
Showing
7 changed files
with
230 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ venv | |
|
||
nosetests.xml | ||
.vscode/ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# | ||
# MIT License | ||
# | ||
# (C) Copyright 2024 Hewlett Packard Enterprise Development LP | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a | ||
# copy of this software and associated documentation files (the "Software"), | ||
# to deal in the Software without restriction, including without limitation | ||
# the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
# and/or sell copies of the Software, and to permit persons to whom the | ||
# Software is furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included | ||
# in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
# OTHER DEALINGS IN THE SOFTWARE. | ||
# | ||
""" | ||
Class for representing a semver version | ||
""" | ||
from semver import Version | ||
|
||
|
||
class LooseVersion: | ||
"""A LooseVersion representing a version that may be semver or may not. | ||
This class is used to compare versions that may or may not comply with the | ||
semver format. If the version does not comply with the semver format, then | ||
the MAX_VERSION is used as the comparison version, which results in the | ||
version being considered greater than any other version. | ||
Args: | ||
version_str (str): The string representation of the version. | ||
Attributes: | ||
version_str (str): The string representation of the version. | ||
comparison_version (semver.version.Version): The semver.version.Version | ||
object of either the version_str or the MAX_VERSION if semver fails to | ||
parse the version | ||
""" | ||
|
||
MAX_VERSION = "99999999999.99999999999.99999999999" | ||
|
||
def __init__(self, version_str): | ||
"""Creates a new LooseVersion object from the given product_version string. | ||
Args: | ||
version_str (str): The string representation of the version. | ||
""" | ||
self.version_str = version_str | ||
self.comparison_version = self.parse_version(version_str) | ||
|
||
def __str__(self): | ||
return f'{self.version_str}' | ||
|
||
def __repr__(self): | ||
return f"{self.__class__.__name__}('{self.version_str}')" | ||
|
||
def __lt__(self, other): | ||
return self.comparison_version < other.comparison_version | ||
|
||
def __le__(self, other): | ||
return self.comparison_version <= other.comparison_version | ||
|
||
def __eq__(self, other): | ||
return (isinstance(self, type(other)) and | ||
self.comparison_version == other.comparison_version) | ||
|
||
def __gt__(self, other): | ||
return self.comparison_version > other.comparison_version | ||
|
||
def __ge__(self, other): | ||
return self.comparison_version >= other.comparison_version | ||
|
||
def parse_version(self, version_str): | ||
"""Parse the version string into a semver.version.Version object if possible. | ||
Args: | ||
version_str (str): The string representation of the version. | ||
Returns: | ||
semver.version.Version: The semver.version.Version object of either | ||
the version_str or the MAX_VERSION if semver fails to parse the | ||
version | ||
""" | ||
|
||
try: | ||
parsed_version = Version.parse(version_str) | ||
except ValueError: | ||
parsed_version = Version.parse(self.MAX_VERSION) | ||
|
||
return parsed_version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# | ||
# MIT License | ||
# | ||
# (C) Copyright 2024 Hewlett Packard Enterprise Development LP | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a | ||
# copy of this software and associated documentation files (the "Software"), | ||
# to deal in the Software without restriction, including without limitation | ||
# the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
# and/or sell copies of the Software, and to permit persons to whom the | ||
# Software is furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included | ||
# in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
# OTHER DEALINGS IN THE SOFTWARE. | ||
# | ||
""" | ||
Tests for the LooseVersion utility class. | ||
""" | ||
|
||
import unittest | ||
|
||
from sat.loose_version import LooseVersion | ||
|
||
|
||
class TestLooseVersion(unittest.TestCase): | ||
"""Tests for the XName class""" | ||
|
||
def setUp(self): | ||
self.examples = [ | ||
"1.0.0", | ||
"1.0.0", | ||
"1.0.2", | ||
"2.1.3", | ||
"20240129105721.3d243a4b5120", | ||
"1.9.16-20240522115029.beaa1ce7a544", | ||
"1.9.18-20240625143747.59e8b16343aa", | ||
"2.1.1-64-cos-3.0-aarch64", | ||
"23.10.30-20231027", | ||
"2.2.0-57-cos-3.0-x86-64" | ||
] | ||
|
||
def test_version_str(self): | ||
"""Test version_str property stores unmodified version string""" | ||
for version_str in self.examples: | ||
self.assertEqual(LooseVersion(version_str).version_str, version_str) | ||
|
||
def test_bad_version_str(self): | ||
"""Test that bad version string evaluates to max value""" | ||
bad_version_str = "1" | ||
good_version_str = "2.0.0" | ||
self.assertLess(LooseVersion(good_version_str), LooseVersion(bad_version_str)) | ||
|
||
def test_eq(self): | ||
"""Test __eq__ for LooseVersion.""" | ||
for version_str in self.examples: | ||
self.assertEqual(LooseVersion(version_str), LooseVersion(version_str)) | ||
|
||
def test_lt(self): | ||
"""Test __lt__ for LooseVersion.""" | ||
self.assertLess(LooseVersion('1.0.0'), LooseVersion('1.0.1')) | ||
|
||
def test_le(self): | ||
"""Test __le__ for LooseVersion.""" | ||
self.assertLessEqual(LooseVersion('1.0.0'), LooseVersion('1.0.1')) | ||
self.assertLessEqual(LooseVersion('1.0.1'), LooseVersion('1.0.1')) | ||
|
||
def test_gt(self): | ||
"""Test __gt__ for LooseVersion.""" | ||
self.assertGreater(LooseVersion('1.0.1'), LooseVersion('1.0.0')) | ||
|
||
def test_ge(self): | ||
"""Test __ge__ for LooseVersion.""" | ||
self.assertGreaterEqual(LooseVersion('1.0.0'), LooseVersion('1.0.0')) | ||
self.assertGreaterEqual(LooseVersion('1.0.1'), LooseVersion('1.0.0')) | ||
|
||
def test_repr(self): | ||
"""Test __repr__ for LooseVersion.""" | ||
for version_str in self.examples: | ||
self.assertEqual(repr(LooseVersion(version_str)), "LooseVersion('{}')".format(version_str)) | ||
|
||
def test_str(self): | ||
for version_str in self.examples: | ||
self.assertEqual(str(LooseVersion(version_str)), version_str) |