blob: 89966893092851cc5296a741177dc5538d1ea8a2 [file] [log] [blame]
Nick Coghland0cf0632013-11-11 22:11:55 +10001import unittest
2import unittest.mock
Nick Coghland0cf0632013-11-11 22:11:55 +10003import test.support
Nick Coghland3e83e22013-11-13 22:24:58 +10004import os
5import os.path
Nick Coghlanfdf3a622013-11-30 17:15:09 +10006import contextlib
7import sys
Nick Coghland0cf0632013-11-11 22:11:55 +10008
Nick Coghlanf71cae02013-12-23 18:20:34 +10009import ensurepip
10import ensurepip._uninstall
Nick Coghland0cf0632013-11-11 22:11:55 +100011
Nick Coghlanae2ee962013-12-23 23:07:07 +100012
Nick Coghland0cf0632013-11-11 22:11:55 +100013class TestEnsurePipVersion(unittest.TestCase):
14
15 def test_returns_version(self):
16 self.assertEqual(ensurepip._PIP_VERSION, ensurepip.version())
17
Nick Coghlanf71cae02013-12-23 18:20:34 +100018class EnsurepipMixin:
Nick Coghland0cf0632013-11-11 22:11:55 +100019
20 def setUp(self):
21 run_pip_patch = unittest.mock.patch("ensurepip._run_pip")
22 self.run_pip = run_pip_patch.start()
Igor Filatov9adda0c2017-09-21 13:07:45 +030023 self.run_pip.return_value = 0
Nick Coghland0cf0632013-11-11 22:11:55 +100024 self.addCleanup(run_pip_patch.stop)
25
Nick Coghland3e83e22013-11-13 22:24:58 +100026 # Avoid side effects on the actual os module
Nick Coghlan6edd82a2014-02-04 23:02:36 +100027 real_devnull = os.devnull
Nick Coghland3e83e22013-11-13 22:24:58 +100028 os_patch = unittest.mock.patch("ensurepip.os")
29 patched_os = os_patch.start()
30 self.addCleanup(os_patch.stop)
Nick Coghlan6edd82a2014-02-04 23:02:36 +100031 patched_os.devnull = real_devnull
Nick Coghland3e83e22013-11-13 22:24:58 +100032 patched_os.path = os.path
33 self.os_environ = patched_os.environ = os.environ.copy()
Nick Coghland0cf0632013-11-11 22:11:55 +100034
Nick Coghlanae2ee962013-12-23 23:07:07 +100035
Nick Coghlanf71cae02013-12-23 18:20:34 +100036class TestBootstrap(EnsurepipMixin, unittest.TestCase):
37
Nick Coghland0cf0632013-11-11 22:11:55 +100038 def test_basic_bootstrapping(self):
39 ensurepip.bootstrap()
40
41 self.run_pip.assert_called_once_with(
42 [
43 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -050044 unittest.mock.ANY, "setuptools", "pip",
Nick Coghland0cf0632013-11-11 22:11:55 +100045 ],
46 unittest.mock.ANY,
47 )
48
49 additional_paths = self.run_pip.call_args[0][1]
50 self.assertEqual(len(additional_paths), 2)
51
52 def test_bootstrapping_with_root(self):
53 ensurepip.bootstrap(root="/foo/bar/")
54
55 self.run_pip.assert_called_once_with(
56 [
57 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -050058 unittest.mock.ANY, "--root", "/foo/bar/",
Nick Coghland0cf0632013-11-11 22:11:55 +100059 "setuptools", "pip",
60 ],
61 unittest.mock.ANY,
62 )
63
64 def test_bootstrapping_with_user(self):
65 ensurepip.bootstrap(user=True)
66
67 self.run_pip.assert_called_once_with(
68 [
69 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -050070 unittest.mock.ANY, "--user", "setuptools", "pip",
Nick Coghland0cf0632013-11-11 22:11:55 +100071 ],
72 unittest.mock.ANY,
73 )
74
75 def test_bootstrapping_with_upgrade(self):
76 ensurepip.bootstrap(upgrade=True)
77
78 self.run_pip.assert_called_once_with(
79 [
80 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -050081 unittest.mock.ANY, "--upgrade", "setuptools", "pip",
Nick Coghland0cf0632013-11-11 22:11:55 +100082 ],
83 unittest.mock.ANY,
84 )
85
86 def test_bootstrapping_with_verbosity_1(self):
87 ensurepip.bootstrap(verbosity=1)
88
89 self.run_pip.assert_called_once_with(
90 [
91 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -050092 unittest.mock.ANY, "-v", "setuptools", "pip",
Nick Coghland0cf0632013-11-11 22:11:55 +100093 ],
94 unittest.mock.ANY,
95 )
96
97 def test_bootstrapping_with_verbosity_2(self):
98 ensurepip.bootstrap(verbosity=2)
99
100 self.run_pip.assert_called_once_with(
101 [
102 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -0500103 unittest.mock.ANY, "-vv", "setuptools", "pip",
Nick Coghland0cf0632013-11-11 22:11:55 +1000104 ],
105 unittest.mock.ANY,
106 )
107
108 def test_bootstrapping_with_verbosity_3(self):
109 ensurepip.bootstrap(verbosity=3)
110
111 self.run_pip.assert_called_once_with(
112 [
113 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -0500114 unittest.mock.ANY, "-vvv", "setuptools", "pip",
Nick Coghland0cf0632013-11-11 22:11:55 +1000115 ],
116 unittest.mock.ANY,
117 )
118
119 def test_bootstrapping_with_regular_install(self):
120 ensurepip.bootstrap()
121 self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "install")
122
123 def test_bootstrapping_with_alt_install(self):
124 ensurepip.bootstrap(altinstall=True)
125 self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "altinstall")
126
127 def test_bootstrapping_with_default_pip(self):
128 ensurepip.bootstrap(default_pip=True)
129 self.assertNotIn("ENSUREPIP_OPTIONS", self.os_environ)
130
131 def test_altinstall_default_pip_conflict(self):
132 with self.assertRaises(ValueError):
133 ensurepip.bootstrap(altinstall=True, default_pip=True)
Benjamin Petersonc844b002014-04-16 16:06:39 -0400134 self.assertFalse(self.run_pip.called)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000135
Nick Coghlan6256fcb2013-12-23 16:16:07 +1000136 def test_pip_environment_variables_removed(self):
137 # ensurepip deliberately ignores all pip environment variables
138 # See http://bugs.python.org/issue19734 for details
139 self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder"
140 ensurepip.bootstrap()
141 self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
142
Nick Coghlan6edd82a2014-02-04 23:02:36 +1000143 def test_pip_config_file_disabled(self):
144 # ensurepip deliberately ignores the pip config file
145 # See http://bugs.python.org/issue20053 for details
146 ensurepip.bootstrap()
147 self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
Nick Coghlan6256fcb2013-12-23 16:16:07 +1000148
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000149@contextlib.contextmanager
150def fake_pip(version=ensurepip._PIP_VERSION):
151 if version is None:
152 pip = None
153 else:
154 class FakePip():
155 __version__ = version
156 pip = FakePip()
157 sentinel = object()
158 orig_pip = sys.modules.get("pip", sentinel)
159 sys.modules["pip"] = pip
160 try:
161 yield pip
162 finally:
163 if orig_pip is sentinel:
164 del sys.modules["pip"]
165 else:
166 sys.modules["pip"] = orig_pip
167
Nick Coghlanf71cae02013-12-23 18:20:34 +1000168class TestUninstall(EnsurepipMixin, unittest.TestCase):
Nick Coghlaned9af522013-12-23 17:39:12 +1000169
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000170 def test_uninstall_skipped_when_not_installed(self):
171 with fake_pip(None):
Nick Coghlanf71cae02013-12-23 18:20:34 +1000172 ensurepip._uninstall_helper()
Benjamin Petersonc844b002014-04-16 16:06:39 -0400173 self.assertFalse(self.run_pip.called)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000174
Nick Coghlana46cf122014-02-28 23:35:05 +1000175 def test_uninstall_skipped_with_warning_for_wrong_version(self):
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000176 with fake_pip("not a valid version"):
Nick Coghlana46cf122014-02-28 23:35:05 +1000177 with test.support.captured_stderr() as stderr:
Nick Coghlanf71cae02013-12-23 18:20:34 +1000178 ensurepip._uninstall_helper()
Nick Coghlana46cf122014-02-28 23:35:05 +1000179 warning = stderr.getvalue().strip()
180 self.assertIn("only uninstall a matching version", warning)
Benjamin Petersonc844b002014-04-16 16:06:39 -0400181 self.assertFalse(self.run_pip.called)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000182
183
184 def test_uninstall(self):
185 with fake_pip():
Nick Coghlanf71cae02013-12-23 18:20:34 +1000186 ensurepip._uninstall_helper()
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000187
188 self.run_pip.assert_called_once_with(
Donald Stufft71a85892015-06-02 10:37:08 -0400189 [
190 "uninstall", "-y", "--disable-pip-version-check", "pip",
191 "setuptools",
192 ]
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000193 )
194
195 def test_uninstall_with_verbosity_1(self):
196 with fake_pip():
Nick Coghlanf71cae02013-12-23 18:20:34 +1000197 ensurepip._uninstall_helper(verbosity=1)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000198
199 self.run_pip.assert_called_once_with(
Donald Stufft71a85892015-06-02 10:37:08 -0400200 [
201 "uninstall", "-y", "--disable-pip-version-check", "-v", "pip",
202 "setuptools",
203 ]
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000204 )
205
206 def test_uninstall_with_verbosity_2(self):
207 with fake_pip():
Nick Coghlanf71cae02013-12-23 18:20:34 +1000208 ensurepip._uninstall_helper(verbosity=2)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000209
210 self.run_pip.assert_called_once_with(
Donald Stufft71a85892015-06-02 10:37:08 -0400211 [
212 "uninstall", "-y", "--disable-pip-version-check", "-vv", "pip",
213 "setuptools",
214 ]
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000215 )
216
217 def test_uninstall_with_verbosity_3(self):
218 with fake_pip():
Nick Coghlanf71cae02013-12-23 18:20:34 +1000219 ensurepip._uninstall_helper(verbosity=3)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000220
221 self.run_pip.assert_called_once_with(
Donald Stufft71a85892015-06-02 10:37:08 -0400222 [
223 "uninstall", "-y", "--disable-pip-version-check", "-vvv",
224 "pip", "setuptools",
225 ]
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000226 )
227
Nick Coghlaned9af522013-12-23 17:39:12 +1000228 def test_pip_environment_variables_removed(self):
229 # ensurepip deliberately ignores all pip environment variables
230 # See http://bugs.python.org/issue19734 for details
231 self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder"
232 with fake_pip():
Nick Coghlanf71cae02013-12-23 18:20:34 +1000233 ensurepip._uninstall_helper()
Nick Coghlaned9af522013-12-23 17:39:12 +1000234 self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000235
Nick Coghlan6edd82a2014-02-04 23:02:36 +1000236 def test_pip_config_file_disabled(self):
237 # ensurepip deliberately ignores the pip config file
238 # See http://bugs.python.org/issue20053 for details
239 with fake_pip():
240 ensurepip._uninstall_helper()
241 self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
242
Nick Coghland0cf0632013-11-11 22:11:55 +1000243
Nick Coghlanf71cae02013-12-23 18:20:34 +1000244# Basic testing of the main functions and their argument parsing
245
246EXPECTED_VERSION_OUTPUT = "pip " + ensurepip._PIP_VERSION
247
248class TestBootstrappingMainFunction(EnsurepipMixin, unittest.TestCase):
249
250 def test_bootstrap_version(self):
251 with test.support.captured_stdout() as stdout:
252 with self.assertRaises(SystemExit):
253 ensurepip._main(["--version"])
254 result = stdout.getvalue().strip()
255 self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
Benjamin Petersonc844b002014-04-16 16:06:39 -0400256 self.assertFalse(self.run_pip.called)
Nick Coghlanf71cae02013-12-23 18:20:34 +1000257
258 def test_basic_bootstrapping(self):
Igor Filatov9adda0c2017-09-21 13:07:45 +0300259 exit_code = ensurepip._main([])
Nick Coghlanf71cae02013-12-23 18:20:34 +1000260
261 self.run_pip.assert_called_once_with(
262 [
263 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -0500264 unittest.mock.ANY, "setuptools", "pip",
Nick Coghlanf71cae02013-12-23 18:20:34 +1000265 ],
266 unittest.mock.ANY,
267 )
268
269 additional_paths = self.run_pip.call_args[0][1]
270 self.assertEqual(len(additional_paths), 2)
Igor Filatov9adda0c2017-09-21 13:07:45 +0300271 self.assertEqual(exit_code, 0)
272
273 def test_bootstrapping_error_code(self):
274 self.run_pip.return_value = 2
275 exit_code = ensurepip._main([])
276 self.assertEqual(exit_code, 2)
277
Nick Coghlanf71cae02013-12-23 18:20:34 +1000278
279class TestUninstallationMainFunction(EnsurepipMixin, unittest.TestCase):
280
281 def test_uninstall_version(self):
282 with test.support.captured_stdout() as stdout:
283 with self.assertRaises(SystemExit):
284 ensurepip._uninstall._main(["--version"])
285 result = stdout.getvalue().strip()
286 self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
Benjamin Petersonc844b002014-04-16 16:06:39 -0400287 self.assertFalse(self.run_pip.called)
Nick Coghlanf71cae02013-12-23 18:20:34 +1000288
289 def test_basic_uninstall(self):
290 with fake_pip():
Igor Filatov9adda0c2017-09-21 13:07:45 +0300291 exit_code = ensurepip._uninstall._main([])
Nick Coghlanf71cae02013-12-23 18:20:34 +1000292
293 self.run_pip.assert_called_once_with(
Donald Stufft71a85892015-06-02 10:37:08 -0400294 [
295 "uninstall", "-y", "--disable-pip-version-check", "pip",
296 "setuptools",
297 ]
Nick Coghlanf71cae02013-12-23 18:20:34 +1000298 )
299
Igor Filatov9adda0c2017-09-21 13:07:45 +0300300 self.assertEqual(exit_code, 0)
301
302 def test_uninstall_error_code(self):
303 with fake_pip():
304 self.run_pip.return_value = 2
305 exit_code = ensurepip._uninstall._main([])
306 self.assertEqual(exit_code, 2)
Nick Coghlanf71cae02013-12-23 18:20:34 +1000307
Nick Coghland0cf0632013-11-11 22:11:55 +1000308
309if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500310 unittest.main()