Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fd12f2

Browse files
committed
Issue 19734: better diagnostics for test_venv failures
1 parent fcafe43 commit 6fd12f2

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

Lib/test/test_venv.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,27 @@ def test_with_pip(self):
285285
# warnings in current versions of Python. Ensure related
286286
# environment settings don't cause venv to fail.
287287
envvars["PYTHONWARNINGS"] = "e"
288-
self.run_with_capture(venv.create, self.env_dir, with_pip=True)
288+
try:
289+
self.run_with_capture(venv.create, self.env_dir, with_pip=True)
290+
except subprocess.CalledProcessError as exc:
291+
# The output this produces can be a little hard to read, but
292+
# least it has all the details
293+
details = exc.output.decode(errors="replace")
294+
msg = "{}\n\n**Subprocess Output**\n{}".format(exc, details)
295+
self.fail(msg)
289296
envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
290297
cmd = [envpy, '-m', 'pip', '--version']
291298
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
292299
stderr=subprocess.PIPE)
293300
out, err = p.communicate()
294-
self.assertEqual(err, b"")
295-
self.assertTrue(out.startswith(b"pip"))
296-
self.assertIn(self.env_dir.encode(), out)
301+
# We force everything to text, so unittest gives the detailed diff
302+
# if we get unexpected results
303+
err = err.decode("latin-1") # Force to text, prevent decoding errors
304+
self.assertEqual(err, "")
305+
out = out.decode("latin-1") # Force to text, prevent decoding errors
306+
env_dir = os.fsencode(self.env_dir).decode("latin-1")
307+
self.assertTrue(out.startswith("pip"))
308+
self.assertIn(env_dir, out)
297309

298310

299311
def test_main():

Lib/venv/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@ def _setup_pip(self, context):
237237
# We run ensurepip in isolated mode to avoid side effects from
238238
# environment vars, the current directory and anything else
239239
# intended for the global Python environment
240-
cmd = [context.env_exe, '-Im', 'ensurepip', '--upgrade',
240+
cmd = [context.env_exe, '-m', 'ensurepip', '--upgrade',
241241
'--default-pip']
242-
subprocess.check_output(cmd)
242+
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
243243

244244
def setup_scripts(self, context):
245245
"""

0 commit comments

Comments
 (0)