Skip to content

Commit

Permalink
Add dbt plugin version when get_version_information is called
Browse files Browse the repository at this point in the history
  • Loading branch information
sumanau7 authored and Sumanau Sareen committed Apr 3, 2020
1 parent 1dd4187 commit a85fe6c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 21 deletions.
31 changes: 25 additions & 6 deletions core/dbt/version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json

import pkg_resources
import requests

import dbt.exceptions
Expand Down Expand Up @@ -36,24 +37,42 @@ def get_version_information():

version_msg = ("installed version: {}\n"
" latest version: {}\n\n".format(installed_s, latest_s))
dbt_plugins_with_version = _get_dbt_plugins_info()
plugin_version_msg = "Plugins:\n"
for plugin_name, version in dbt_plugins_with_version:
plugin_version_msg += ' - {plugin_name}: {version}\n'.format(
plugin_name=plugin_name, version=version
)

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, PYPI_VERSION_URL))
"Make sure that the following URL is accessible:\n{}\n\n"
.format(
version_msg,
PYPI_VERSION_URL,
plugin_version_msg)
)

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

elif installed > latest:
return ("{}Your version of dbt is ahead of the latest "
"release!".format(version_msg))
"release!\n\n{}".format(version_msg, plugin_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))
"https://docs.getdbt.com/docs/installation\n\n{}"
.format(version_msg, plugin_version_msg))


def _get_dbt_plugins_info():
return [
(d.project_name, d.version) for d in pkg_resources.working_set if (
d.project_name.startswith('dbt-') and 'core' not in d.project_name
)
]


__version__ = '0.14.1'
Expand Down
74 changes: 59 additions & 15 deletions test/unit/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,87 @@

import dbt.main
import dbt.version
import sys


class VersionTest(unittest.TestCase):

@patch("dbt.version.__version__", "0.10.0")
@patch("dbt.version._get_dbt_plugins_info", autospec=True)
@patch('dbt.version.requests.get')
def test_versions_equal(self, mock_get):
mock_get.return_value.json.return_value = {'info': {'version': '0.10.0'}}
def test_versions_equal(self, mock_get, mock_get_dbt_plugins_info):
mock_get.return_value.json.return_value = {
'info': {'version': '0.10.0'}
}
mock_get_dbt_plugins_info.return_value = [
('dbt-postgres', '0.10.0'),
('dbt-redshift', '0.10.0'),
('dbt-bigquery', '0.10.0'),
('dbt-snowflake', '0.10.0')
]

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!"

"Up to date!\n\n" \
"Plugins:\n" \
" - dbt-postgres: 0.10.0\n" \
" - dbt-redshift: 0.10.0\n" \
" - dbt-bigquery: 0.10.0\n" \
" - dbt-snowflake: 0.10.0\n"
self.assertEqual(latest_version, installed_version)
self.assertEqual(latest_version, installed_version)
self.assertMultiLineEqual(version_information,
expected_version_information)
self.assertMultiLineEqual(
version_information, expected_version_information
)

@patch("dbt.version.__version__", "0.10.2-a1")
@patch("dbt.version._get_dbt_plugins_info", autospec=True)
@patch('dbt.version.requests.get')
def test_installed_version_greater(self, mock_get):
mock_get.return_value.json.return_value = {'info': {'version': '0.10.1'}}

def test_installed_version_greater(
self, mock_get, mock_get_dbt_plugins_info
):
mock_get.return_value.json.return_value = {
'info': {'version': '0.10.1'}
}
mock_get_dbt_plugins_info.return_value = [
('dbt-postgres', '0.10.0'),
('dbt-redshift', '0.10.0'),
('dbt-bigquery', '0.10.0'),
('dbt-snowflake', '0.10.0')
]
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!"
"Your version of dbt is ahead of the latest release!\n\n" \
"Plugins:\n" \
" - dbt-postgres: 0.10.0\n" \
" - dbt-redshift: 0.10.0\n" \
" - dbt-bigquery: 0.10.0\n" \
" - dbt-snowflake: 0.10.0\n"

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

@patch("dbt.version.__version__", "0.9.5")
@patch("dbt.version._get_dbt_plugins_info", autospec=True)
@patch('dbt.version.requests.get')
def test_installed_version_lower(self, mock_get):
mock_get.return_value.json.return_value = {'info': {'version': '0.10.0'}}
def test_installed_version_lower(self, mock_get, mock_get_dbt_plugins_info):
mock_get.return_value.json.return_value = {
'info': {'version': '0.10.0'}
}
mock_get_dbt_plugins_info.return_value = [
('dbt-postgres', '0.10.0'),
('dbt-redshift', '0.10.0'),
('dbt-bigquery', '0.10.0'),
('dbt-snowflake', '0.10.0')
]

latest_version = dbt.version.get_latest_version()
installed_version = dbt.version.get_installed_version()
Expand All @@ -56,7 +93,12 @@ def test_installed_version_lower(self, mock_get):
" 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"
"https://docs.getdbt.com/docs/installation\n\n" \
"Plugins:\n" \
" - dbt-postgres: 0.10.0\n" \
" - dbt-redshift: 0.10.0\n" \
" - dbt-bigquery: 0.10.0\n" \
" - dbt-snowflake: 0.10.0\n"

assert installed_version < latest_version
self.assertMultiLineEqual(version_information,
Expand All @@ -66,7 +108,9 @@ def test_installed_version_lower(self, mock_get):
@patch('sys.stderr')
@patch('dbt.version.requests.get')
def test_dbt_version_flag(self, mock_get, stderr):
mock_get.return_value.json.return_value = {'info': {'version': '0.10.1'}}
mock_get.return_value.json.return_value = {
'info': {'version': '0.10.1'}
}

with self.assertRaises(SystemExit) as exc:
dbt.main.handle_and_check(['--version'])
Expand Down

0 comments on commit a85fe6c

Please sign in to comment.