Skip to content

Commit

Permalink
Merge pull request #944 from ababic/issue/#992
Browse files Browse the repository at this point in the history
Fix #922: Assume less about the signature of apps' `get_version()` in VersionsPanel
  • Loading branch information
aaugustin committed May 11, 2017
2 parents c998e96 + 577759e commit 6799ee4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
29 changes: 17 additions & 12 deletions debug_toolbar/panels/versions.py
Expand Up @@ -41,20 +41,25 @@ def gen_app_versions(self):
yield app.__name__, name, version

def get_app_version(self, app):
if hasattr(app, 'get_version'):
get_version = app.get_version
if callable(get_version):
version = get_version()
else:
version = get_version
elif hasattr(app, 'VERSION'):
version = app.VERSION
elif hasattr(app, '__version__'):
version = app.__version__
else:
return
version = self.get_version_from_app(app)
if isinstance(version, (list, tuple)):
# We strip dots from the right because we do not want to show
# trailing dots if there are empty elements in the list/tuple
version = '.'.join(str(o) for o in version).rstrip('.')
return version

def get_version_from_app(self, app):
if hasattr(app, 'get_version'):
get_version = app.get_version
if callable(get_version):
try:
return get_version()
except TypeError:
pass
else:
return get_version
if hasattr(app, 'VERSION'):
return app.VERSION
if hasattr(app, '__version__'):
return app.__version__
return
14 changes: 13 additions & 1 deletion tests/panels/test_versions.py
Expand Up @@ -25,14 +25,26 @@ def get_version(self):

self.assertEqual(self.panel.get_app_version(FakeApp()), '1.2.3')

def test_incompatible_app_version_fn(self):

class FakeApp:

def get_version(self, some_other_arg):
# This should be ignored by the get_version_from_app
return version_info_t(0, 0, 0, '', '')

VERSION = version_info_t(1, 2, 3, '', '')

self.assertEqual(self.panel.get_app_version(FakeApp()), '1.2.3')

def test_app_version_from_VERSION(self):

class FakeApp:
VERSION = version_info_t(1, 2, 3, '', '')

self.assertEqual(self.panel.get_app_version(FakeApp()), '1.2.3')

def test_app_version_from_underscode_version(self):
def test_app_version_from_underscore_version(self):

class FakeApp:
__version__ = version_info_t(1, 2, 3, '', '')
Expand Down

0 comments on commit 6799ee4

Please sign in to comment.