Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #26 from mitodl/feature/cg/version_fix
Browse files Browse the repository at this point in the history
Use absolute path to __file__.
  • Loading branch information
pwilkins committed Mar 17, 2015
2 parents db77d9f + 244534b commit 27ff539
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
34 changes: 19 additions & 15 deletions pylmod/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@
from pylmod.gradebook import GradeBook
from pylmod.membership import Membership

# pylint: disable=no-member
try:
DIST = get_distribution('pylmod')
# Normalize case for Windows systems
DIST_LOC = os.path.normcase(DIST.location)
HERE = os.path.normcase(__file__)
if not HERE.startswith(
os.path.join(DIST_LOC, 'pylmod')
): # pragma: no cover
# not installed, but there is another version that *is*
raise DistributionNotFound
except DistributionNotFound: # pragma: no cover
__version__ = 'Please install this project with setup.py'
else:
__version__ = DIST.version

def _get_version():
"""Grab version from pkg_resources"""
# pylint: disable=no-member
try:
dist = get_distribution(__project__)
# Normalize case for Windows systems
dist_loc = os.path.normcase(dist.location)
here = os.path.normcase(os.path.abspath(__file__))
if not here.startswith(
os.path.join(dist_loc, __project__)
):
# not installed, but there is another version that *is*
raise DistributionNotFound
except DistributionNotFound:
return 'Please install this project with setup.py'
else:
return dist.version

__all__ = ['Client', 'GradeBook', 'Membership']
__project__ = 'pylmod'
__version__ = _get_version()
21 changes: 21 additions & 0 deletions pylmod/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import unittest

import mock
import semantic_version


Expand All @@ -30,3 +31,23 @@ def test_client_classes(self):
['Client', 'GradeBook', 'Membership'],
pylmod.__all__
)

def test_bad_version(self):
"""Verify bad version handling"""
from pkg_resources import DistributionNotFound
from pylmod import _get_version

error_string = 'Please install this project with setup.py'

with mock.patch('pylmod.get_distribution') as mock_distribution:
# Test with distribution not found:
mock_distribution.side_effect = DistributionNotFound()
self.assertEqual(_get_version(), error_string)

# Test with loc path not matching
with mock.patch('os.path.abspath') as mock_path:
mock_path.return_value = 'not/where/we/are'
self.assertEqual(_get_version(), error_string)
# Bonus regression test to make sure we are calling
# abspath.
self.assertTrue(mock_path.called)

0 comments on commit 27ff539

Please sign in to comment.