Skip to content

Commit 00166c3

Browse files
committed
gh-131178: Add tests for ensurepip command-line interface
The documented options of ``python -m ensurepip`` (--upgrade, --user, --root, --altinstall, --default-pip, --verbose) were only tested through bootstrap(), not through the command line. Drive each one through _main() with pip mocked, including the altinstall/default-pip conflict, --help and an unknown option.
1 parent c8da735 commit 00166c3

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lib/test/test_ensurepip.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,115 @@ def test_bootstrapping_error_code(self):
342342
exit_code = ensurepip._main([])
343343
self.assertEqual(exit_code, 2)
344344

345+
def test_bootstrapping_with_root(self):
346+
exit_code = ensurepip._main(["--root", "/foo/bar/"])
347+
348+
self.run_pip.assert_called_once_with(
349+
[
350+
"install", "--no-cache-dir", "--no-index", "--find-links",
351+
unittest.mock.ANY, "--root", "/foo/bar/", *COMPILE_OPT,
352+
"pip",
353+
],
354+
unittest.mock.ANY,
355+
)
356+
self.assertEqual(exit_code, 0)
357+
358+
def test_bootstrapping_with_user(self):
359+
ensurepip._main(["--user"])
360+
361+
self.run_pip.assert_called_once_with(
362+
[
363+
"install", "--no-cache-dir", "--no-index", "--find-links",
364+
unittest.mock.ANY, "--user", *COMPILE_OPT, "pip",
365+
],
366+
unittest.mock.ANY,
367+
)
368+
369+
def test_bootstrapping_with_upgrade(self):
370+
for option in ("--upgrade", "-U"):
371+
with self.subTest(option=option):
372+
self.run_pip.reset_mock()
373+
ensurepip._main([option])
374+
375+
self.run_pip.assert_called_once_with(
376+
[
377+
"install", "--no-cache-dir", "--no-index",
378+
"--find-links", unittest.mock.ANY, "--upgrade",
379+
*COMPILE_OPT, "pip",
380+
],
381+
unittest.mock.ANY,
382+
)
383+
384+
def test_bootstrapping_with_verbosity(self):
385+
for argv, expected in (
386+
(["-v"], "-v"),
387+
(["--verbose"], "-v"),
388+
(["-vv"], "-vv"),
389+
(["-v", "-v", "-v"], "-vvv"),
390+
):
391+
with self.subTest(argv=argv):
392+
self.run_pip.reset_mock()
393+
ensurepip._main(argv)
394+
395+
self.run_pip.assert_called_once_with(
396+
[
397+
"install", "--no-cache-dir", "--no-index",
398+
"--find-links", unittest.mock.ANY, expected,
399+
*COMPILE_OPT, "pip",
400+
],
401+
unittest.mock.ANY,
402+
)
403+
404+
def test_bootstrapping_with_altinstall(self):
405+
ensurepip._main(["--altinstall"])
406+
self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "altinstall")
407+
408+
def test_bootstrapping_with_default_pip(self):
409+
ensurepip._main(["--default-pip"])
410+
self.assertNotIn("ENSUREPIP_OPTIONS", self.os_environ)
411+
412+
def test_altinstall_default_pip_conflict(self):
413+
with self.assertRaises(ValueError):
414+
ensurepip._main(["--altinstall", "--default-pip"])
415+
self.assertFalse(self.run_pip.called)
416+
417+
def test_bootstrapping_with_all_options(self):
418+
# The order of the options on the command line doesn't matter.
419+
exit_code = ensurepip._main([
420+
"-v", "--user", "--upgrade", "--root", "/foo/bar/",
421+
"--altinstall", "-v",
422+
])
423+
424+
self.run_pip.assert_called_once_with(
425+
[
426+
"install", "--no-cache-dir", "--no-index", "--find-links",
427+
unittest.mock.ANY, "--root", "/foo/bar/", "--upgrade",
428+
"--user", "-vv", *COMPILE_OPT, "pip",
429+
],
430+
unittest.mock.ANY,
431+
)
432+
self.assertEqual(self.os_environ["ENSUREPIP_OPTIONS"], "altinstall")
433+
self.assertEqual(exit_code, 0)
434+
435+
def test_help(self):
436+
with test.support.captured_stdout() as stdout:
437+
with self.assertRaises(SystemExit) as cm:
438+
ensurepip._main(["--help"])
439+
self.assertEqual(cm.exception.code, 0)
440+
help_text = stdout.getvalue()
441+
for option in ("--version", "--verbose", "--upgrade", "--user",
442+
"--root", "--altinstall", "--default-pip"):
443+
self.assertIn(option, help_text)
444+
self.assertFalse(self.run_pip.called)
445+
446+
def test_unknown_option(self):
447+
with test.support.captured_stderr() as stderr:
448+
with self.assertRaises(SystemExit) as cm:
449+
ensurepip._main(["--unknown"])
450+
self.assertEqual(cm.exception.code, 2)
451+
self.assertIn("unrecognized arguments: --unknown", stderr.getvalue())
452+
self.assertFalse(self.run_pip.called)
453+
345454

346455
class TestUninstallationMainFunction(EnsurepipMixin, unittest.TestCase):
347456

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add tests for the :mod:`ensurepip` command-line interface.

0 commit comments

Comments
 (0)