8000 [3.9] gh-93975: Nicer error reporting in test_venv (GH-93959) by jaraco · Pull Request #94005 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.9] gh-93975: Nicer error reporting in test_venv (GH-93959) #94005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[3.9] gh-93975: Nicer error reporting in test_venv (GH-93959)
- gh-93957: Provide nicer error reporting from subprocesses in test_venv.EnsurePipTest.test_with_pip.
- Update changelog

This change does three things:

1. Extract a function for trapping output in subprocesses.
2. Emit both stdout and stderr when encountering an error.
3. Apply the change to `ensurepip._uninstall` check..
(cherry picked from commit 6066f45)

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
  • Loading branch information
jaraco committed Jun 19, 2022
commit 3f5b7aca4e74209b2e075721189d4268587ce0fc
36 changes: 26 additions & 10 deletions Lib/test/test_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Licensed to the PSF under a contributor agreement.
"""

import contextlib
import ensurepip
import os
import os.path
Expand Down Expand Up @@ -479,16 +480,10 @@ def do_test_with_pip(self, system_site_packages):

# Actually run the create command with all that unhelpful
# config in place to ensure we ignore it
try:
with self.nicer_error():
self.run_with_capture(venv.create, self.env_dir,
8000 system_site_packages=system_site_packages,
with_pip=True)
except subprocess.CalledProcessError as exc:
# The output this produces can be a little hard to read,
# but at least it has all the details
details = exc.output.decode(errors="replace")
msg = "{}\n\n**Subprocess Output**\n{}"
self.fail(msg.format(exc, details))
# Ensure pip is available in the virtual environment
envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
# Ignore DeprecationWarning since pip code is not part of Python
Expand All @@ -508,9 +503,10 @@ def do_test_with_pip(self, system_site_packages):
# Check the private uninstall command provided for the Windows
# installers works (at least in a virtual environment)
with EnvironmentVarGuard() as envvars:
out, err = check_output([envpy,
'-W', 'ignore::DeprecationWarning', '-I',
'-m', 'ensurepip._uninstall'])
with self.nicer_error():
out, err = check_output([envpy,
'-W', 'ignore::DeprecationWarning', '-I',
'-m', 'ensurepip._uninstall'])
# We force everything to text, so unittest gives the detailed diff
# if we get unexpected results
err = err.decode("latin-1") # Force to text, prevent decoding errors
Expand All @@ -536,12 +532,32 @@ def do_test_with_pip(self, system_site_packages):
if not system_site_packages:
self.assert_pip_not_installed()

@contextlib.contextmanager
def nicer_error(self):
"""
Capture output from a failed subprocess for easier debugging.

The output this handler produces can be a little hard to read,
but at least it has all the details.
"""
try:
yield
except subprocess.CalledProcessError as exc:
out = exc.output.decode(errors="replace")
err = exc.stderr.decode(errors="replace")
self.fail(
f"{exc}\n\n"
f"**Subprocess Output**\n{out}\n\n"
f"**Subprocess Error**\n{err}"
)

# Issue #26610: pip/pep425tags.py requires ctypes
@unittest.skipUnless(ctypes, 'pip requires ctypes')
@requires_zlib()
def test_with_pip(self):
self.do_test_with_pip(False)
self.do_test_with_pip(True)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Provide nicer error reporting from subprocesses in
test_venv.EnsurePipTest.test_with_pip.
0