Skip to content

Handle .x in version sorting #6012

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 3 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions readthedocs/projects/version_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ def parse_version_failsafe(version_string):
else:
uni_version = version_string

final_form = ''

try:
normalized_version = unicodedata.normalize('NFKD', uni_version)
ascii_version = normalized_version.encode('ascii', 'ignore')
final_form = ascii_version.decode('ascii')
return Version(final_form)
except (UnicodeError, InvalidVersion):
return None
except InvalidVersion:
# Handle the special case of 1.x, 2.x or 1.0.x, 1.1.x
if final_form and '.x' in final_form:
return parse_version_failsafe(final_form.replace('.x', '.0'))
except UnicodeError:
pass

return None


def comparable_version(version_string):
Expand Down
67 changes: 67 additions & 0 deletions readthedocs/rtd_tests/tests/projects/test_version_sorting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from django.test import TestCase
from django_dynamic_fixture import get

from readthedocs.builds.constants import BRANCH
from readthedocs.builds.models import Version
from readthedocs.projects.models import Project
from readthedocs.projects.templatetags.projects_tags import sort_version_aware


class SortVersionsTest(TestCase):

def setUp(self):
self.project = get(Project)

def test_basic_sort(self):
identifiers = ['1.0', '2.0', '1.1', '1.9', '1.10']
for identifier in identifiers:
get(
Version,
project=self.project,
type=BRANCH,
identifier=identifier,
verbose_name=identifier,
slug=identifier,
)

versions = list(Version.objects.filter(project=self.project))
self.assertEqual(
['latest', '2.0', '1.10', '1.9', '1.1', '1.0'],
[v.slug for v in sort_version_aware(versions)],
)

def test_sort_wildcard(self):
identifiers = ['1.0.x', '2.0.x', '1.1.x', '1.9.x', '1.10.x']
for identifier in identifiers:
get(
Version,
project=self.project,
type=BRANCH,
identifier=identifier,
verbose_name=identifier,
slug=identifier,
)

versions = list(Version.objects.filter(project=self.project))
self.assertEqual(
['latest', '2.0.x', '1.10.x', '1.9.x', '1.1.x', '1.0.x'],
[v.slug for v in sort_version_aware(versions)],
)

def test_sort_alpha(self):
identifiers = ['banana', 'apple', 'carrot']
for identifier in identifiers:
get(
Version,
project=self.project,
type=BRANCH,
identifier=identifier,
verbose_name=identifier,
slug=identifier,
)

versions = list(Version.objects.filter(project=self.project))
self.assertEqual(
['latest', 'carrot', 'banana', 'apple'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that this is not a behavior that you changed, but I'd like to sort alphanumeric versions from A to Z instead at some point. Feels more human-readable when you have a many of these.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any examples of projects that use something like this? I can think of Ubuntu off the top of my head, but that's about it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test-builds, which may not apply as a real project but shows the sorting problem when building alphanumeric branch names

[v.slug for v in sort_version_aware(versions)],
)