Skip to content

fix: treat 1.0 and 1 as the same for excel users #4543

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

Merged
merged 9 commits into from
Dec 18, 2024
18 changes: 18 additions & 0 deletions cve_bin_tool/version_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ def version_compare(v1: str, v2: str):
if v1_array[i] in pre_release_words:
return -1

# special edge case for folk editing version info in excel
# who may lose the trailing .0 in versions like 1.0
try:
if int(v1_array[i]) == 0 and len(v1_array) == len(v2_array) + 1:
return 0

except ValueError:
return 1

# Otherwise, v1 has more digits than v2 and the previous ones matched,
# so it's probably later. e.g. 1.2.3 amd 1.2.q are both > 1.2
return 1
Expand All @@ -150,6 +159,15 @@ def version_compare(v1: str, v2: str):
if v2_array[len(v1_array)] in pre_release_words:
return 1

# special edge case for folk editing version info in excel
# who may lose the trailing .0 in versions like 1.0
try:
if int(v2_array[len(v1_array)]) == 0 and len(v2_array) == len(v1_array) + 1:
return 0

except ValueError:
return -1

return -1

return 0
Expand Down
9 changes: 9 additions & 0 deletions test/test_version_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def test_eq(self):
assert Version("4.4.A") == Version("4.4.a")
assert Version("5.6 ") == Version("5.6")
assert Version("f835f2caaa") == Version("f835f2caaa")
assert Version("42.0") == Version(
"42"
) # edge case for folk editing versions in excel
assert Version("1") == Version(
"1.0"
) # edge case for folk editing versions in excel

def test_lt(self):
"""Make sure < works between versions, including some with unusual version schemes"""
Expand Down Expand Up @@ -75,3 +81,6 @@ def test_ne(self):
"""Test some != cases with hashes to make sure we aren't comparing the string 'HASH'"""
assert Version("f835f2caab") != Version("f835f2caaa")
assert Version("HASH") != Version("f835f2caaa")
assert Version("1") != Version(
"1.0.0"
) # the edge case for excel only works on single .0
Loading