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