blob: f671ee3c0b76b90d3ba7d20c53b73ab35d3a3132 [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(
219 ["uninstall", "-y", "pip", "setuptools"]
220 )
221
222 @requires_usable_pip
223 def test_uninstall_with_verbosity_1(self):
224 with fake_pip():
225 ensurepip._uninstall_helper(verbosity=1)
226
227 self.run_pip.assert_called_once_with(
228 ["uninstall", "-y", "-v", "pip", "setuptools"]
229 )
230
231 @requires_usable_pip
232 def test_uninstall_with_verbosity_2(self):
233 with fake_pip():
234 ensurepip._uninstall_helper(verbosity=2)
235
236 self.run_pip.assert_called_once_with(
237 ["uninstall", "-y", "-vv", "pip", "setuptools"]
238 )
239
240 @requires_usable_pip
241 def test_uninstall_with_verbosity_3(self):
242 with fake_pip():
243 ensurepip._uninstall_helper(verbosity=3)
244
245 self.run_pip.assert_called_once_with(
246 ["uninstall", "-y", "-vvv", "pip", "setuptools"]
247 )
248
249 @requires_usable_pip
250 def test_pip_environment_variables_removed(self):
251 # ensurepip deliberately ignores all pip environment variables
252 # See http://bugs.python.org/issue19734 for details
253 self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder"
254 with fake_pip():
255 ensurepip._uninstall_helper()
256 self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
257
258 @requires_usable_pip
259 def test_pip_config_file_disabled(self):
260 # ensurepip deliberately ignores the pip config file
261 # See http://bugs.python.org/issue20053 for details
262 with fake_pip():
263 ensurepip._uninstall_helper()
264 self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
265
266
267class TestMissingSSL(EnsurepipMixin, unittest.TestCase):
268
269 def setUp(self):
270 sys.modules["ensurepip"] = ensurepip_no_ssl
271
272 @self.addCleanup
273 def restore_module():
274 sys.modules["ensurepip"] = ensurepip
275 super(TestMissingSSL, self).setUp()
276
277 def test_bootstrap_requires_ssl(self):
278 self.os_environ["PIP_THIS_SHOULD_STAY"] = "test fodder"
279 with self.assertRaisesRegexp(RuntimeError, "requires SSL/TLS"):
280 ensurepip_no_ssl.bootstrap()
281 self.assertFalse(self.run_pip.called)
282 self.assertIn("PIP_THIS_SHOULD_STAY", self.os_environ)
283
284 def test_uninstall_requires_ssl(self):
285 self.os_environ["PIP_THIS_SHOULD_STAY"] = "test fodder"
286 with self.assertRaisesRegexp(RuntimeError, "requires SSL/TLS"):
287 with fake_pip():
288 ensurepip_no_ssl._uninstall_helper()
289 self.assertFalse(self.run_pip.called)
290 self.assertIn("PIP_THIS_SHOULD_STAY", self.os_environ)
291
292 def test_main_exits_early_with_warning(self):
293 with test.test_support.captured_stderr() as stderr:
294 ensurepip_no_ssl._main(["--version"])
295 warning = stderr.getvalue().strip()
296 self.assertTrue(warning.endswith("requires SSL/TLS"), warning)
297 self.assertFalse(self.run_pip.called)
298
299# Basic testing of the main functions and their argument parsing
300
301EXPECTED_VERSION_OUTPUT = "pip " + ensurepip._PIP_VERSION
302
303
304class TestBootstrappingMainFunction(EnsurepipMixin, unittest.TestCase):
305
306 @requires_usable_pip
307 def test_bootstrap_version(self):
308 with test.test_support.captured_stderr() as stderr:
309 with self.assertRaises(SystemExit):
310 ensurepip._main(["--version"])
311 result = stderr.getvalue().strip()
312 self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
313 self.assertFalse(self.run_pip.called)
314
315 @requires_usable_pip
316 def test_basic_bootstrapping(self):
317 ensurepip._main([])
318
319 self.run_pip.assert_called_once_with(
320 [
321 "install", "--no-index", "--find-links",
322 mock.ANY, "setuptools", "pip",
323 ],
324 mock.ANY,
325 )
326
327 additional_paths = self.run_pip.call_args[0][1]
328 self.assertEqual(len(additional_paths), 2)
329
330
331class TestUninstallationMainFunction(EnsurepipMixin, unittest.TestCase):
332
333 def test_uninstall_version(self):
334 with test.test_support.captured_stderr() as stderr:
335 with self.assertRaises(SystemExit):
336 ensurepip._uninstall._main(["--version"])
337 result = stderr.getvalue().strip()
338 self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
339 self.assertFalse(self.run_pip.called)
340
341 @requires_usable_pip
342 def test_basic_uninstall(self):
343 with fake_pip():
344 ensurepip._uninstall._main([])
345
346 self.run_pip.assert_called_once_with(
347 ["uninstall", "-y", "pip", "setuptools"]
348 )
349
350
351if __name__ == "__main__":
352 test.test_support.run_unittest(__name__)