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 |
Nick Coghlan | 6edd82a | 2014-02-04 23:02:36 +1000 | [diff] [blame] | 39 | real_devnull = os.devnull |
Nick Coghlan | d3e83e2 | 2013-11-13 22:24:58 +1000 | [diff] [blame] | 40 | os_patch = unittest.mock.patch("ensurepip.os") |
| 41 | patched_os = os_patch.start() |
| 42 | self.addCleanup(os_patch.stop) |
Nick Coghlan | 6edd82a | 2014-02-04 23:02:36 +1000 | [diff] [blame] | 43 | patched_os.devnull = real_devnull |
Nick Coghlan | d3e83e2 | 2013-11-13 22:24:58 +1000 | [diff] [blame] | 44 | patched_os.path = os.path |
| 45 | self.os_environ = patched_os.environ = os.environ.copy() |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 46 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 47 | |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 48 | class TestBootstrap(EnsurepipMixin, unittest.TestCase): |
| 49 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 50 | @requires_usable_pip |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 51 | def test_basic_bootstrapping(self): |
| 52 | ensurepip.bootstrap() |
| 53 | |
| 54 | self.run_pip.assert_called_once_with( |
| 55 | [ |
| 56 | "install", "--no-index", "--find-links", |
Donald Stufft | f7f58f8 | 2014-01-02 09:33:35 -0500 | [diff] [blame] | 57 | unittest.mock.ANY, "setuptools", "pip", |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 58 | ], |
| 59 | unittest.mock.ANY, |
| 60 | ) |
| 61 | |
| 62 | additional_paths = self.run_pip.call_args[0][1] |
| 63 | self.assertEqual(len(additional_paths), 2) |
| 64 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 65 | @requires_usable_pip |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 66 | def test_bootstrapping_with_root(self): |
| 67 | ensurepip.bootstrap(root="/foo/bar/") |
| 68 | |
| 69 | self.run_pip.assert_called_once_with( |
| 70 | [ |
| 71 | "install", "--no-index", "--find-links", |
Donald Stufft | f7f58f8 | 2014-01-02 09:33:35 -0500 | [diff] [blame] | 72 | unittest.mock.ANY, "--root", "/foo/bar/", |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 73 | "setuptools", "pip", |
| 74 | ], |
| 75 | unittest.mock.ANY, |
| 76 | ) |
| 77 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 78 | @requires_usable_pip |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 79 | def test_bootstrapping_with_user(self): |
| 80 | ensurepip.bootstrap(user=True) |
| 81 | |
| 82 | self.run_pip.assert_called_once_with( |
| 83 | [ |
| 84 | "install", "--no-index", "--find-links", |
Donald Stufft | f7f58f8 | 2014-01-02 09:33:35 -0500 | [diff] [blame] | 85 | unittest.mock.ANY, "--user", "setuptools", "pip", |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 86 | ], |
| 87 | unittest.mock.ANY, |
| 88 | ) |
| 89 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 90 | @requires_usable_pip |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 91 | def test_bootstrapping_with_upgrade(self): |
| 92 | ensurepip.bootstrap(upgrade=True) |
| 93 | |
| 94 | self.run_pip.assert_called_once_with( |
| 95 | [ |
| 96 | "install", "--no-index", "--find-links", |
Donald Stufft | f7f58f8 | 2014-01-02 09:33:35 -0500 | [diff] [blame] | 97 | unittest.mock.ANY, "--upgrade", "setuptools", "pip", |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 98 | ], |
| 99 | unittest.mock.ANY, |
| 100 | ) |
| 101 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 102 | @requires_usable_pip |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 103 | def test_bootstrapping_with_verbosity_1(self): |
| 104 | ensurepip.bootstrap(verbosity=1) |
| 105 | |
| 106 | self.run_pip.assert_called_once_with( |
| 107 | [ |
| 108 | "install", "--no-index", "--find-links", |
Donald Stufft | f7f58f8 | 2014-01-02 09:33:35 -0500 | [diff] [blame] | 109 | unittest.mock.ANY, "-v", "setuptools", "pip", |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 110 | ], |
| 111 | unittest.mock.ANY, |
| 112 | ) |
| 113 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 114 | @requires_usable_pip |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 115 | def test_bootstrapping_with_verbosity_2(self): |
| 116 | ensurepip.bootstrap(verbosity=2) |
| 117 | |
| 118 | self.run_pip.assert_called_once_with( |
| 119 | [ |
| 120 | "install", "--no-index", "--find-links", |
Donald Stufft | f7f58f8 | 2014-01-02 09:33:35 -0500 | [diff] [blame] | 121 | unittest.mock.ANY, "-vv", "setuptools", "pip", |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 122 | ], |
| 123 | unittest.mock.ANY, |
| 124 | ) |
| 125 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 126 | @requires_usable_pip |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 127 | def test_bootstrapping_with_verbosity_3(self): |
| 128 | ensurepip.bootstrap(verbosity=3) |
| 129 | |
| 130 | self.run_pip.assert_called_once_with( |
| 131 | [ |
| 132 | "install", "--no-index", "--find-links", |
Donald Stufft | f7f58f8 | 2014-01-02 09:33:35 -0500 | [diff] [blame] | 133 | unittest.mock.ANY, "-vvv", "setuptools", "pip", |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 134 | ], |
| 135 | unittest.mock.ANY, |
| 136 | ) |
| 137 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 138 | @requires_usable_pip |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 139 | def test_bootstrapping_with_regular_install(self): |
| 140 | ensurepip.bootstrap() |
| 141 | self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "install") |
| 142 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 143 | @requires_usable_pip |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 144 | def test_bootstrapping_with_alt_install(self): |
| 145 | ensurepip.bootstrap(altinstall=True) |
| 146 | self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "altinstall") |
| 147 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 148 | @requires_usable_pip |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 149 | def test_bootstrapping_with_default_pip(self): |
| 150 | ensurepip.bootstrap(default_pip=True) |
| 151 | self.assertNotIn("ENSUREPIP_OPTIONS", self.os_environ) |
| 152 | |
| 153 | def test_altinstall_default_pip_conflict(self): |
| 154 | with self.assertRaises(ValueError): |
| 155 | ensurepip.bootstrap(altinstall=True, default_pip=True) |
Benjamin Peterson | c844b00 | 2014-04-16 16:06:39 -0400 | [diff] [blame] | 156 | self.assertFalse(self.run_pip.called) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 157 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 158 | @requires_usable_pip |
Nick Coghlan | 6256fcb | 2013-12-23 16:16:07 +1000 | [diff] [blame] | 159 | def test_pip_environment_variables_removed(self): |
| 160 | # ensurepip deliberately ignores all pip environment variables |
| 161 | # See http://bugs.python.org/issue19734 for details |
| 162 | self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder" |
| 163 | ensurepip.bootstrap() |
| 164 | self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ) |
| 165 | |
Nick Coghlan | 6edd82a | 2014-02-04 23:02:36 +1000 | [diff] [blame] | 166 | @requires_usable_pip |
| 167 | def test_pip_config_file_disabled(self): |
| 168 | # ensurepip deliberately ignores the pip config file |
| 169 | # See http://bugs.python.org/issue20053 for details |
| 170 | ensurepip.bootstrap() |
| 171 | self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull) |
Nick Coghlan | 6256fcb | 2013-12-23 16:16:07 +1000 | [diff] [blame] | 172 | |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 173 | @contextlib.contextmanager |
| 174 | def fake_pip(version=ensurepip._PIP_VERSION): |
| 175 | if version is None: |
| 176 | pip = None |
| 177 | else: |
| 178 | class FakePip(): |
| 179 | __version__ = version |
| 180 | pip = FakePip() |
| 181 | sentinel = object() |
| 182 | orig_pip = sys.modules.get("pip", sentinel) |
| 183 | sys.modules["pip"] = pip |
| 184 | try: |
| 185 | yield pip |
| 186 | finally: |
| 187 | if orig_pip is sentinel: |
| 188 | del sys.modules["pip"] |
| 189 | else: |
| 190 | sys.modules["pip"] = orig_pip |
| 191 | |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 192 | class TestUninstall(EnsurepipMixin, unittest.TestCase): |
Nick Coghlan | ed9af52 | 2013-12-23 17:39:12 +1000 | [diff] [blame] | 193 | |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 194 | def test_uninstall_skipped_when_not_installed(self): |
| 195 | with fake_pip(None): |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 196 | ensurepip._uninstall_helper() |
Benjamin Peterson | c844b00 | 2014-04-16 16:06:39 -0400 | [diff] [blame] | 197 | self.assertFalse(self.run_pip.called) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 198 | |
Nick Coghlan | a46cf12 | 2014-02-28 23:35:05 +1000 | [diff] [blame] | 199 | def test_uninstall_skipped_with_warning_for_wrong_version(self): |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 200 | with fake_pip("not a valid version"): |
Nick Coghlan | a46cf12 | 2014-02-28 23:35:05 +1000 | [diff] [blame] | 201 | with test.support.captured_stderr() as stderr: |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 202 | ensurepip._uninstall_helper() |
Nick Coghlan | a46cf12 | 2014-02-28 23:35:05 +1000 | [diff] [blame] | 203 | warning = stderr.getvalue().strip() |
| 204 | self.assertIn("only uninstall a matching version", warning) |
Benjamin Peterson | c844b00 | 2014-04-16 16:06:39 -0400 | [diff] [blame] | 205 | self.assertFalse(self.run_pip.called) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 206 | |
| 207 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 208 | @requires_usable_pip |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 209 | def test_uninstall(self): |
| 210 | with fake_pip(): |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 211 | ensurepip._uninstall_helper() |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 212 | |
| 213 | self.run_pip.assert_called_once_with( |
Donald Stufft | 71a8589 | 2015-06-02 10:37:08 -0400 | [diff] [blame] | 214 | [ |
| 215 | "uninstall", "-y", "--disable-pip-version-check", "pip", |
| 216 | "setuptools", |
| 217 | ] |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 218 | ) |
| 219 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 220 | @requires_usable_pip |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 221 | def test_uninstall_with_verbosity_1(self): |
| 222 | with fake_pip(): |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 223 | ensurepip._uninstall_helper(verbosity=1) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 224 | |
| 225 | self.run_pip.assert_called_once_with( |
Donald Stufft | 71a8589 | 2015-06-02 10:37:08 -0400 | [diff] [blame] | 226 | [ |
| 227 | "uninstall", "-y", "--disable-pip-version-check", "-v", "pip", |
| 228 | "setuptools", |
| 229 | ] |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 230 | ) |
| 231 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 232 | @requires_usable_pip |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 233 | def test_uninstall_with_verbosity_2(self): |
| 234 | with fake_pip(): |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 235 | ensurepip._uninstall_helper(verbosity=2) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 236 | |
| 237 | self.run_pip.assert_called_once_with( |
Donald Stufft | 71a8589 | 2015-06-02 10:37:08 -0400 | [diff] [blame] | 238 | [ |
| 239 | "uninstall", "-y", "--disable-pip-version-check", "-vv", "pip", |
| 240 | "setuptools", |
| 241 | ] |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 242 | ) |
| 243 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 244 | @requires_usable_pip |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 245 | def test_uninstall_with_verbosity_3(self): |
| 246 | with fake_pip(): |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 247 | ensurepip._uninstall_helper(verbosity=3) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 248 | |
| 249 | self.run_pip.assert_called_once_with( |
Donald Stufft | 71a8589 | 2015-06-02 10:37:08 -0400 | [diff] [blame] | 250 | [ |
| 251 | "uninstall", "-y", "--disable-pip-version-check", "-vvv", |
| 252 | "pip", "setuptools", |
| 253 | ] |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 254 | ) |
| 255 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 256 | @requires_usable_pip |
Nick Coghlan | ed9af52 | 2013-12-23 17:39:12 +1000 | [diff] [blame] | 257 | def test_pip_environment_variables_removed(self): |
| 258 | # ensurepip deliberately ignores all pip environment variables |
| 259 | # See http://bugs.python.org/issue19734 for details |
| 260 | self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder" |
| 261 | with fake_pip(): |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 262 | ensurepip._uninstall_helper() |
Nick Coghlan | ed9af52 | 2013-12-23 17:39:12 +1000 | [diff] [blame] | 263 | self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 264 | |
Nick Coghlan | 6edd82a | 2014-02-04 23:02:36 +1000 | [diff] [blame] | 265 | @requires_usable_pip |
| 266 | def test_pip_config_file_disabled(self): |
| 267 | # ensurepip deliberately ignores the pip config file |
| 268 | # See http://bugs.python.org/issue20053 for details |
| 269 | with fake_pip(): |
| 270 | ensurepip._uninstall_helper() |
| 271 | self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull) |
| 272 | |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 273 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 274 | class TestMissingSSL(EnsurepipMixin, unittest.TestCase): |
| 275 | |
| 276 | def setUp(self): |
| 277 | sys.modules["ensurepip"] = ensurepip_no_ssl |
| 278 | @self.addCleanup |
| 279 | def restore_module(): |
| 280 | sys.modules["ensurepip"] = ensurepip |
| 281 | super().setUp() |
| 282 | |
| 283 | def test_bootstrap_requires_ssl(self): |
| 284 | self.os_environ["PIP_THIS_SHOULD_STAY"] = "test fodder" |
| 285 | with self.assertRaisesRegex(RuntimeError, "requires SSL/TLS"): |
| 286 | ensurepip_no_ssl.bootstrap() |
Benjamin Peterson | c844b00 | 2014-04-16 16:06:39 -0400 | [diff] [blame] | 287 | self.assertFalse(self.run_pip.called) |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 288 | self.assertIn("PIP_THIS_SHOULD_STAY", self.os_environ) |
| 289 | |
| 290 | def test_uninstall_requires_ssl(self): |
| 291 | self.os_environ["PIP_THIS_SHOULD_STAY"] = "test fodder" |
| 292 | with self.assertRaisesRegex(RuntimeError, "requires SSL/TLS"): |
| 293 | with fake_pip(): |
| 294 | ensurepip_no_ssl._uninstall_helper() |
Benjamin Peterson | c844b00 | 2014-04-16 16:06:39 -0400 | [diff] [blame] | 295 | self.assertFalse(self.run_pip.called) |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 296 | self.assertIn("PIP_THIS_SHOULD_STAY", self.os_environ) |
| 297 | |
Nick Coghlan | c00fa63 | 2014-02-15 09:14:54 +1000 | [diff] [blame] | 298 | def test_main_exits_early_with_warning(self): |
| 299 | with test.support.captured_stderr() as stderr: |
| 300 | ensurepip_no_ssl._main(["--version"]) |
| 301 | warning = stderr.getvalue().strip() |
| 302 | self.assertTrue(warning.endswith("requires SSL/TLS"), warning) |
Benjamin Peterson | c844b00 | 2014-04-16 16:06:39 -0400 | [diff] [blame] | 303 | self.assertFalse(self.run_pip.called) |
Nick Coghlan | c00fa63 | 2014-02-15 09:14:54 +1000 | [diff] [blame] | 304 | |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 305 | # Basic testing of the main functions and their argument parsing |
| 306 | |
| 307 | EXPECTED_VERSION_OUTPUT = "pip " + ensurepip._PIP_VERSION |
| 308 | |
| 309 | class TestBootstrappingMainFunction(EnsurepipMixin, unittest.TestCase): |
| 310 | |
Nick Coghlan | c00fa63 | 2014-02-15 09:14:54 +1000 | [diff] [blame] | 311 | @requires_usable_pip |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 312 | def test_bootstrap_version(self): |
| 313 | with test.support.captured_stdout() as stdout: |
| 314 | with self.assertRaises(SystemExit): |
| 315 | ensurepip._main(["--version"]) |
| 316 | result = stdout.getvalue().strip() |
| 317 | self.assertEqual(result, EXPECTED_VERSION_OUTPUT) |
Benjamin Peterson | c844b00 | 2014-04-16 16:06:39 -0400 | [diff] [blame] | 318 | self.assertFalse(self.run_pip.called) |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 319 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 320 | @requires_usable_pip |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 321 | def test_basic_bootstrapping(self): |
| 322 | ensurepip._main([]) |
| 323 | |
| 324 | self.run_pip.assert_called_once_with( |
| 325 | [ |
| 326 | "install", "--no-index", "--find-links", |
Donald Stufft | f7f58f8 | 2014-01-02 09:33:35 -0500 | [diff] [blame] | 327 | unittest.mock.ANY, "setuptools", "pip", |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 328 | ], |
| 329 | unittest.mock.ANY, |
| 330 | ) |
| 331 | |
| 332 | additional_paths = self.run_pip.call_args[0][1] |
| 333 | self.assertEqual(len(additional_paths), 2) |
| 334 | |
| 335 | class TestUninstallationMainFunction(EnsurepipMixin, unittest.TestCase): |
| 336 | |
| 337 | def test_uninstall_version(self): |
| 338 | with test.support.captured_stdout() as stdout: |
| 339 | with self.assertRaises(SystemExit): |
| 340 | ensurepip._uninstall._main(["--version"]) |
| 341 | result = stdout.getvalue().strip() |
| 342 | self.assertEqual(result, EXPECTED_VERSION_OUTPUT) |
Benjamin Peterson | c844b00 | 2014-04-16 16:06:39 -0400 | [diff] [blame] | 343 | self.assertFalse(self.run_pip.called) |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 344 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 345 | @requires_usable_pip |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 346 | def test_basic_uninstall(self): |
| 347 | with fake_pip(): |
| 348 | ensurepip._uninstall._main([]) |
| 349 | |
| 350 | self.run_pip.assert_called_once_with( |
Donald Stufft | 71a8589 | 2015-06-02 10:37:08 -0400 | [diff] [blame] | 351 | [ |
| 352 | "uninstall", "-y", "--disable-pip-version-check", "pip", |
| 353 | "setuptools", |
| 354 | ] |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 355 | ) |
| 356 | |
| 357 | |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 358 | |
| 359 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 360 | unittest.main() |