|
| 1 | +""" |
| 2 | +locations.py tests |
| 3 | +
|
| 4 | +""" |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import shutil |
| 8 | +import tempfile |
| 9 | +import getpass |
| 10 | +from mock import Mock |
| 11 | +from nose import SkipTest |
| 12 | +from nose.tools import assert_raises |
| 13 | +import pip |
| 14 | + |
| 15 | +class TestLocations: |
| 16 | + def setup(self): |
| 17 | + self.tempdir = tempfile.mkdtemp() |
| 18 | + self.st_uid = 9999 |
| 19 | + self.username = "example" |
| 20 | + self.patch() |
| 21 | + |
| 22 | + def tearDown(self): |
| 23 | + self.revert_patch() |
| 24 | + shutil.rmtree(self.tempdir, ignore_errors=True) |
| 25 | + |
| 26 | + def patch(self): |
| 27 | + """ first store and then patch python methods pythons """ |
| 28 | + self.tempfile_gettempdir = tempfile.gettempdir |
| 29 | + self.old_os_fstat = os.fstat |
| 30 | + if sys.platform != 'win32': |
| 31 | + # os.getuid not implemented on windows |
| 32 | + self.old_os_getuid = os.getuid |
| 33 | + self.old_getpass_getuser = getpass.getuser |
| 34 | + |
| 35 | + # now patch |
| 36 | + tempfile.gettempdir = lambda : self.tempdir |
| 37 | + getpass.getuser = lambda : self.username |
| 38 | + os.getuid = lambda : self.st_uid |
| 39 | + os.fstat = lambda fd : self.get_mock_fstat(fd) |
| 40 | + |
| 41 | + def revert_patch(self): |
| 42 | + """ revert the patches to python methods """ |
| 43 | + tempfile.gettempdir = self.tempfile_gettempdir |
| 44 | + getpass.getuser = self.old_getpass_getuser |
| 45 | + if sys.platform != 'win32': |
| 46 | + # os.getuid not implemented on windows |
| 47 | + os.getuid = self.old_os_getuid |
| 48 | + os.fstat = self.old_os_fstat |
| 49 | + |
| 50 | + def get_mock_fstat(self, fd): |
| 51 | + """ returns a basic mock fstat call result. |
| 52 | + Currently only the st_uid attribute has been set. |
| 53 | + """ |
| 54 | + result = Mock() |
| 55 | + result.st_uid = self.st_uid |
| 56 | + return result |
| 57 | + |
| 58 | + def get_build_dir_location(self): |
| 59 | + """ returns a string pointing to the |
| 60 | + current build_prefix. |
| 61 | + """ |
| 62 | + return os.path.join(self.tempdir, 'pip-build-%s' % self.username) |
| 63 | + |
| 64 | + def test_dir_path(self): |
| 65 | + """ test the path name for the build_prefix |
| 66 | + """ |
| 67 | + from pip import locations |
| 68 | + assert locations._get_build_prefix() == self.get_build_dir_location() |
| 69 | + |
| 70 | + def test_dir_created(self): |
| 71 | + """ test that the build_prefix directory is generated when |
| 72 | + _get_build_prefix is called. |
| 73 | + """ |
| 74 | + #skip on windows, build dir is not created |
| 75 | + if sys.platform == 'win32': |
| 76 | + raise SkipTest() |
| 77 | + assert not os.path.exists(self.get_build_dir_location() ), \ |
| 78 | + "the build_prefix directory should not exist yet!" |
| 79 | + from pip import locations |
| 80 | + locations._get_build_prefix() |
| 81 | + assert os.path.exists(self.get_build_dir_location() ), \ |
| 82 | + "the build_prefix directory should now exist!" |
| 83 | + |
| 84 | + def test_error_raised_when_owned_by_another(self): |
| 85 | + """ test calling _get_build_prefix when there is a temporary |
| 86 | + directory owned by another user raises an InstallationError. |
| 87 | + """ |
| 88 | + #skip on windows; this exception logic only runs on linux |
| 89 | + if sys.platform == 'win32': |
| 90 | + raise SkipTest() |
| 91 | + from pip import locations |
| 92 | + os.getuid = lambda : 1111 |
| 93 | + os.mkdir(self.get_build_dir_location() ) |
| 94 | + assert_raises(pip.exceptions.InstallationError, locations._get_build_prefix) |
| 95 | + |
| 96 | + def test_no_error_raised_when_owned_by_you(self): |
| 97 | + """ test calling _get_build_prefix when there is a temporary |
| 98 | + directory owned by you raise no InstallationError. |
| 99 | + """ |
| 100 | + from pip import locations |
| 101 | + os.mkdir(self.get_build_dir_location()) |
| 102 | + locations._get_build_prefix() |
0 commit comments