9
9
import getpass
10
10
from mock import Mock
11
11
from nose import SkipTest
12
- from nose .tools import assert_raises
12
+ from nose .tools import assert_raises , assert_equal
13
13
import pip
14
14
15
+ if sys .platform == 'win32' :
16
+ pwd = Mock ()
17
+ else :
18
+ import pwd
19
+
20
+
15
21
class TestLocations :
16
22
def setup (self ):
17
23
self .tempdir = tempfile .mkdtemp ()
@@ -28,23 +34,28 @@ def patch(self):
28
34
self .tempfile_gettempdir = tempfile .gettempdir
29
35
self .old_os_fstat = os .fstat
30
36
if sys .platform != 'win32' :
31
- # os.getuid not implemented on windows
32
- self .old_os_getuid = os .getuid
37
+ # os.geteuid and pwd.getpwuid are not implemented on windows
38
+ self .old_os_geteuid = os .geteuid
39
+ self .old_pwd_getpwuid = pwd .getpwuid
33
40
self .old_getpass_getuser = getpass .getuser
34
41
35
42
# now patch
36
43
tempfile .gettempdir = lambda : self .tempdir
37
44
getpass .getuser = lambda : self .username
38
- os .getuid = lambda : self .st_uid
45
+ os .geteuid = lambda : self .st_uid
39
46
os .fstat = lambda fd : self .get_mock_fstat (fd )
40
47
48
+ if sys .platform != 'win32' :
49
+ pwd .getpwuid = lambda uid : self .get_mock_getpwuid (uid )
50
+
41
51
def revert_patch (self ):
42
52
""" revert the patches to python methods """
43
53
tempfile .gettempdir = self .tempfile_gettempdir
44
54
getpass .getuser = self .old_getpass_getuser
45
55
if sys .platform != 'win32' :
46
- # os.getuid not implemented on windows
47
- os .getuid = self .old_os_getuid
56
+ # os.geteuid and pwd.getpwuid are not implemented on windows
57
+ os .geteuid = self .old_os_geteuid
58
+ pwd .getpwuid = self .old_pwd_getpwuid
48
59
os .fstat = self .old_os_fstat
49
60
50
61
def get_mock_fstat (self , fd ):
@@ -55,6 +66,14 @@ def get_mock_fstat(self, fd):
55
66
result .st_uid = self .st_uid
56
67
return result
57
68
69
+ def get_mock_getpwuid (self , uid ):
70
+ """ returns a basic mock pwd.getpwuid call result.
71
+ Currently only the pw_name attribute has been set.
72
+ """
73
+ result = Mock ()
74
+ result .pw_name = self .username
75
+ return result
76
+
58
77
def get_build_dir_location (self ):
59
78
""" returns a string pointing to the
60
79
current build_prefix.
@@ -65,7 +84,8 @@ def test_dir_path(self):
65
84
""" test the path name for the build_prefix
66
85
"""
67
86
from pip import locations
68
- assert locations ._get_build_prefix () == self .get_build_dir_location ()
87
+ assert_equal (locations ._get_build_prefix (),
88
+ self .get_build_dir_location ())
69
89
70
90
def test_dir_created (self ):
71
91
""" test that the build_prefix directory is generated when
@@ -89,7 +109,7 @@ def test_error_raised_when_owned_by_another(self):
89
109
if sys .platform == 'win32' :
90
110
raise SkipTest ()
91
111
from pip import locations
92
- os .getuid = lambda : 1111
112
+ os .geteuid = lambda : 1111
93
113
os .mkdir (self .get_build_dir_location () )
94
114
assert_raises (pip .exceptions .InstallationError , locations ._get_build_prefix )
95
115
0 commit comments