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