bpo-34011: Fixes missing venv files and other tests (GH-9458)

This commit is contained in:
Steve Dower 2018-09-20 13:38:34 -07:00 committed by GitHub
parent bc85475058
commit f14c28f397
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 146 additions and 112 deletions

View file

@ -6,7 +6,8 @@ executing have not been removed.
"""
import unittest
import test.support
from test.support import captured_stderr, TESTFN, EnvironmentVarGuard
from test.support import (captured_stderr, TESTFN, EnvironmentVarGuard,
change_cwd)
import builtins
import os
import sys
@ -346,40 +347,47 @@ class ImportSideEffectTests(unittest.TestCase):
# __file__ if abs_paths() does not get run. sys and builtins (the
# only other modules imported before site.py runs) do not have
# __file__ or __cached__ because they are built-in.
parent = os.path.relpath(os.path.dirname(os.__file__))
env = os.environ.copy()
env['PYTHONPATH'] = parent
code = ('import os, sys',
# use ASCII to avoid locale issues with non-ASCII directories
'os_file = os.__file__.encode("ascii", "backslashreplace")',
r'sys.stdout.buffer.write(os_file + b"\n")',
'os_cached = os.__cached__.encode("ascii", "backslashreplace")',
r'sys.stdout.buffer.write(os_cached + b"\n")')
command = '\n'.join(code)
# First, prove that with -S (no 'import site'), the paths are
# relative.
proc = subprocess.Popen([sys.executable, '-S', '-c', command],
env=env,
stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
try:
parent = os.path.relpath(os.path.dirname(os.__file__))
cwd = os.getcwd()
except ValueError:
# Failure to get relpath probably means we need to chdir
# to the same drive.
cwd, parent = os.path.split(os.path.dirname(os.__file__))
with change_cwd(cwd):
env = os.environ.copy()
env['PYTHONPATH'] = parent
code = ('import os, sys',
# use ASCII to avoid locale issues with non-ASCII directories
'os_file = os.__file__.encode("ascii", "backslashreplace")',
r'sys.stdout.buffer.write(os_file + b"\n")',
'os_cached = os.__cached__.encode("ascii", "backslashreplace")',
r'sys.stdout.buffer.write(os_cached + b"\n")')
command = '\n'.join(code)
# First, prove that with -S (no 'import site'), the paths are
# relative.
proc = subprocess.Popen([sys.executable, '-S', '-c', command],
env=env,
stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
self.assertEqual(proc.returncode, 0)
os__file__, os__cached__ = stdout.splitlines()[:2]
self.assertFalse(os.path.isabs(os__file__))
self.assertFalse(os.path.isabs(os__cached__))
# Now, with 'import site', it works.
proc = subprocess.Popen([sys.executable, '-c', command],
env=env,
stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
self.assertEqual(proc.returncode, 0)
os__file__, os__cached__ = stdout.splitlines()[:2]
self.assertTrue(os.path.isabs(os__file__),
"expected absolute path, got {}"
.format(os__file__.decode('ascii')))
self.assertTrue(os.path.isabs(os__cached__),
"expected absolute path, got {}"
.format(os__cached__.decode('ascii')))
self.assertEqual(proc.returncode, 0)
os__file__, os__cached__ = stdout.splitlines()[:2]
self.assertFalse(os.path.isabs(os__file__))
self.assertFalse(os.path.isabs(os__cached__))
# Now, with 'import site', it works.
proc = subprocess.Popen([sys.executable, '-c', command],
env=env,
stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
self.assertEqual(proc.returncode, 0)
os__file__, os__cached__ = stdout.splitlines()[:2]
self.assertTrue(os.path.isabs(os__file__),
"expected absolute path, got {}"
.format(os__file__.decode('ascii')))
self.assertTrue(os.path.isabs(os__cached__),
"expected absolute path, got {}"
.format(os__cached__.decode('ascii')))
def test_no_duplicate_paths(self):
# No duplicate paths should exist in sys.path