Skip to content

Commit

Permalink
add resolve_to_specific_version
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor McArthur committed Sep 28, 2017
1 parent cf65b71 commit 5cf1cc7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
31 changes: 28 additions & 3 deletions dbt/semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,18 @@ def is_exact(self):


def reduce_versions(*args):
version_specifiers = [
VersionSpecifier.from_version_string(version_string)
for version_string in args]
version_specifiers = []

for version in args:
if isinstance(version, UnboundedVersionSpecifier):
continue

elif isinstance(version, VersionSpecifier):
version_specifiers.append(version)

else:
version_specifiers.append(
VersionSpecifier.from_version_string(version))

for version_specifier in version_specifiers:
if not isinstance(version_specifier, VersionSpecifier):
Expand All @@ -271,3 +280,19 @@ def versions_compatible(*args):
return True
except VersionsNotCompatibleException as e:
return False

def resolve_to_specific_version(requested_range, available_versions):
max_version = None
max_version_string = None

for version_string in available_versions:
version = VersionSpecifier.from_version_string(version_string)

if(versions_compatible(version,
requested_range.start,
requested_range.end) and
(max_version is None or max_version.compare(version) < 0)):
max_version = version
max_version_string = version_string

return max_version_string
34 changes: 33 additions & 1 deletion test/unit/test_semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from dbt.exceptions import VersionsNotCompatibleException
from dbt.semver import VersionSpecifier, UnboundedVersionSpecifier, \
VersionRange, reduce_versions, versions_compatible
VersionRange, reduce_versions, versions_compatible, \
resolve_to_specific_version

def create_range(start_version_string, end_version_string):
start = UnboundedVersionSpecifier()
Expand Down Expand Up @@ -100,3 +101,34 @@ def test__reduce_versions(self):
self.assertInvalidVersionSet(['<=0.0.3', '>0.0.3'])
self.assertInvalidVersionSet(['<0.0.3', '>=0.0.3'])
self.assertInvalidVersionSet(['<0.0.3', '>0.0.3'])

def test__resolve_to_specific_version(self):
self.assertEqual(
resolve_to_specific_version(
create_range('>0.0.1', None),
['0.0.1', '0.0.2']),
'0.0.2')

self.assertEqual(
resolve_to_specific_version(
create_range('>=0.0.2', None),
['0.0.1', '0.0.2']),
'0.0.2')

self.assertEqual(
resolve_to_specific_version(
create_range('>=0.0.3', None),
['0.0.1', '0.0.2']),
None)

self.assertEqual(
resolve_to_specific_version(
create_range('>=0.0.3', '<0.0.5'),
['0.0.3', '0.0.4', '0.0.5']),
'0.0.4')

self.assertEqual(
resolve_to_specific_version(
create_range(None, '<=0.0.5'),
['0.0.3', '0.1.4', '0.0.5']),
'0.0.5')

0 comments on commit 5cf1cc7

Please sign in to comment.