blob: 9b04c18b0e296882487642744f54784946eb9607 [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()
23 self.addCleanup(run_pip_patch.stop)
24
Nick Coghland3e83e22013-11-13 22:24:58 +100025 # Avoid side effects on the actual os module
Nick Coghlan6edd82a2014-02-04 23:02:36 +100026 real_devnull = os.devnull
Nick Coghland3e83e22013-11-13 22:24:58 +100027 os_patch = unittest.mock.patch("ensurepip.os")
28 patched_os = os_patch.start()
29 self.addCleanup(os_patch.stop)
Nick Coghlan6edd82a2014-02-04 23:02:36 +100030 patched_os.devnull = real_devnull
Nick Coghland3e83e22013-11-13 22:24:58 +100031 patched_os.path = os.path
32 self.os_environ = patched_os.environ = os.environ.copy()
Nick Coghland0cf0632013-11-11 22:11:55 +100033
Nick Coghlanae2ee962013-12-23 23:07:07 +100034
Nick Coghlanf71cae02013-12-23 18:20:34 +100035class TestBootstrap(EnsurepipMixin, unittest.TestCase):
36
Nick Coghland0cf0632013-11-11 22:11:55 +100037 def test_basic_bootstrapping(self):
38 ensurepip.bootstrap()
39
40 self.run_pip.assert_called_once_with(
41 [
42 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -050043 unittest.mock.ANY, "setuptools", "pip",
Nick Coghland0cf0632013-11-11 22:11:55 +100044 ],
45 unittest.mock.ANY,
46 )
47
48 additional_paths = self.run_pip.call_args[0][1]
49 self.assertEqual(len(additional_paths), 2)
50
51 def test_bootstrapping_with_root(self):
52 ensurepip.bootstrap(root="/foo/bar/")
53
54 self.run_pip.assert_called_once_with(
55 [
56 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -050057 unittest.mock.ANY, "--root", "/foo/bar/",
Nick Coghland0cf0632013-11-11 22:11:55 +100058 "setuptools", "pip",
59 ],
60 unittest.mock.ANY,
61 )
62
63 def test_bootstrapping_with_user(self):
64 ensurepip.bootstrap(user=True)
65
66 self.run_pip.assert_called_once_with(
67 [
68 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -050069 unittest.mock.ANY, "--user", "setuptools", "pip",
Nick Coghland0cf0632013-11-11 22:11:55 +100070 ],
71 unittest.mock.ANY,
72 )
73
74 def test_bootstrapping_with_upgrade(self):
75 ensurepip.bootstrap(upgrade=True)
76
77 self.run_pip.assert_called_once_with(
78 [
79 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -050080 unittest.mock.ANY, "--upgrade", "setuptools", "pip",
Nick Coghland0cf0632013-11-11 22:11:55 +100081 ],
82 unittest.mock.ANY,
83 )
84
85 def test_bootstrapping_with_verbosity_1(self):
86 ensurepip.bootstrap(verbosity=1)
87
88 self.run_pip.assert_called_once_with(
89 [
90 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -050091 unittest.mock.ANY, "-v", "setuptools", "pip",
Nick Coghland0cf0632013-11-11 22:11:55 +100092 ],
93 unittest.mock.ANY,
94 )
95
96 def test_bootstrapping_with_verbosity_2(self):
97 ensurepip.bootstrap(verbosity=2)
98
99 self.run_pip.assert_called_once_with(
100 [
101 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -0500102 unittest.mock.ANY, "-vv", "setuptools", "pip",
Nick Coghland0cf0632013-11-11 22:11:55 +1000103 ],
104 unittest.mock.ANY,
105 )
106
107 def test_bootstrapping_with_verbosity_3(self):
108 ensurepip.bootstrap(verbosity=3)
109
110 self.run_pip.assert_called_once_with(
111 [
112 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -0500113 unittest.mock.ANY, "-vvv", "setuptools", "pip",
Nick Coghland0cf0632013-11-11 22:11:55 +1000114 ],
115 unittest.mock.ANY,
116 )
117
118 def test_bootstrapping_with_regular_install(self):
119 ensurepip.bootstrap()
120 self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "install")
121
122 def test_bootstrapping_with_alt_install(self):
123 ensurepip.bootstrap(altinstall=True)
124 self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "altinstall")
125
126 def test_bootstrapping_with_default_pip(self):
127 ensurepip.bootstrap(default_pip=True)
128 self.assertNotIn("ENSUREPIP_OPTIONS", self.os_environ)
129
130 def test_altinstall_default_pip_conflict(self):
131 with self.assertRaises(ValueError):
132 ensurepip.bootstrap(altinstall=True, default_pip=True)
Benjamin Petersonc844b002014-04-16 16:06:39 -0400133 self.assertFalse(self.run_pip.called)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000134
Nick Coghlan6256fcb2013-12-23 16:16:07 +1000135 def test_pip_environment_variables_removed(self):
136 # ensurepip deliberately ignores all pip environment variables
137 # See http://bugs.python.org/issue19734 for details
138 self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder"
139 ensurepip.bootstrap()
140 self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
141
Nick Coghlan6edd82a2014-02-04 23:02:36 +1000142 def test_pip_config_file_disabled(self):
143 # ensurepip deliberately ignores the pip config file
144 # See http://bugs.python.org/issue20053 for details
145 ensurepip.bootstrap()
146 self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
Nick Coghlan6256fcb2013-12-23 16:16:07 +1000147
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000148@contextlib.contextmanager
149def fake_pip(version=ensurepip._PIP_VERSION):
150 if version is None:
151 pip = None
152 else:
153 class FakePip():
154 __version__ = version
155 pip = FakePip()
156 sentinel = object()
157 orig_pip = sys.modules.get("pip", sentinel)
158 sys.modules["pip"] = pip
159 try:
160 yield pip
161 finally:
162 if orig_pip is sentinel:
163 del sys.modules["pip"]
164 else:
165 sys.modules["pip"] = orig_pip
166
Nick Coghlanf71cae02013-12-23 18:20:34 +1000167class TestUninstall(EnsurepipMixin, unittest.TestCase):
Nick Coghlaned9af522013-12-23 17:39:12 +1000168
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000169 def test_uninstall_skipped_when_not_installed(self):
170 with fake_pip(None):
Nick Coghlanf71cae02013-12-23 18:20:34 +1000171 ensurepip._uninstall_helper()
Benjamin Petersonc844b002014-04-16 16:06:39 -0400172 self.assertFalse(self.run_pip.called)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000173
Nick Coghlana46cf122014-02-28 23:35:05 +1000174 def test_uninstall_skipped_with_warning_for_wrong_version(self):
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000175 with fake_pip("not a valid version"):
Nick Coghlana46cf122014-02-28 23:35:05 +1000176 with test.support.captured_stderr() as stderr:
Nick Coghlanf71cae02013-12-23 18:20:34 +1000177 ensurepip._uninstall_helper()
Nick Coghlana46cf122014-02-28 23:35:05 +1000178 warning = stderr.getvalue().strip()
179 self.assertIn("only uninstall a matching version", warning)
Benjamin Petersonc844b002014-04-16 16:06:39 -0400180 self.assertFalse(self.run_pip.called)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000181
182
183 def test_uninstall(self):
184 with fake_pip():
Nick Coghlanf71cae02013-12-23 18:20:34 +1000185 ensurepip._uninstall_helper()
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000186
187 self.run_pip.assert_called_once_with(
Donald Stufft71a85892015-06-02 10:37:08 -0400188 [
189 "uninstall", "-y", "--disable-pip-version-check", "pip",
190 "setuptools",
191 ]
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000192 )
193
194 def test_uninstall_with_verbosity_1(self):
195 with fake_pip():
Nick Coghlanf71cae02013-12-23 18:20:34 +1000196 ensurepip._uninstall_helper(verbosity=1)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000197
198 self.run_pip.assert_called_once_with(
Donald Stufft71a85892015-06-02 10:37:08 -0400199 [
200 "uninstall", "-y", "--disable-pip-version-check", "-v", "pip",
201 "setuptools",
202 ]
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000203 )
204
205 def test_uninstall_with_verbosity_2(self):
206 with fake_pip():
Nick Coghlanf71cae02013-12-23 18:20:34 +1000207 ensurepip._uninstall_helper(verbosity=2)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000208
209 self.run_pip.assert_called_once_with(
Donald Stufft71a85892015-06-02 10:37:08 -0400210 [
211 "uninstall", "-y", "--disable-pip-version-check", "-vv", "pip",
212 "setuptools",
213 ]
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000214 )
215
216 def test_uninstall_with_verbosity_3(self):
217 with fake_pip():
Nick Coghlanf71cae02013-12-23 18:20:34 +1000218 ensurepip._uninstall_helper(verbosity=3)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000219
220 self.run_pip.assert_called_once_with(
Donald Stufft71a85892015-06-02 10:37:08 -0400221 [
222 "uninstall", "-y", "--disable-pip-version-check", "-vvv",
223 "pip", "setuptools",
224 ]
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000225 )
226
Nick Coghlaned9af522013-12-23 17:39:12 +1000227 def test_pip_environment_variables_removed(self):
228 # ensurepip deliberately ignores all pip environment variables
229 # See http://bugs.python.org/issue19734 for details
230 self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder"
231 with fake_pip():
Nick Coghlanf71cae02013-12-23 18:20:34 +1000232 ensurepip._uninstall_helper()
Nick Coghlaned9af522013-12-23 17:39:12 +1000233 self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ)
Nick Coghlanfdf3a622013-11-30 17:15:09 +1000234
Nick Coghlan6edd82a2014-02-04 23:02:36 +1000235 def test_pip_config_file_disabled(self):
236 # ensurepip deliberately ignores the pip config file
237 # See http://bugs.python.org/issue20053 for details
238 with fake_pip():
239 ensurepip._uninstall_helper()
240 self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull)
241
Nick Coghland0cf0632013-11-11 22:11:55 +1000242
Nick Coghlanf71cae02013-12-23 18:20:34 +1000243# Basic testing of the main functions and their argument parsing
244
245EXPECTED_VERSION_OUTPUT = "pip " + ensurepip._PIP_VERSION
246
247class TestBootstrappingMainFunction(EnsurepipMixin, unittest.TestCase):
248
249 def test_bootstrap_version(self):
250 with test.support.captured_stdout() as stdout:
251 with self.assertRaises(SystemExit):
252 ensurepip._main(["--version"])
253 result = stdout.getvalue().strip()
254 self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
Benjamin Petersonc844b002014-04-16 16:06:39 -0400255 self.assertFalse(self.run_pip.called)
Nick Coghlanf71cae02013-12-23 18:20:34 +1000256
257 def test_basic_bootstrapping(self):
258 ensurepip._main([])
259
260 self.run_pip.assert_called_once_with(
261 [
262 "install", "--no-index", "--find-links",
Donald Stufftf7f58f82014-01-02 09:33:35 -0500263 unittest.mock.ANY, "setuptools", "pip",
Nick Coghlanf71cae02013-12-23 18:20:34 +1000264 ],
265 unittest.mock.ANY,
266 )
267
268 additional_paths = self.run_pip.call_args[0][1]
269 self.assertEqual(len(additional_paths), 2)
270
271class TestUninstallationMainFunction(EnsurepipMixin, unittest.TestCase):
272
273 def test_uninstall_version(self):
274 with test.support.captured_stdout() as stdout:
275 with self.assertRaises(SystemExit):
276 ensurepip._uninstall._main(["--version"])
277 result = stdout.getvalue().strip()
278 self.assertEqual(result, EXPECTED_VERSION_OUTPUT)
Benjamin Petersonc844b002014-04-16 16:06:39 -0400279 self.assertFalse(self.run_pip.called)
Nick Coghlanf71cae02013-12-23 18:20:34 +1000280
281 def test_basic_uninstall(self):
282 with fake_pip():
283 ensurepip._uninstall._main([])
284
285 self.run_pip.assert_called_once_with(
Donald Stufft71a85892015-06-02 10:37:08 -0400286 [
287 "uninstall", "-y", "--disable-pip-version-check", "pip",
288 "setuptools",
289 ]
Nick Coghlanf71cae02013-12-23 18:20:34 +1000290 )
291
292
Nick Coghland0cf0632013-11-11 22:11:55 +1000293
294if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500295 unittest.main()