Skip to content

Allow single char version slugs. #1407

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 1 commit into from
Jul 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions readthedocs/builds/version_slug.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
# Regex breakdown:
# [a-z0-9] -- start with alphanumeric value
# [-._a-z0-9] -- allow dash, dot, underscore, digit, lowercase ascii
# +? -- allow multiple of those, but be not greedy about the matching
# *? -- allow multiple of those, but be not greedy about the matching
# (?: ... ) -- wrap everything so that the pattern cannot escape when used in
# regexes.
VERSION_SLUG_REGEX = '(?:[a-z0-9][-._a-z0-9]+?)'
VERSION_SLUG_REGEX = '(?:[a-z0-9][-._a-z0-9]*?)'
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe the final ? is not required here. * should catch none/any number of instances of allowed characters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My initial intend for adding the "non-greedy" matcher was that the regex does not leak into some other bits of a URL regex when it is embedded into one. But, yeah, I think it won't make any difference in real use. Shall I remove it?

Copy link
Contributor

Choose a reason for hiding this comment

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

This makes sense, I figured there were some other cases to consider here. Should be fine leaving it



class VersionSlugField(models.CharField):
Expand Down
20 changes: 20 additions & 0 deletions readthedocs/rtd_tests/tests/test_version_slug.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
import re
from django.test import TestCase

from builds.models import Version
from builds.version_slug import VersionSlugField
from builds.version_slug import VERSION_SLUG_REGEX
from projects.models import Project


class VersionSlugPatternTests(TestCase):
pattern = re.compile('^{pattern}$'.format(pattern=VERSION_SLUG_REGEX))

def test_single_char(self):
self.assertTrue(self.pattern.match('v'))
self.assertFalse(self.pattern.match('.'))

def test_trailing_punctuation(self):
self.assertTrue(self.pattern.match('with_'))
self.assertTrue(self.pattern.match('with.'))
self.assertTrue(self.pattern.match('with-'))
self.assertFalse(self.pattern.match('with!'))

def test_multiple_words(self):
self.assertTrue(self.pattern.match('release-1.0'))
self.assertTrue(self.pattern.match('fix_this-and-that.'))


class VersionSlugFieldTests(TestCase):
fixtures = ["eric", "test_data"]

Expand Down