Skip to content

Commit 37f9806

Browse files
authored
Version comparing: sort 1.x versions last (#10326)
Fixes #10046
1 parent a7926d1 commit 37f9806

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

readthedocs/projects/version_handling.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33

44
from packaging.version import InvalidVersion, Version
55

6-
from readthedocs.builds.constants import (
7-
LATEST_VERBOSE_NAME,
8-
STABLE_VERBOSE_NAME,
9-
TAG,
10-
)
6+
from readthedocs.builds.constants import LATEST_VERBOSE_NAME, STABLE_VERBOSE_NAME, TAG
117
from readthedocs.vcs_support.backends import backend_cls
128

139

@@ -41,7 +37,9 @@ def parse_version_failsafe(version_string):
4137
except InvalidVersion:
4238
# Handle the special case of 1.x, 2.x or 1.0.x, 1.1.x
4339
if final_form and '.x' in final_form:
44-
return parse_version_failsafe(final_form.replace('.x', '.0'))
40+
# Replace the .x with .999999 so it's sorted last.
41+
final_form = final_form.replace(".x", ".999999")
42+
return parse_version_failsafe(final_form)
4543
except UnicodeError:
4644
pass
4745

readthedocs/rtd_tests/tests/projects/test_version_sorting.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,19 @@ def test_basic_sort(self):
3737
)
3838

3939
def test_sort_wildcard(self):
40-
identifiers = ['1.0.x', '2.0.x', '1.1.x', '1.9.x', '1.10.x']
40+
identifiers = [
41+
"1.0.x",
42+
"2.0.x",
43+
"1.1.x",
44+
"1.9.x",
45+
"1.10.x",
46+
"1.0.0",
47+
"1.0.1",
48+
"2.1.0",
49+
"2.1.99",
50+
"1.11.0",
51+
"1.10.8",
52+
]
4153
for identifier in identifiers:
4254
get(
4355
Version,
@@ -50,7 +62,20 @@ def test_sort_wildcard(self):
5062

5163
versions = list(Version.objects.filter(project=self.project))
5264
self.assertEqual(
53-
['latest', '2.0.x', '1.10.x', '1.9.x', '1.1.x', '1.0.x'],
65+
[
66+
"latest",
67+
"2.1.99",
68+
"2.1.0",
69+
"2.0.x",
70+
"1.11.0",
71+
"1.10.x",
72+
"1.10.8",
73+
"1.9.x",
74+
"1.1.x",
75+
"1.0.x",
76+
"1.0.1",
77+
"1.0.0",
78+
],
5479
[v.slug for v in sort_version_aware(versions)],
5580
)
5681

0 commit comments

Comments
 (0)