Debian Codegraph @

Lib/test/test_ensurepip.py

raw
1 import contextlib
2 import os
3 import os.path
4 import sys
5 import tempfile
6 import test.support
7 import unittest
8 import unittest.mock
9 from importlib.resources.abc import Traversable
10 from pathlib import Path
11
12 import ensurepip
13 import ensurepip._uninstall
14
15
16 class TestPackages(unittest.TestCase):
17 def touch(self, directory, filename):
18 fullname = os.path.join(directory, filename)
19 open(fullname, "wb").close()
20
21 def test_version(self):
22 # Test version()
23 with tempfile.TemporaryDirectory() as tmpdir:
24 self.touch(tmpdir, "pip-1.2.3b1-py2.py3-none-any.whl")
25 with unittest.mock.patch.object(ensurepip, '_WHEEL_PKG_DIR', Path(tmpdir)):
26 self.assertEqual(ensurepip.version(), '1.2.3b1')
27
28 def test_version_no_dir(self):
29 # Test version() without a wheel package directory
30 with unittest.mock.patch.object(ensurepip, '_WHEEL_PKG_DIR', None):
31 # when the bundled pip wheel is used, we get _PIP_VERSION
32 self.assertEqual(ensurepip._PIP_VERSION, ensurepip.version())
33
34 def test_selected_wheel_path_no_dir(self):
35 pip_filename = f'pip-{ensurepip._PIP_VERSION}-py3-none-any.whl'
36 with unittest.mock.patch.object(ensurepip, '_WHEEL_PKG_DIR', None):
37 with ensurepip._get_pip_whl_path_ctx() as bundled_wheel_path:
38 self.assertEqual(pip_filename, bundled_wheel_path.name)
39
40 def test_selected_wheel_path_with_dir(self):
41 # Test _get_pip_whl_path_ctx() with a wheel package directory
42 pip_filename = "pip-20.2.2-py2.py3-none-any.whl"
43
44 with tempfile.TemporaryDirectory() as tmpdir:
45 self.touch(tmpdir, pip_filename)
46 # not used, make sure that they're ignored
47 self.touch(tmpdir, "pip-1.2.3-py2.py3-none-any.whl")
48 self.touch(tmpdir, "wheel-0.34.2-py2.py3-none-any.whl")
49 self.touch(tmpdir, "pip-script.py")
50
51 with unittest.mock.patch.object(ensurepip, '_WHEEL_PKG_DIR', Path(tmpdir)):
52 with ensurepip._get_pip_whl_path_ctx() as bundled_wheel_path:
53 self.assertEqual(pip_filename, bundled_wheel_path.name)
54
55
56 class EnsurepipMixin:
57
58 def setUp(self):
59 run_pip_patch = unittest.mock.patch("ensurepip._run_pip")
60 self.run_pip = run_pip_patch.start()
61 self.run_pip.return_value = 0
62 self.addCleanup(run_pip_patch.stop)
63
64 # Avoid side effects on the actual os module
65 real_devnull = os.devnull
66 os_patch = unittest.mock.patch("ensurepip.os")
67 patched_os = os_patch.start()
68 # But expose os.listdir() used by _find_wheel_pkg_dir_pip()
69 patched_os.listdir = os.listdir
70 self.addCleanup(os_patch.stop)
71 patched_os.devnull = real_devnull
72 patched_os.path = os.path
73 self.os_environ = patched_os.environ = os.environ.copy()
74
75
76 class TestBootstrap(EnsurepipMixin, unittest.TestCase):
77
78 def test_basic_bootstrapping(self):
79 ensurepip.bootstrap()
80
81 self.run_pip.assert_called_once_with(
82 [
83 "install", "--no-cache-dir", "--no-index", "--find-links",
84 unittest.mock.ANY, "pip",
85 ],
86 unittest.mock.ANY,
87 )
88
89 additional_paths = self.run_pip.call_args[0][1]
90 self.assertEqual(len(additional_paths), 1)
91
92 def test_bootstrapping_with_root(self):
93 ensurepip.bootstrap(root="/foo/bar/")
94
95 self.run_pip.assert_called_once_with(
96 [
97 "install", "--no-cache-dir", "--no-index", "--find-links",
98 unittest.mock.ANY, "--root", "/foo/bar/",
99 "pip",
100 ],
101 unittest.mock.ANY,
102 )
103
104 def test_bootstrapping_with_user(self):
105 ensurepip.bootstrap(user=True)
106
107 self.run_pip.assert_called_once_with(
108 [
109 "install", "--no-cache-dir", "--no-index", "--find-links",
110 unittest.mock.ANY, "--user", "pip",
111 ],
112 unittest.mock.ANY,
113 )
114
115 def test_bootstrapping_with_upgrade(self):
116 ensurepip.bootstrap(upgrade=True)
117
118 self.run_pip.assert_called_once_with(
119 [
120 "install", "--no-cache-dir", "--no-index", "--find-links",
121 unittest.mock.ANY, "--upgrade", "pip",
122 ],
123 unittest.mock.ANY,
124 )
125
126 def test_bootstrapping_with_verbosity_1(self):
127 ensurepip.bootstrap(verbosity=1)
128
129 self.run_pip.assert_called_once_with(
130 [
131 "install", "--no-cache-dir", "--no-index", "--find-links",
132 unittest.mock.ANY, "-v", "pip",
133 ],
134 unittest.mock.ANY,
135 )
136
137 def test_bootstrapping_with_verbosity_2(self):
138 ensurepip.bootstrap(verbosity=2)
139
140 self.run_pip.assert_called_once_with(
141 [
142 "install", "--no-cache-dir", "--no-index", "--find-links",
143 unittest.mock.ANY, "-vv", "pip",
144 ],
145 unittest.mock.ANY,
146 )
147
148 def test_bootstrapping_with_verbosity_3(self):
149 ensurepip.bootstrap(verbosity=3)
150
151 self.run_pip.assert_called_once_with(
152 [
153 "install", "--no-cache-dir", "--no-index", "--find-links",
154 unittest.mock.ANY, "-vvv", "pip",
155 ],
156 unittest.mock.ANY,
157 )
158
159 def test_bootstrapping_with_regular_install(self):
160 ensurepip.bootstrap()
161 self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "install")
162
163 def test_bootstrapping_with_alt_install(self):
164 ensurepip.bootstrap(altinstall=True)
165 self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "altinstall")
166
167 def test_bootstrapping_with_default_pip(self):
168 ensurepip.bootstrap(default_pip=True)
169 self.assertNotIn("ENSUREPIP_OPTIONS", self.os_environ)
170
171 def test_altinstall_default_pip_conflict(self):
172 with self.assertRaises(ValueError):
173 ensurepip.bootstrap(altinstall=True, default_pip=True)
174 self.assertFalse(self.run_pip.called)
175
176 def test_pip_environment_variables_removed(self):
177 # ensurepip deliberately ignores all pip environment variables
178 # See http://bugs.python.org/issue19734 for details
179 self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder"
180 ensurepip.bootstrap()
181 self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
182
183 def test_pip_config_file_disabled(self):
184 # ensurepip deliberately ignores the pip config file
185 # See http://bugs.python.org/issue20053 for details
186 ensurepip.bootstrap()
187 self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
188
189 @contextlib.contextmanager
190 def fake_pip(version=ensurepip.version()):
191 if version is None:
192 pip = None
193 else:
194 class FakePip():
195 __version__ = version
196 pip = FakePip()
197 sentinel = object()
198 orig_pip = sys.modules.get("pip", sentinel)
199 sys.modules["pip"] = pip
200 try:
201 yield pip
202 finally:
203 if orig_pip is sentinel:
204 del sys.modules["pip"]
205 else:
206 sys.modules["pip"] = orig_pip
207
208 class TestUninstall(EnsurepipMixin, unittest.TestCase):
209
210 def test_uninstall_skipped_when_not_installed(self):
211 with fake_pip(None):
212 ensurepip._uninstall_helper()
213 self.assertFalse(self.run_pip.called)
214
215 def test_uninstall_skipped_with_warning_for_wrong_version(self):
216 with fake_pip("not a valid version"):
217 with test.support.captured_stderr() as stderr:
218 ensurepip._uninstall_helper()
219 warning = stderr.getvalue().strip()
220 self.assertIn("only uninstall a matching version", warning)
221 self.assertFalse(self.run_pip.called)
222
223
224 def test_uninstall(self):
225 with fake_pip():
226 ensurepip._uninstall_helper()
227
228 self.run_pip.assert_called_once_with(
229 [
230 "uninstall", "-y", "--disable-pip-version-check", "pip",
231 ]
232 )
233
234 def test_uninstall_with_verbosity_1(self):
235 with fake_pip():
236 ensurepip._uninstall_helper(verbosity=1)
237
238 self.run_pip.assert_called_once_with(
239 [
240 "uninstall", "-y", "--disable-pip-version-check", "-v", "pip",
241 ]
242 )
243
244 def test_uninstall_with_verbosity_2(self):
245 with fake_pip():
246 ensurepip._uninstall_helper(verbosity=2)
247
248 self.run_pip.assert_called_once_with(
249 [
250 "uninstall", "-y", "--disable-pip-version-check", "-vv", "pip",
251 ]
252 )
253
254 def test_uninstall_with_verbosity_3(self):
255 with fake_pip():
256 ensurepip._uninstall_helper(verbosity=3)
257
258 self.run_pip.assert_called_once_with(
259 [
260 "uninstall", "-y", "--disable-pip-version-check", "-vvv",
261 "pip"
262 ]
263 )
264
265 def test_pip_environment_variables_removed(self):
266 # ensurepip deliberately ignores all pip environment variables
267 # See http://bugs.python.org/issue19734 for details
268 self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder"
269 with fake_pip():
270 ensurepip._uninstall_helper()
271 self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
272
273 def test_pip_config_file_disabled(self):
274 # ensurepip deliberately ignores the pip config file
275 # See http://bugs.python.org/issue20053 for details
276 with fake_pip():
277 ensurepip._uninstall_helper()
278 self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
279
280
281 # Basic testing of the main functions and their argument parsing
282
283 EXPECTED_VERSION_OUTPUT = "pip " + ensurepip.version()
284
285 class TestBootstrappingMainFunction(EnsurepipMixin, unittest.TestCase):
286
287 def test_bootstrap_version(self):
288 with test.support.captured_stdout() as stdout:
289 with self.assertRaises(SystemExit):
290 ensurepip._main(["--version"])
291 result = stdout.getvalue().strip()
292 self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
293 self.assertFalse(self.run_pip.called)
294
295 def test_basic_bootstrapping(self):
296 exit_code = ensurepip._main([])
297
298 self.run_pip.assert_called_once_with(
299 [
300 "install", "--no-cache-dir", "--no-index", "--find-links",
301 unittest.mock.ANY, "pip",
302 ],
303 unittest.mock.ANY,
304 )
305
306 additional_paths = self.run_pip.call_args[0][1]
307 self.assertEqual(len(additional_paths), 1)
308 self.assertEqual(exit_code, 0)
309
310 def test_bootstrapping_error_code(self):
311 self.run_pip.return_value = 2
312 exit_code = ensurepip._main([])
313 self.assertEqual(exit_code, 2)
314
315
316 class TestUninstallationMainFunction(EnsurepipMixin, unittest.TestCase):
317
318 def test_uninstall_version(self):
319 with test.support.captured_stdout() as stdout:
320 with self.assertRaises(SystemExit):
321 ensurepip._uninstall._main(["--version"])
322 result = stdout.getvalue().strip()
323 self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
324 self.assertFalse(self.run_pip.called)
325
326 def test_basic_uninstall(self):
327 with fake_pip():
328 exit_code = ensurepip._uninstall._main([])
329
330 self.run_pip.assert_called_once_with(
331 [
332 "uninstall", "-y", "--disable-pip-version-check", "pip",
333 ]
334 )
335
336 self.assertEqual(exit_code, 0)
337
338 def test_uninstall_error_code(self):
339 with fake_pip():
340 self.run_pip.return_value = 2
341 exit_code = ensurepip._uninstall._main([])
342 self.assertEqual(exit_code, 2)
343
344
345 if __name__ == "__main__":
346 unittest.main()