test_ensurepip.py 11 KB

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