root / env / lib / python2.7 / site-packages / distribute-0.6.19-py2.7.egg / setuptools / tests / test_easy_install.py @ 1a305335
History | View | Annotate | Download (7.83 KB)
1 |
"""Easy install Tests
|
---|---|
2 |
"""
|
3 |
import sys |
4 |
import os, shutil, tempfile, unittest |
5 |
import site |
6 |
from StringIO import StringIO |
7 |
from setuptools.command.easy_install import easy_install, get_script_args, main |
8 |
from setuptools.command.easy_install import PthDistributions |
9 |
from setuptools.command import easy_install as easy_install_pkg |
10 |
from setuptools.dist import Distribution |
11 |
from pkg_resources import Distribution as PRDistribution |
12 |
|
13 |
try:
|
14 |
import multiprocessing |
15 |
import logging |
16 |
_LOG = logging.getLogger('test_easy_install')
|
17 |
logging.basicConfig(level=logging.INFO, stream=sys.stderr) |
18 |
_MULTIPROC = True
|
19 |
except ImportError: |
20 |
_MULTIPROC = False
|
21 |
_LOG = None
|
22 |
|
23 |
class FakeDist(object): |
24 |
def get_entry_map(self, group): |
25 |
if group != 'console_scripts': |
26 |
return {}
|
27 |
return {'name': 'ep'} |
28 |
|
29 |
def as_requirement(self): |
30 |
return 'spec' |
31 |
|
32 |
WANTED = """\
|
33 |
#!%s
|
34 |
# EASY-INSTALL-ENTRY-SCRIPT: 'spec','console_scripts','name'
|
35 |
__requires__ = 'spec'
|
36 |
import sys
|
37 |
from pkg_resources import load_entry_point
|
38 |
|
39 |
if __name__ == '__main__':
|
40 |
sys.exit(
|
41 |
load_entry_point('spec', 'console_scripts', 'name')()
|
42 |
)
|
43 |
""" % sys.executable
|
44 |
|
45 |
SETUP_PY = """\
|
46 |
from setuptools import setup
|
47 |
|
48 |
setup(name='foo')
|
49 |
"""
|
50 |
|
51 |
class TestEasyInstallTest(unittest.TestCase): |
52 |
|
53 |
def test_install_site_py(self): |
54 |
dist = Distribution() |
55 |
cmd = easy_install(dist) |
56 |
cmd.sitepy_installed = False
|
57 |
cmd.install_dir = tempfile.mkdtemp() |
58 |
try:
|
59 |
cmd.install_site_py() |
60 |
sitepy = os.path.join(cmd.install_dir, 'site.py')
|
61 |
self.assert_(os.path.exists(sitepy))
|
62 |
finally:
|
63 |
shutil.rmtree(cmd.install_dir) |
64 |
|
65 |
def test_get_script_args(self): |
66 |
dist = FakeDist() |
67 |
|
68 |
old_platform = sys.platform |
69 |
try:
|
70 |
name, script = get_script_args(dist).next() |
71 |
finally:
|
72 |
sys.platform = old_platform |
73 |
|
74 |
self.assertEquals(script, WANTED)
|
75 |
|
76 |
def test_no_setup_cfg(self): |
77 |
# makes sure easy_install as a command (main)
|
78 |
# doesn't use a setup.cfg file that is located
|
79 |
# in the current working directory
|
80 |
dir = tempfile.mkdtemp() |
81 |
setup_cfg = open(os.path.join(dir, 'setup.cfg'), 'w') |
82 |
setup_cfg.write('[easy_install]\nfind_links = http://example.com')
|
83 |
setup_cfg.close() |
84 |
setup_py = open(os.path.join(dir, 'setup.py'), 'w') |
85 |
setup_py.write(SETUP_PY) |
86 |
setup_py.close() |
87 |
|
88 |
from setuptools.dist import Distribution |
89 |
|
90 |
def _parse_command_line(self): |
91 |
msg = 'Error: a local setup.cfg was used'
|
92 |
opts = self.command_options
|
93 |
if 'easy_install' in opts: |
94 |
assert 'find_links' not in opts['easy_install'], msg |
95 |
return self._old_parse_command_line |
96 |
|
97 |
Distribution._old_parse_command_line = Distribution.parse_command_line |
98 |
Distribution.parse_command_line = _parse_command_line |
99 |
|
100 |
old_wd = os.getcwd() |
101 |
try:
|
102 |
os.chdir(dir)
|
103 |
main([]) |
104 |
finally:
|
105 |
os.chdir(old_wd) |
106 |
shutil.rmtree(dir)
|
107 |
|
108 |
def test_no_find_links(self): |
109 |
# new option '--no-find-links', that blocks find-links added at
|
110 |
# the project level
|
111 |
dist = Distribution() |
112 |
cmd = easy_install(dist) |
113 |
cmd.check_pth_processing = lambda : True |
114 |
cmd.no_find_links = True
|
115 |
cmd.find_links = ['link1', 'link2'] |
116 |
cmd.install_dir = os.path.join(tempfile.mkdtemp(), 'ok')
|
117 |
cmd.args = ['ok']
|
118 |
cmd.ensure_finalized() |
119 |
self.assertEquals(cmd.package_index.scanned_urls, {})
|
120 |
|
121 |
# let's try without it (default behavior)
|
122 |
cmd = easy_install(dist) |
123 |
cmd.check_pth_processing = lambda : True |
124 |
cmd.find_links = ['link1', 'link2'] |
125 |
cmd.install_dir = os.path.join(tempfile.mkdtemp(), 'ok')
|
126 |
cmd.args = ['ok']
|
127 |
cmd.ensure_finalized() |
128 |
keys = cmd.package_index.scanned_urls.keys() |
129 |
keys.sort() |
130 |
self.assertEquals(keys, ['link1', 'link2']) |
131 |
|
132 |
|
133 |
class TestPTHFileWriter(unittest.TestCase): |
134 |
def test_add_from_cwd_site_sets_dirty(self): |
135 |
'''a pth file manager should set dirty
|
136 |
if a distribution is in site but also the cwd
|
137 |
'''
|
138 |
pth = PthDistributions('does-not_exist', [os.getcwd()])
|
139 |
self.assert_(not pth.dirty) |
140 |
pth.add(PRDistribution(os.getcwd())) |
141 |
self.assert_(pth.dirty)
|
142 |
|
143 |
def test_add_from_site_is_ignored(self): |
144 |
pth = PthDistributions('does-not_exist', ['/test/location/does-not-have-to-exist']) |
145 |
self.assert_(not pth.dirty) |
146 |
pth.add(PRDistribution('/test/location/does-not-have-to-exist'))
|
147 |
self.assert_(not pth.dirty) |
148 |
|
149 |
|
150 |
class TestUserInstallTest(unittest.TestCase): |
151 |
|
152 |
def setUp(self): |
153 |
self.dir = tempfile.mkdtemp()
|
154 |
setup = os.path.join(self.dir, 'setup.py') |
155 |
f = open(setup, 'w') |
156 |
f.write(SETUP_PY) |
157 |
f.close() |
158 |
self.old_cwd = os.getcwd()
|
159 |
os.chdir(self.dir)
|
160 |
if sys.version >= "2.6": |
161 |
self.old_has_site = easy_install_pkg.HAS_USER_SITE
|
162 |
self.old_file = easy_install_pkg.__file__
|
163 |
self.old_base = site.USER_BASE
|
164 |
site.USER_BASE = tempfile.mkdtemp() |
165 |
self.old_site = site.USER_SITE
|
166 |
site.USER_SITE = tempfile.mkdtemp() |
167 |
easy_install_pkg.__file__ = site.USER_SITE |
168 |
|
169 |
def tearDown(self): |
170 |
os.chdir(self.old_cwd)
|
171 |
shutil.rmtree(self.dir)
|
172 |
if sys.version >= "2.6": |
173 |
shutil.rmtree(site.USER_BASE) |
174 |
shutil.rmtree(site.USER_SITE) |
175 |
site.USER_BASE = self.old_base
|
176 |
site.USER_SITE = self.old_site
|
177 |
easy_install_pkg.HAS_USER_SITE = self.old_has_site
|
178 |
easy_install_pkg.__file__ = self.old_file
|
179 |
|
180 |
def test_user_install_implied(self): |
181 |
easy_install_pkg.HAS_USER_SITE = True # disabled sometimes |
182 |
#XXX: replace with something meaningfull
|
183 |
if sys.version < "2.6": |
184 |
return #SKIP |
185 |
dist = Distribution() |
186 |
dist.script_name = 'setup.py'
|
187 |
cmd = easy_install(dist) |
188 |
cmd.args = ['py']
|
189 |
cmd.ensure_finalized() |
190 |
self.assertTrue(cmd.user, 'user should be implied') |
191 |
|
192 |
def test_multiproc_atexit(self): |
193 |
if not _MULTIPROC: |
194 |
return
|
195 |
_LOG.info('this should not break')
|
196 |
|
197 |
def test_user_install_not_implied_without_usersite_enabled(self): |
198 |
easy_install_pkg.HAS_USER_SITE = False # usually enabled |
199 |
#XXX: replace with something meaningfull
|
200 |
if sys.version < "2.6": |
201 |
return #SKIP |
202 |
dist = Distribution() |
203 |
dist.script_name = 'setup.py'
|
204 |
cmd = easy_install(dist) |
205 |
cmd.args = ['py']
|
206 |
cmd.initialize_options() |
207 |
self.assertFalse(cmd.user, 'NOT user should be implied') |
208 |
|
209 |
def test_local_index(self): |
210 |
# make sure the local index is used
|
211 |
# when easy_install looks for installed
|
212 |
# packages
|
213 |
new_location = tempfile.mkdtemp() |
214 |
target = tempfile.mkdtemp() |
215 |
egg_file = os.path.join(new_location, 'foo-1.0.egg-info')
|
216 |
f = open(egg_file, 'w') |
217 |
try:
|
218 |
f.write('Name: foo\n')
|
219 |
except:
|
220 |
f.close() |
221 |
|
222 |
sys.path.append(target) |
223 |
old_ppath = os.environ.get('PYTHONPATH')
|
224 |
os.environ['PYTHONPATH'] = ':'.join(sys.path) |
225 |
try:
|
226 |
dist = Distribution() |
227 |
dist.script_name = 'setup.py'
|
228 |
cmd = easy_install(dist) |
229 |
cmd.install_dir = target |
230 |
cmd.args = ['foo']
|
231 |
cmd.ensure_finalized() |
232 |
cmd.local_index.scan([new_location]) |
233 |
res = cmd.easy_install('foo')
|
234 |
self.assertEquals(res.location, new_location)
|
235 |
finally:
|
236 |
sys.path.remove(target) |
237 |
shutil.rmtree(new_location) |
238 |
shutil.rmtree(target) |
239 |
if old_ppath is not None: |
240 |
os.environ['PYTHONPATH'] = old_ppath
|
241 |
else:
|
242 |
del os.environ['PYTHONPATH'] |
243 |
|