1 | n/a | import unittest |
---|
2 | n/a | import unittest.mock |
---|
3 | n/a | import test.support |
---|
4 | n/a | import os |
---|
5 | n/a | import os.path |
---|
6 | n/a | import contextlib |
---|
7 | n/a | import sys |
---|
8 | n/a | |
---|
9 | n/a | import ensurepip |
---|
10 | n/a | import ensurepip._uninstall |
---|
11 | n/a | |
---|
12 | n/a | |
---|
13 | n/a | class TestEnsurePipVersion(unittest.TestCase): |
---|
14 | n/a | |
---|
15 | n/a | def test_returns_version(self): |
---|
16 | n/a | self.assertEqual(ensurepip._PIP_VERSION, ensurepip.version()) |
---|
17 | n/a | |
---|
18 | n/a | class EnsurepipMixin: |
---|
19 | n/a | |
---|
20 | n/a | def setUp(self): |
---|
21 | n/a | run_pip_patch = unittest.mock.patch("ensurepip._run_pip") |
---|
22 | n/a | self.run_pip = run_pip_patch.start() |
---|
23 | n/a | self.addCleanup(run_pip_patch.stop) |
---|
24 | n/a | |
---|
25 | n/a | # Avoid side effects on the actual os module |
---|
26 | n/a | real_devnull = os.devnull |
---|
27 | n/a | os_patch = unittest.mock.patch("ensurepip.os") |
---|
28 | n/a | patched_os = os_patch.start() |
---|
29 | n/a | self.addCleanup(os_patch.stop) |
---|
30 | n/a | patched_os.devnull = real_devnull |
---|
31 | n/a | patched_os.path = os.path |
---|
32 | n/a | self.os_environ = patched_os.environ = os.environ.copy() |
---|
33 | n/a | |
---|
34 | n/a | |
---|
35 | n/a | class TestBootstrap(EnsurepipMixin, unittest.TestCase): |
---|
36 | n/a | |
---|
37 | n/a | def test_basic_bootstrapping(self): |
---|
38 | n/a | ensurepip.bootstrap() |
---|
39 | n/a | |
---|
40 | n/a | self.run_pip.assert_called_once_with( |
---|
41 | n/a | [ |
---|
42 | n/a | "install", "--no-index", "--find-links", |
---|
43 | n/a | unittest.mock.ANY, "setuptools", "pip", |
---|
44 | n/a | ], |
---|
45 | n/a | unittest.mock.ANY, |
---|
46 | n/a | ) |
---|
47 | n/a | |
---|
48 | n/a | additional_paths = self.run_pip.call_args[0][1] |
---|
49 | n/a | self.assertEqual(len(additional_paths), 2) |
---|
50 | n/a | |
---|
51 | n/a | def test_bootstrapping_with_root(self): |
---|
52 | n/a | ensurepip.bootstrap(root="/foo/bar/") |
---|
53 | n/a | |
---|
54 | n/a | self.run_pip.assert_called_once_with( |
---|
55 | n/a | [ |
---|
56 | n/a | "install", "--no-index", "--find-links", |
---|
57 | n/a | unittest.mock.ANY, "--root", "/foo/bar/", |
---|
58 | n/a | "setuptools", "pip", |
---|
59 | n/a | ], |
---|
60 | n/a | unittest.mock.ANY, |
---|
61 | n/a | ) |
---|
62 | n/a | |
---|
63 | n/a | def test_bootstrapping_with_user(self): |
---|
64 | n/a | ensurepip.bootstrap(user=True) |
---|
65 | n/a | |
---|
66 | n/a | self.run_pip.assert_called_once_with( |
---|
67 | n/a | [ |
---|
68 | n/a | "install", "--no-index", "--find-links", |
---|
69 | n/a | unittest.mock.ANY, "--user", "setuptools", "pip", |
---|
70 | n/a | ], |
---|
71 | n/a | unittest.mock.ANY, |
---|
72 | n/a | ) |
---|
73 | n/a | |
---|
74 | n/a | def test_bootstrapping_with_upgrade(self): |
---|
75 | n/a | ensurepip.bootstrap(upgrade=True) |
---|
76 | n/a | |
---|
77 | n/a | self.run_pip.assert_called_once_with( |
---|
78 | n/a | [ |
---|
79 | n/a | "install", "--no-index", "--find-links", |
---|
80 | n/a | unittest.mock.ANY, "--upgrade", "setuptools", "pip", |
---|
81 | n/a | ], |
---|
82 | n/a | unittest.mock.ANY, |
---|
83 | n/a | ) |
---|
84 | n/a | |
---|
85 | n/a | def test_bootstrapping_with_verbosity_1(self): |
---|
86 | n/a | ensurepip.bootstrap(verbosity=1) |
---|
87 | n/a | |
---|
88 | n/a | self.run_pip.assert_called_once_with( |
---|
89 | n/a | [ |
---|
90 | n/a | "install", "--no-index", "--find-links", |
---|
91 | n/a | unittest.mock.ANY, "-v", "setuptools", "pip", |
---|
92 | n/a | ], |
---|
93 | n/a | unittest.mock.ANY, |
---|
94 | n/a | ) |
---|
95 | n/a | |
---|
96 | n/a | def test_bootstrapping_with_verbosity_2(self): |
---|
97 | n/a | ensurepip.bootstrap(verbosity=2) |
---|
98 | n/a | |
---|
99 | n/a | self.run_pip.assert_called_once_with( |
---|
100 | n/a | [ |
---|
101 | n/a | "install", "--no-index", "--find-links", |
---|
102 | n/a | unittest.mock.ANY, "-vv", "setuptools", "pip", |
---|
103 | n/a | ], |
---|
104 | n/a | unittest.mock.ANY, |
---|
105 | n/a | ) |
---|
106 | n/a | |
---|
107 | n/a | def test_bootstrapping_with_verbosity_3(self): |
---|
108 | n/a | ensurepip.bootstrap(verbosity=3) |
---|
109 | n/a | |
---|
110 | n/a | self.run_pip.assert_called_once_with( |
---|
111 | n/a | [ |
---|
112 | n/a | "install", "--no-index", "--find-links", |
---|
113 | n/a | unittest.mock.ANY, "-vvv", "setuptools", "pip", |
---|
114 | n/a | ], |
---|
115 | n/a | unittest.mock.ANY, |
---|
116 | n/a | ) |
---|
117 | n/a | |
---|
118 | n/a | def test_bootstrapping_with_regular_install(self): |
---|
119 | n/a | ensurepip.bootstrap() |
---|
120 | n/a | self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "install") |
---|
121 | n/a | |
---|
122 | n/a | def test_bootstrapping_with_alt_install(self): |
---|
123 | n/a | ensurepip.bootstrap(altinstall=True) |
---|
124 | n/a | self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "altinstall") |
---|
125 | n/a | |
---|
126 | n/a | def test_bootstrapping_with_default_pip(self): |
---|
127 | n/a | ensurepip.bootstrap(default_pip=True) |
---|
128 | n/a | self.assertNotIn("ENSUREPIP_OPTIONS", self.os_environ) |
---|
129 | n/a | |
---|
130 | n/a | def test_altinstall_default_pip_conflict(self): |
---|
131 | n/a | with self.assertRaises(ValueError): |
---|
132 | n/a | ensurepip.bootstrap(altinstall=True, default_pip=True) |
---|
133 | n/a | self.assertFalse(self.run_pip.called) |
---|
134 | n/a | |
---|
135 | n/a | def test_pip_environment_variables_removed(self): |
---|
136 | n/a | # ensurepip deliberately ignores all pip environment variables |
---|
137 | n/a | # See http://bugs.python.org/issue19734 for details |
---|
138 | n/a | self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder" |
---|
139 | n/a | ensurepip.bootstrap() |
---|
140 | n/a | self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ) |
---|
141 | n/a | |
---|
142 | n/a | def test_pip_config_file_disabled(self): |
---|
143 | n/a | # ensurepip deliberately ignores the pip config file |
---|
144 | n/a | # See http://bugs.python.org/issue20053 for details |
---|
145 | n/a | ensurepip.bootstrap() |
---|
146 | n/a | self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull) |
---|
147 | n/a | |
---|
148 | n/a | @contextlib.contextmanager |
---|
149 | n/a | def fake_pip(version=ensurepip._PIP_VERSION): |
---|
150 | n/a | if version is None: |
---|
151 | n/a | pip = None |
---|
152 | n/a | else: |
---|
153 | n/a | class FakePip(): |
---|
154 | n/a | __version__ = version |
---|
155 | n/a | pip = FakePip() |
---|
156 | n/a | sentinel = object() |
---|
157 | n/a | orig_pip = sys.modules.get("pip", sentinel) |
---|
158 | n/a | sys.modules["pip"] = pip |
---|
159 | n/a | try: |
---|
160 | n/a | yield pip |
---|
161 | n/a | finally: |
---|
162 | n/a | if orig_pip is sentinel: |
---|
163 | n/a | del sys.modules["pip"] |
---|
164 | n/a | else: |
---|
165 | n/a | sys.modules["pip"] = orig_pip |
---|
166 | n/a | |
---|
167 | n/a | class TestUninstall(EnsurepipMixin, unittest.TestCase): |
---|
168 | n/a | |
---|
169 | n/a | def test_uninstall_skipped_when_not_installed(self): |
---|
170 | n/a | with fake_pip(None): |
---|
171 | n/a | ensurepip._uninstall_helper() |
---|
172 | n/a | self.assertFalse(self.run_pip.called) |
---|
173 | n/a | |
---|
174 | n/a | def test_uninstall_skipped_with_warning_for_wrong_version(self): |
---|
175 | n/a | with fake_pip("not a valid version"): |
---|
176 | n/a | with test.support.captured_stderr() as stderr: |
---|
177 | n/a | ensurepip._uninstall_helper() |
---|
178 | n/a | warning = stderr.getvalue().strip() |
---|
179 | n/a | self.assertIn("only uninstall a matching version", warning) |
---|
180 | n/a | self.assertFalse(self.run_pip.called) |
---|
181 | n/a | |
---|
182 | n/a | |
---|
183 | n/a | def test_uninstall(self): |
---|
184 | n/a | with fake_pip(): |
---|
185 | n/a | ensurepip._uninstall_helper() |
---|
186 | n/a | |
---|
187 | n/a | self.run_pip.assert_called_once_with( |
---|
188 | n/a | [ |
---|
189 | n/a | "uninstall", "-y", "--disable-pip-version-check", "pip", |
---|
190 | n/a | "setuptools", |
---|
191 | n/a | ] |
---|
192 | n/a | ) |
---|
193 | n/a | |
---|
194 | n/a | def test_uninstall_with_verbosity_1(self): |
---|
195 | n/a | with fake_pip(): |
---|
196 | n/a | ensurepip._uninstall_helper(verbosity=1) |
---|
197 | n/a | |
---|
198 | n/a | self.run_pip.assert_called_once_with( |
---|
199 | n/a | [ |
---|
200 | n/a | "uninstall", "-y", "--disable-pip-version-check", "-v", "pip", |
---|
201 | n/a | "setuptools", |
---|
202 | n/a | ] |
---|
203 | n/a | ) |
---|
204 | n/a | |
---|
205 | n/a | def test_uninstall_with_verbosity_2(self): |
---|
206 | n/a | with fake_pip(): |
---|
207 | n/a | ensurepip._uninstall_helper(verbosity=2) |
---|
208 | n/a | |
---|
209 | n/a | self.run_pip.assert_called_once_with( |
---|
210 | n/a | [ |
---|
211 | n/a | "uninstall", "-y", "--disable-pip-version-check", "-vv", "pip", |
---|
212 | n/a | "setuptools", |
---|
213 | n/a | ] |
---|
214 | n/a | ) |
---|
215 | n/a | |
---|
216 | n/a | def test_uninstall_with_verbosity_3(self): |
---|
217 | n/a | with fake_pip(): |
---|
218 | n/a | ensurepip._uninstall_helper(verbosity=3) |
---|
219 | n/a | |
---|
220 | n/a | self.run_pip.assert_called_once_with( |
---|
221 | n/a | [ |
---|
222 | n/a | "uninstall", "-y", "--disable-pip-version-check", "-vvv", |
---|
223 | n/a | "pip", "setuptools", |
---|
224 | n/a | ] |
---|
225 | n/a | ) |
---|
226 | n/a | |
---|
227 | n/a | def test_pip_environment_variables_removed(self): |
---|
228 | n/a | # ensurepip deliberately ignores all pip environment variables |
---|
229 | n/a | # See http://bugs.python.org/issue19734 for details |
---|
230 | n/a | self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder" |
---|
231 | n/a | with fake_pip(): |
---|
232 | n/a | ensurepip._uninstall_helper() |
---|
233 | n/a | self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ) |
---|
234 | n/a | |
---|
235 | n/a | def test_pip_config_file_disabled(self): |
---|
236 | n/a | # ensurepip deliberately ignores the pip config file |
---|
237 | n/a | # See http://bugs.python.org/issue20053 for details |
---|
238 | n/a | with fake_pip(): |
---|
239 | n/a | ensurepip._uninstall_helper() |
---|
240 | n/a | self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull) |
---|
241 | n/a | |
---|
242 | n/a | |
---|
243 | n/a | # Basic testing of the main functions and their argument parsing |
---|
244 | n/a | |
---|
245 | n/a | EXPECTED_VERSION_OUTPUT = "pip " + ensurepip._PIP_VERSION |
---|
246 | n/a | |
---|
247 | n/a | class TestBootstrappingMainFunction(EnsurepipMixin, unittest.TestCase): |
---|
248 | n/a | |
---|
249 | n/a | def test_bootstrap_version(self): |
---|
250 | n/a | with test.support.captured_stdout() as stdout: |
---|
251 | n/a | with self.assertRaises(SystemExit): |
---|
252 | n/a | ensurepip._main(["--version"]) |
---|
253 | n/a | result = stdout.getvalue().strip() |
---|
254 | n/a | self.assertEqual(result, EXPECTED_VERSION_OUTPUT) |
---|
255 | n/a | self.assertFalse(self.run_pip.called) |
---|
256 | n/a | |
---|
257 | n/a | def test_basic_bootstrapping(self): |
---|
258 | n/a | ensurepip._main([]) |
---|
259 | n/a | |
---|
260 | n/a | self.run_pip.assert_called_once_with( |
---|
261 | n/a | [ |
---|
262 | n/a | "install", "--no-index", "--find-links", |
---|
263 | n/a | unittest.mock.ANY, "setuptools", "pip", |
---|
264 | n/a | ], |
---|
265 | n/a | unittest.mock.ANY, |
---|
266 | n/a | ) |
---|
267 | n/a | |
---|
268 | n/a | additional_paths = self.run_pip.call_args[0][1] |
---|
269 | n/a | self.assertEqual(len(additional_paths), 2) |
---|
270 | n/a | |
---|
271 | n/a | class TestUninstallationMainFunction(EnsurepipMixin, unittest.TestCase): |
---|
272 | n/a | |
---|
273 | n/a | def test_uninstall_version(self): |
---|
274 | n/a | with test.support.captured_stdout() as stdout: |
---|
275 | n/a | with self.assertRaises(SystemExit): |
---|
276 | n/a | ensurepip._uninstall._main(["--version"]) |
---|
277 | n/a | result = stdout.getvalue().strip() |
---|
278 | n/a | self.assertEqual(result, EXPECTED_VERSION_OUTPUT) |
---|
279 | n/a | self.assertFalse(self.run_pip.called) |
---|
280 | n/a | |
---|
281 | n/a | def test_basic_uninstall(self): |
---|
282 | n/a | with fake_pip(): |
---|
283 | n/a | ensurepip._uninstall._main([]) |
---|
284 | n/a | |
---|
285 | n/a | self.run_pip.assert_called_once_with( |
---|
286 | n/a | [ |
---|
287 | n/a | "uninstall", "-y", "--disable-pip-version-check", "pip", |
---|
288 | n/a | "setuptools", |
---|
289 | n/a | ] |
---|
290 | n/a | ) |
---|
291 | n/a | |
---|
292 | n/a | |
---|
293 | n/a | |
---|
294 | n/a | if __name__ == "__main__": |
---|
295 | n/a | unittest.main() |
---|