Skip to content

Commit

Permalink
Merge pull request #949 from fishtown-analytics/selectively-download-…
Browse files Browse the repository at this point in the history
…bumpversion

Remove unconditional calls to get_latest_version (#907)
  • Loading branch information
beckjake authored Aug 28, 2018
2 parents 8240542 + d946ac2 commit 6060146
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
34 changes: 31 additions & 3 deletions dbt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,35 @@
"""


class DBTVersion(argparse.Action):
"""This is very very similar to the builtin argparse._Version action,
except it just calls dbt.version.get_version_information().
"""
def __init__(self,
option_strings,
version=None,
dest=argparse.SUPPRESS,
default=argparse.SUPPRESS,
help="show program's version number and exit"):
super(DBTVersion, self).__init__(
option_strings=option_strings,
dest=dest,
default=default,
nargs=0,
help=help)

def __call__(self, parser, namespace, values, option_string=None):
formatter = parser._get_formatter()
formatter.add_text(dbt.version.get_version_information())
parser.exit(message=formatter.format_help())


class DBTArgumentParser(argparse.ArgumentParser):
def __init__(self, *args, **kwargs):
super(DBTArgumentParser, self).__init__(*args, **kwargs)
self.register('action', 'dbtversion', DBTVersion)


def main(args=None):
if args is None:
args = sys.argv[1:]
Expand Down Expand Up @@ -252,14 +281,13 @@ def invoke_dbt(parsed):


def parse_args(args):
p = argparse.ArgumentParser(
p = DBTArgumentParser(
prog='dbt: data build tool',
formatter_class=argparse.RawTextHelpFormatter)

p.add_argument(
'--version',
action='version',
version=dbt.version.get_version_information(),
action='dbtversion',
help="Show version information")

p.add_argument(
Expand Down
13 changes: 11 additions & 2 deletions dbt/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def get_remote_version_file_contents(url=REMOTE_VERSION_FILE):

def get_latest_version():
contents = get_remote_version_file_contents()
if contents == '':
return None
version_string = get_version_string_from_text(contents)
return dbt.semver.VersionSpecifier.from_version_string(version_string)

Expand All @@ -48,11 +50,19 @@ def get_version_information():
latest = get_latest_version()

installed_s = installed.to_version_string(skip_matcher=True)
latest_s = latest.to_version_string(skip_matcher=True)
if latest is None:
latest_s = 'unknown'
else:
latest_s = latest.to_version_string(skip_matcher=True)

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

if latest is None:
return ("{}The latest version of dbt could not be determined!\n"
"Make sure that the following URL is accessible:\n{}"
.format(version_msg, REMOTE_VERSION_FILE))

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

Expand All @@ -69,4 +79,3 @@ def get_version_information():

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

import dbt.main
import dbt.version
import sys


class VersionTest(unittest.TestCase):
Expand Down Expand Up @@ -87,3 +89,23 @@ def test_installed_version_lower(self):
assert installed_version < latest_version
self.assertMultiLineEqual(version_information,
expected_version_information)

# suppress having version info printed to the screen during tests.
@patch('sys.stderr')
def test_dbt_version_flag(self, stderr):
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]
""")

with self.assertRaises(SystemExit) as exc:
dbt.main.handle_and_check(['--version'])
self.assertEqual(exc.exception.code, 0)

0 comments on commit 6060146

Please sign in to comment.