Skip to content

Commit

Permalink
Allow for more complex version comparison (redux) (#797)
Browse files Browse the repository at this point in the history
* Allow for more complex version comparison

Allow for letters in version names
Add tests for version info

* Fix integration tests

* Combine code from @mturzanska's PR with development

Based on: #577
Fixes: #557
  • Loading branch information
drewbanin committed Jun 27, 2018
1 parent 457db9d commit d28407d
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 34 deletions.
68 changes: 34 additions & 34 deletions dbt/version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import argparse
import os
import re

import dbt.semver

try:
# For Python 3.0 and later
from urllib.request import urlopen
Expand All @@ -14,59 +14,59 @@
'master/.bumpversion.cfg'


def __parse_version(contents):
matches = re.search(r"current_version = ([\.0-9]+)", contents)
def get_version_string_from_text(contents):
matches = re.search(r"current_version = ([\.0-9a-z]+)", contents)
if matches is None or len(matches.groups()) != 1:
return "unknown"
else:
version = matches.groups()[0]
return version

return ""
version = matches.groups()[0]
return version

def get_version():
return __version__


def get_latest_version():
def get_remote_version_file_contents(url=REMOTE_VERSION_FILE):
try:
f = urlopen(REMOTE_VERSION_FILE)
f = urlopen(url)
contents = f.read()
except Exception:
contents = ''
if hasattr(contents, 'decode'):
contents = contents.decode('utf-8')
return __parse_version(contents)
return contents


def not_latest():
return """Your version of dbt is out of date! You can find instructions
for upgrading here:
https://docs.getdbt.com/docs/installation
"""
def get_latest_version():
contents = get_remote_version_file_contents()
version_string = get_version_string_from_text(contents)
return dbt.semver.VersionSpecifier.from_version_string(version_string)


def get_version_string():
return "installed version: {}\n latest version: {}".format(
installed, latest
)
def get_installed_version():
return dbt.semver.VersionSpecifier.from_version_string(__version__)


def get_version_information():
basic = get_version_string()
installed = get_installed_version()
latest = get_latest_version()

if is_latest():
basic += '\nUp to date!'
else:
basic += '\n{}'.format(not_latest())
installed_s = installed.to_version_string(skip_matcher=True)
latest_s = latest.to_version_string(skip_matcher=True)

return basic
version_msg = ("installed version: {}\n"
" latest version: {}\n\n".format(installed_s, latest_s))

if installed == latest:
return "{}Up to date!".format(version_msg)

def is_latest():
return installed == latest
elif installed > latest:
return ("{}Your version of dbt is ahead of the latest "
"release!".format(version_msg))

else:
return ("{}Your version of dbt is out of date! "
"You can find instructions for upgrading here:\n"
"https://docs.getdbt.com/docs/installation"
.format(version_msg))


__version__ = '0.10.1'
installed = get_version()
installed = get_installed_version()
latest = get_latest_version()
89 changes: 89 additions & 0 deletions test/unit/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from mock import patch, MagicMock
import unittest

import dbt.version


class VersionTest(unittest.TestCase):

@patch("dbt.version.__version__", "0.10.0")
def test_versions_equal(self):

dbt.version.get_remote_version_file_contents = MagicMock(
return_value="""
[bumpversion]
current_version = 0.10.0
commit = True
tag = True
[bumpversion:file:setup.py]
[bumpversion:file:dbt/version.py]
""")

latest_version = dbt.version.get_latest_version()
installed_version = dbt.version.get_installed_version()
version_information = dbt.version.get_version_information()

expected_version_information = "installed version: 0.10.0\n" \
" latest version: 0.10.0\n\n" \
"Up to date!"

self.assertEqual(latest_version, installed_version)
self.assertEqual(latest_version, installed_version)
self.assertMultiLineEqual(version_information,
expected_version_information)

@patch("dbt.version.__version__", "0.10.2-a1")
def test_installed_version_greater(self):
dbt.version.get_remote_version_file_contents = MagicMock(
return_value="""
[bumpversion]
current_version = 0.10.1
commit = True
tag = True
[bumpversion:file:setup.py]
[bumpversion:file:dbt/version.py]
""")

latest_version = dbt.version.get_latest_version()
installed_version = dbt.version.get_installed_version()
version_information = dbt.version.get_version_information()

expected_version_information = "installed version: 0.10.2-a1\n" \
" latest version: 0.10.1\n\n" \
"Your version of dbt is ahead of the latest release!"

assert installed_version > latest_version
self.assertMultiLineEqual(version_information,
expected_version_information)

@patch("dbt.version.__version__", "0.9.5")
def test_installed_version_lower(self):
dbt.version.get_remote_version_file_contents = MagicMock(
return_value="""
[bumpversion]
current_version = 0.10.0
commit = True
tag = True
[bumpversion:file:setup.py]
[bumpversion:file:dbt/version.py]
""")

latest_version = dbt.version.get_latest_version()
installed_version = dbt.version.get_installed_version()
version_information = dbt.version.get_version_information()

expected_version_information = "installed version: 0.9.5\n" \
" latest version: 0.10.0\n\n" \
"Your version of dbt is out of date! " \
"You can find instructions for upgrading here:\n" \
"https://docs.getdbt.com/docs/installation"

assert installed_version < latest_version
self.assertMultiLineEqual(version_information,
expected_version_information)

0 comments on commit d28407d

Please sign in to comment.