blob: 8645f058da8ea4c48658f75932aa40d8b0f342bc [file] [log] [blame]
Donald Stufft8aaff542014-11-11 10:24:11 -05001import unittest
2import os
3import os.path
4import contextlib
5import sys
6import test._mock_backport as mock
7import test.test_support
8
9import ensurepip
10import ensurepip._uninstall
11
12# pip currently requires ssl support, so we ensure we handle
13# it being missing (http://bugs.python.org/issue19744)
14ensurepip_no_ssl = test.test_support.import_fresh_module("ensurepip",
15 blocked=["ssl"])
16try:
17 import ssl
18except ImportError:
19 ssl = None
20
21 def requires_usable_pip(f):
22 deco = unittest.skip(ensurepip._MISSING_SSL_MESSAGE)
23 return deco(f)
24else:
25 def requires_usable_pip(f):
26 return f
27
28
29class TestEnsurePipVersion(unittest.TestCase):
30
31 def test_returns_version(self):
32 self.assertEqual(ensurepip._PIP_VERSION, ensurepip.version())
33
34
35class EnsurepipMixin:
36
37 def setUp(self):
38 run_pip_patch = mock.patch("ensurepip._run_pip")
39 self.run_pip = run_pip_patch.start()
40 self.addCleanup(run_pip_patch.stop)
41
42 # Avoid side effects on the actual os module
43 real_devnull = os.devnull
44 os_patch = mock.patch("ensurepip.os")
45 patched_os = os_patch.start()
46 self.addCleanup(os_patch.stop)
47 patched_os.devnull = real_devnull
48 patched_os.path = os.path
49 self.os_environ = patched_os.environ = os.environ.copy()
50
51
52class TestBootstrap(EnsurepipMixin, unittest.TestCase):
53
54 @requires_usable_pip
55 def test_basic_bootstrapping(self):
56 ensurepip.bootstrap()
57
58 self.run_pip.assert_called_once_with(
59 [
60 "install", "--no-index", "--find-links",
61 mock.ANY, "setuptools", "pip",
62 ],
63 mock.ANY,
64 )
65
66 additional_paths = self.run_pip.call_args[0][1]
67 self.assertEqual(len(additional_paths), 2)
68
69 @requires_usable_pip
70 def test_bootstrapping_with_root(self):
71 ensurepip.bootstrap(root="/foo/bar/")
72
73 self.run_pip.assert_called_once_with(
74 [
75 "install", "--no-index", "--find-links",
76 mock.ANY, "--root", "/foo/bar/",
77 "setuptools", "pip",
78 ],
79 mock.ANY,
80 )
81
82 @requires_usable_pip
83 def test_bootstrapping_with_user(self):
84 ensurepip.bootstrap(user=True)
85
86 self.run_pip.assert_called_once_with(
87 [
88 "install", "--no-index", "--find-links",
89 mock.ANY, "--user", "setuptools", "pip",
90 ],
91 mock.ANY,
92 )
93
94 @requires_usable_pip
95 def test_bootstrapping_with_upgrade(self):
96 ensurepip.bootstrap(upgrade=True)
97
98 self.run_pip.assert_called_once_with(
99 [
100 "install", "--no-index", "--find-links",
101 mock.ANY, "--upgrade", "setuptools", "pip",
102 ],
103 mock.ANY,
104 )
105
106 @requires_usable_pip
107 def test_bootstrapping_with_verbosity_1(self):
108 ensurepip.bootstrap(verbosity=1)
109
110 self.run_pip.assert_called_once_with(
111 [
112 "install", "--no-index", "--find-links",
113 mock.ANY, "-v", "setuptools", "pip",
114 ],
115 mock.ANY,
116 )
117
118 @requires_usable_pip
119 def test_bootstrapping_with_verbosity_2(self):
120 ensurepip.bootstrap(verbosity=2)
121
122 self.run_pip.assert_called_once_with(
123 [
124 "install", "--no-index", "--find-links",
125 mock.ANY, "-vv", "setuptools", "pip",
126 ],
127 mock.ANY,
128 )
129
130 @requires_usable_pip
131 def test_bootstrapping_with_verbosity_3(self):
132 ensurepip.bootstrap(verbosity=3)
133
134 self.run_pip.assert_called_once_with(
135 [
136 "install", "--no-index", "--find-links",
137 mock.ANY, "-vvv", "setuptools", "pip",
138 ],
139 mock.ANY,
140 )
141
142 @requires_usable_pip
143 def test_bootstrapping_with_regular_install(self):
144 ensurepip.bootstrap()
145 self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "install")
146
147 @requires_usable_pip
148 def test_bootstrapping_with_alt_install(self):
149 ensurepip.bootstrap(altinstall=True)
150 self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "altinstall")
151
152 @requires_usable_pip
153 def test_bootstrapping_with_default_pip(self):
154 ensurepip.bootstrap(default_pip=True)
155 self.assertNotIn("ENSUREPIP_OPTIONS", self.os_environ)
156
157 def test_altinstall_default_pip_conflict(self):
158 with self.assertRaises(ValueError):
159 ensurepip.bootstrap(altinstall=True, default_pip=True)
160 self.assertFalse(self.run_pip.called)
161
162 @requires_usable_pip
163 def test_pip_environment_variables_removed(self):
164 # ensurepip deliberately ignores all pip environment variables
165 # See http://bugs.python.org/issue19734 for details
166 self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder"
167 ensurepip.bootstrap()
168 self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
169
170 @requires_usable_pip
171 def test_pip_config_file_disabled(self):
172 # ensurepip deliberately ignores the pip config file
173 # See http://bugs.python.org/issue20053 for details
174 ensurepip.bootstrap()
175 self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
176
177
178@contextlib.contextmanager
179def fake_pip(version=ensurepip._PIP_VERSION):
180 if version is None:
181 pip = None
182 else:
183 class FakePip():
184 __version__ = version
185 pip = FakePip()
186 sentinel = object()
187 orig_pip = sys.modules.get("pip", sentinel)
188 sys.modules["pip"] = pip
189 try:
190 yield pip
191 finally:
192 if orig_pip is sentinel:
193 del sys.modules["pip"]
194 else:
195 sys.modules["pip"] = orig_pip
196
197
198class TestUninstall(EnsurepipMixin, unittest.TestCase):
199
200 def test_uninstall_skipped_when_not_installed(self):
201 with fake_pip(None):
202 ensurepip._uninstall_helper()
203 self.assertFalse(self.run_pip.called)
204
205 def test_uninstall_skipped_with_warning_for_wrong_version(self):
206 with fake_pip("not a valid version"):
207 with test.test_support.captured_stderr() as stderr:
208 ensurepip._uninstall_helper()
209 warning = stderr.getvalue().strip()
210 self.assertIn("only uninstall a matching version", warning)
211 self.assertFalse(self.run_pip.called)
212
213 @requires_usable_pip
214 def test_uninstall(self):
215 with fake_pip():
216 ensurepip._uninstall_helper()
217
218 self.run_pip.assert_called_once_with(
Donald Stufft69c0d262015-06-02 10:54:37 -0400219 [
220 "uninstall", "-y", "--disable-pip-version-check", "pip",
221 "setuptools",
222 ]
Donald Stufft8aaff542014-11-11 10:24:11 -0500223 )
224
225 @requires_usable_pip
226 def test_uninstall_with_verbosity_1(self):
227 with fake_pip():
228 ensurepip._uninstall_helper(verbosity=1)
229
230 self.run_pip.assert_called_once_with(
Donald Stufft69c0d262015-06-02 10:54:37 -0400231 [
232 "uninstall", "-y", "--disable-pip-version-check", "-v", "pip",
233 "setuptools",
234 ]
Donald Stufft8aaff542014-11-11 10:24:11 -0500235 )
236
237 @requires_usable_pip
238 def test_uninstall_with_verbosity_2(self):
239 with fake_pip():
240 ensurepip._uninstall_helper(verbosity=2)
241
242 self.run_pip.assert_called_once_with(
Donald Stufft69c0d262015-06-02 10:54:37 -0400243 [
244 "uninstall", "-y", "--disable-pip-version-check", "-vv", "pip",
245 "setuptools",
246 ]
Donald Stufft8aaff542014-11-11 10:24:11 -0500247 )
248
249 @requires_usable_pip
250 def test_uninstall_with_verbosity_3(self):
251 with fake_pip():
252 ensurepip._uninstall_helper(verbosity=3)
253
254 self.run_pip.assert_called_once_with(
Donald Stufft69c0d262015-06-02 10:54:37 -0400255 [
256 "uninstall", "-y", "--disable-pip-version-check", "-vvv",
257 "pip", "setuptools",
258 ]
Donald Stufft8aaff542014-11-11 10:24:11 -0500259 )
260
261 @requires_usable_pip
262 def test_pip_environment_variables_removed(self):
263 # ensurepip deliberately ignores all pip environment variables
264 # See http://bugs.python.org/issue19734 for details
265 self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder"
266 with fake_pip():
267 ensurepip._uninstall_helper()
268 self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
269
270 @requires_usable_pip
271 def test_pip_config_file_disabled(self):
272 # ensurepip deliberately ignores the pip config file
273 # See http://bugs.python.org/issue20053 for details
274 with fake_pip():
275 ensurepip._uninstall_helper()
276 self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
277
278
279class TestMissingSSL(EnsurepipMixin, unittest.TestCase):
280
281 def setUp(self):
282 sys.modules["ensurepip"] = ensurepip_no_ssl
283
284 @self.addCleanup
285 def restore_module():
286 sys.modules["ensurepip"] = ensurepip
287 super(TestMissingSSL, self).setUp()
288
289 def test_bootstrap_requires_ssl(self):
290 self.os_environ["PIP_THIS_SHOULD_STAY"] = "test fodder"
291 with self.assertRaisesRegexp(RuntimeError, "requires SSL/TLS"):
292 ensurepip_no_ssl.bootstrap()
293 self.assertFalse(self.run_pip.called)
294 self.assertIn("PIP_THIS_SHOULD_STAY", self.os_environ)
295
296 def test_uninstall_requires_ssl(self):
297 self.os_environ["PIP_THIS_SHOULD_STAY"] = "test fodder"
298 with self.assertRaisesRegexp(RuntimeError, "requires SSL/TLS"):
299 with fake_pip():
300 ensurepip_no_ssl._uninstall_helper()
301 self.assertFalse(self.run_pip.called)
302 self.assertIn("PIP_THIS_SHOULD_STAY", self.os_environ)
303
304 def test_main_exits_early_with_warning(self):
305 with test.test_support.captured_stderr() as stderr:
306 ensurepip_no_ssl._main(["--version"])
307 warning = stderr.getvalue().strip()
308 self.assertTrue(warning.endswith("requires SSL/TLS"), warning)
309 self.assertFalse(self.run_pip.called)
310
311# Basic testing of the main functions and their argument parsing
312
313EXPECTED_VERSION_OUTPUT = "pip " + ensurepip._PIP_VERSION
314
315
316class TestBootstrappingMainFunction(EnsurepipMixin, unittest.TestCase):
317
318 @requires_usable_pip
319 def test_bootstrap_version(self):
320 with test.test_support.captured_stderr() as stderr:
321 with self.assertRaises(SystemExit):
322 ensurepip._main(["--version"])
323 result = stderr.getvalue().strip()
324 self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
325 self.assertFalse(self.run_pip.called)
326
327 @requires_usable_pip
328 def test_basic_bootstrapping(self):
329 ensurepip._main([])
330
331 self.run_pip.assert_called_once_with(
332 [
333 "install", "--no-index", "--find-links",
334 mock.ANY, "setuptools", "pip",
335 ],
336 mock.ANY,
337 )
338
339 additional_paths = self.run_pip.call_args[0][1]
340 self.assertEqual(len(additional_paths), 2)
341
342
343class TestUninstallationMainFunction(EnsurepipMixin, unittest.TestCase):
344
345 def test_uninstall_version(self):
346 with test.test_support.captured_stderr() as stderr:
347 with self.assertRaises(SystemExit):
348 ensurepip._uninstall._main(["--version"])
349 result = stderr.getvalue().strip()
350 self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
351 self.assertFalse(self.run_pip.called)
352
353 @requires_usable_pip
354 def test_basic_uninstall(self):
355 with fake_pip():
356 ensurepip._uninstall._main([])
357
358 self.run_pip.assert_called_once_with(
Donald Stufft69c0d262015-06-02 10:54:37 -0400359 [
360 "uninstall", "-y", "--disable-pip-version-check", "pip",
361 "setuptools",
362 ]
Donald Stufft8aaff542014-11-11 10:24:11 -0500363 )
364
365
366if __name__ == "__main__":
367 test.test_support.run_unittest(__name__)