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