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( |
| 214 | ["uninstall", "-y", "pip", "setuptools"] |
| 215 | ) |
| 216 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 217 | @requires_usable_pip |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 218 | def test_uninstall_with_verbosity_1(self): |
| 219 | with fake_pip(): |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 220 | ensurepip._uninstall_helper(verbosity=1) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 221 | |
| 222 | self.run_pip.assert_called_once_with( |
| 223 | ["uninstall", "-y", "-v", "pip", "setuptools"] |
| 224 | ) |
| 225 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 226 | @requires_usable_pip |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 227 | def test_uninstall_with_verbosity_2(self): |
| 228 | with fake_pip(): |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 229 | ensurepip._uninstall_helper(verbosity=2) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 230 | |
| 231 | self.run_pip.assert_called_once_with( |
| 232 | ["uninstall", "-y", "-vv", "pip", "setuptools"] |
| 233 | ) |
| 234 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 235 | @requires_usable_pip |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 236 | def test_uninstall_with_verbosity_3(self): |
| 237 | with fake_pip(): |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 238 | ensurepip._uninstall_helper(verbosity=3) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 239 | |
| 240 | self.run_pip.assert_called_once_with( |
| 241 | ["uninstall", "-y", "-vvv", "pip", "setuptools"] |
| 242 | ) |
| 243 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 244 | @requires_usable_pip |
Nick Coghlan | ed9af52 | 2013-12-23 17:39:12 +1000 | [diff] [blame] | 245 | def test_pip_environment_variables_removed(self): |
| 246 | # ensurepip deliberately ignores all pip environment variables |
| 247 | # See http://bugs.python.org/issue19734 for details |
| 248 | self.os_environ["PIP_THIS_SHOULD_GO_AWAY"] = "test fodder" |
| 249 | with fake_pip(): |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 250 | ensurepip._uninstall_helper() |
Nick Coghlan | ed9af52 | 2013-12-23 17:39:12 +1000 | [diff] [blame] | 251 | self.assertNotIn("PIP_THIS_SHOULD_GO_AWAY", self.os_environ) |
Nick Coghlan | fdf3a62 | 2013-11-30 17:15:09 +1000 | [diff] [blame] | 252 | |
Nick Coghlan | 6edd82a | 2014-02-04 23:02:36 +1000 | [diff] [blame] | 253 | @requires_usable_pip |
| 254 | def test_pip_config_file_disabled(self): |
| 255 | # ensurepip deliberately ignores the pip config file |
| 256 | # See http://bugs.python.org/issue20053 for details |
| 257 | with fake_pip(): |
| 258 | ensurepip._uninstall_helper() |
| 259 | self.assertEqual(self.os_environ["PIP_CONFIG_FILE"], os.devnull) |
| 260 | |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 261 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 262 | class TestMissingSSL(EnsurepipMixin, unittest.TestCase): |
| 263 | |
| 264 | def setUp(self): |
| 265 | sys.modules["ensurepip"] = ensurepip_no_ssl |
| 266 | @self.addCleanup |
| 267 | def restore_module(): |
| 268 | sys.modules["ensurepip"] = ensurepip |
| 269 | super().setUp() |
| 270 | |
| 271 | def test_bootstrap_requires_ssl(self): |
| 272 | self.os_environ["PIP_THIS_SHOULD_STAY"] = "test fodder" |
| 273 | with self.assertRaisesRegex(RuntimeError, "requires SSL/TLS"): |
| 274 | ensurepip_no_ssl.bootstrap() |
Benjamin Peterson | c844b00 | 2014-04-16 16:06:39 -0400 | [diff] [blame] | 275 | self.assertFalse(self.run_pip.called) |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 276 | self.assertIn("PIP_THIS_SHOULD_STAY", self.os_environ) |
| 277 | |
| 278 | def test_uninstall_requires_ssl(self): |
| 279 | self.os_environ["PIP_THIS_SHOULD_STAY"] = "test fodder" |
| 280 | with self.assertRaisesRegex(RuntimeError, "requires SSL/TLS"): |
| 281 | with fake_pip(): |
| 282 | ensurepip_no_ssl._uninstall_helper() |
Benjamin Peterson | c844b00 | 2014-04-16 16:06:39 -0400 | [diff] [blame] | 283 | self.assertFalse(self.run_pip.called) |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 284 | self.assertIn("PIP_THIS_SHOULD_STAY", self.os_environ) |
| 285 | |
Nick Coghlan | c00fa63 | 2014-02-15 09:14:54 +1000 | [diff] [blame] | 286 | def test_main_exits_early_with_warning(self): |
| 287 | with test.support.captured_stderr() as stderr: |
| 288 | ensurepip_no_ssl._main(["--version"]) |
| 289 | warning = stderr.getvalue().strip() |
| 290 | self.assertTrue(warning.endswith("requires SSL/TLS"), warning) |
Benjamin Peterson | c844b00 | 2014-04-16 16:06:39 -0400 | [diff] [blame] | 291 | self.assertFalse(self.run_pip.called) |
Nick Coghlan | c00fa63 | 2014-02-15 09:14:54 +1000 | [diff] [blame] | 292 | |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 293 | # Basic testing of the main functions and their argument parsing |
| 294 | |
| 295 | EXPECTED_VERSION_OUTPUT = "pip " + ensurepip._PIP_VERSION |
| 296 | |
| 297 | class TestBootstrappingMainFunction(EnsurepipMixin, unittest.TestCase): |
| 298 | |
Nick Coghlan | c00fa63 | 2014-02-15 09:14:54 +1000 | [diff] [blame] | 299 | @requires_usable_pip |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 300 | def test_bootstrap_version(self): |
| 301 | with test.support.captured_stdout() as stdout: |
| 302 | with self.assertRaises(SystemExit): |
| 303 | ensurepip._main(["--version"]) |
| 304 | result = stdout.getvalue().strip() |
| 305 | self.assertEqual(result, EXPECTED_VERSION_OUTPUT) |
Benjamin Peterson | c844b00 | 2014-04-16 16:06:39 -0400 | [diff] [blame] | 306 | self.assertFalse(self.run_pip.called) |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 307 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 308 | @requires_usable_pip |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 309 | def test_basic_bootstrapping(self): |
| 310 | ensurepip._main([]) |
| 311 | |
| 312 | self.run_pip.assert_called_once_with( |
| 313 | [ |
| 314 | "install", "--no-index", "--find-links", |
Donald Stufft | f7f58f8 | 2014-01-02 09:33:35 -0500 | [diff] [blame] | 315 | unittest.mock.ANY, "setuptools", "pip", |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 316 | ], |
| 317 | unittest.mock.ANY, |
| 318 | ) |
| 319 | |
| 320 | additional_paths = self.run_pip.call_args[0][1] |
| 321 | self.assertEqual(len(additional_paths), 2) |
| 322 | |
| 323 | class TestUninstallationMainFunction(EnsurepipMixin, unittest.TestCase): |
| 324 | |
| 325 | def test_uninstall_version(self): |
| 326 | with test.support.captured_stdout() as stdout: |
| 327 | with self.assertRaises(SystemExit): |
| 328 | ensurepip._uninstall._main(["--version"]) |
| 329 | result = stdout.getvalue().strip() |
| 330 | self.assertEqual(result, EXPECTED_VERSION_OUTPUT) |
Benjamin Peterson | c844b00 | 2014-04-16 16:06:39 -0400 | [diff] [blame] | 331 | self.assertFalse(self.run_pip.called) |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 332 | |
Nick Coghlan | ae2ee96 | 2013-12-23 23:07:07 +1000 | [diff] [blame] | 333 | @requires_usable_pip |
Nick Coghlan | f71cae0 | 2013-12-23 18:20:34 +1000 | [diff] [blame] | 334 | def test_basic_uninstall(self): |
| 335 | with fake_pip(): |
| 336 | ensurepip._uninstall._main([]) |
| 337 | |
| 338 | self.run_pip.assert_called_once_with( |
| 339 | ["uninstall", "-y", "pip", "setuptools"] |
| 340 | ) |
| 341 | |
| 342 | |
Nick Coghlan | d0cf063 | 2013-11-11 22:11:55 +1000 | [diff] [blame] | 343 | |
| 344 | if __name__ == "__main__": |
| 345 | test.support.run_unittest(__name__) |