Add support for npm/composer caret and tilde condition extensions

This commit is contained in:
Dave Hall 2015-10-15 11:41:19 +11:00
parent 2ed3d39c29
commit 5ca781f439
3 changed files with 30 additions and 1 deletions

View File

@ -20,6 +20,7 @@ The project has received contributions from (in alphabetical order):
* Raphaël Barrois <raphael.barrois+semver@polytechnique.org> (https://github.com/rbarrois)
* Michael Hrivnak <mhrivnak@hrivnak.org> (https://github.com/mhrivnak)
* Rick Eyre <rick.eyre@outlook.com> (https://github.com/rickeyre)
* Dave Hall <skwadhd@gmail.com> (https://github.com/skwashd)
Contributor license agreement

View File

@ -392,8 +392,11 @@ class SpecItem(object):
KIND_GTE = '>='
KIND_GT = '>'
KIND_NEQ = '!='
KIND_CARET = '^'
KIND_TILDE = '~'
re_spec = re.compile(r'^(<|<=|==|>=|>|!=)(\d.*)$')
caret = re.escape('^')
re_spec = re.compile(r'^(<|<=|==|>=|>|!=|{}|~)(\d.*)$'.format(caret))
def __init__(self, requirement_string):
kind, spec = self.parse(requirement_string)
@ -436,9 +439,21 @@ class SpecItem(object):
return version > self.spec
elif self.kind == self.KIND_NEQ:
return version != self.spec
elif self.kind == self.KIND_CARET:
return self.caretCompare(version)
elif self.kind == self.KIND_TILDE:
return self.tildeCompare(version)
else: # pragma: no cover
raise ValueError('Unexpected match kind: %r' % self.kind)
def caretCompare(self, version):
max_version = version.next_major()
return version >= self.spec and version < max_version
def tildeCompare(self, version):
max_version = version.next_minor()
return version >= self.spec and version < max_version
def __str__(self):
return '%s%s' % (self.kind, self.spec)

View File

@ -27,6 +27,8 @@ class MatchTestCase(unittest.TestCase):
'>=0.1.2-rc1.3.4',
'==0.1.2+build42-12.2012-01-01.12h23',
'!=0.1.2-rc1.3-14.15+build.2012-01-01.11h34',
'^0.1.2',
'~0.1.2',
]
matches = {
@ -86,6 +88,17 @@ class MatchTestCase(unittest.TestCase):
'0.1.1-rc4',
'0.1.0+12.3',
],
'^0.1.2': [
'0.1.2',
'0.1.2+build4.5',
'0.1.3-rc1.3',
'0.2.0',
],
'~0.1.2': [
'0.1.2',
'0.1.2+build4.5',
'0.1.3-rc1.3',
],
}
def test_invalid(self):