Skip to content

Commit d780f0a

Browse files
authored
gh-130486: Fix test_venv fails from within venv (GH-130487)
1 parent 813bc56 commit d780f0a

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

Lib/test/test_venv.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,32 +228,37 @@ def test_upgrade_dependencies(self):
228228
builder = venv.EnvBuilder()
229229
bin_path = 'bin'
230230
python_exe = os.path.split(sys.executable)[1]
231+
expected_exe = os.path.basename(sys._base_executable)
232+
231233
if sys.platform == 'win32':
232234
bin_path = 'Scripts'
233235
if os.path.normcase(os.path.splitext(python_exe)[0]).endswith('_d'):
234-
python_exe = 'python_d.exe'
236+
expected_exe = 'python_d'
235237
else:
236-
python_exe = 'python.exe'
238+
expected_exe = 'python'
239+
python_exe = expected_exe + '.exe'
240+
237241
with tempfile.TemporaryDirectory() as fake_env_dir:
238242
expect_exe = os.path.normcase(
239-
os.path.join(fake_env_dir, bin_path, python_exe)
243+
os.path.join(fake_env_dir, bin_path, expected_exe)
240244
)
241245
if sys.platform == 'win32':
242246
expect_exe = os.path.normcase(os.path.realpath(expect_exe))
243247

244248
def pip_cmd_checker(cmd, **kwargs):
245-
cmd[0] = os.path.normcase(cmd[0])
246249
self.assertEqual(
247-
cmd,
250+
cmd[1:],
248251
[
249-
expect_exe,
250252
'-m',
251253
'pip',
252254
'install',
253255
'--upgrade',
254256
'pip',
255257
]
256258
)
259+
exe_dir = os.path.normcase(os.path.dirname(cmd[0]))
260+
expected_dir = os.path.normcase(os.path.dirname(expect_exe))
261+
self.assertEqual(exe_dir, expected_dir)
257262

258263
fake_context = builder.ensure_directories(fake_env_dir)
259264
with patch('venv.subprocess.check_output', pip_cmd_checker):
@@ -681,7 +686,8 @@ def test_zippath_from_non_installed_posix(self):
681686
self.addCleanup(rmtree, non_installed_dir)
682687
bindir = os.path.join(non_installed_dir, self.bindir)
683688
os.mkdir(bindir)
684-
shutil.copy2(sys.executable, bindir)
689+
python_exe = os.path.basename(sys.executable)
690+
shutil.copy2(sys.executable, os.path.join(bindir, python_exe))
685691
libdir = os.path.join(non_installed_dir, platlibdir, self.lib[1])
686692
os.makedirs(libdir)
687693
landmark = os.path.join(libdir, "os.py")
@@ -717,7 +723,7 @@ def test_zippath_from_non_installed_posix(self):
717723
else:
718724
additional_pythonpath_for_non_installed.append(
719725
eachpath)
720-
cmd = [os.path.join(non_installed_dir, self.bindir, self.exe),
726+
cmd = [os.path.join(non_installed_dir, self.bindir, python_exe),
721727
"-m",
722728
"venv",
723729
"--without-pip",
@@ -748,7 +754,8 @@ def test_zippath_from_non_installed_posix(self):
748754
subprocess.check_call(cmd, env=child_env)
749755
# Now check the venv created from the non-installed python has
750756
# correct zip path in pythonpath.
751-
cmd = [self.envpy(), '-S', '-c', 'import sys; print(sys.path)']
757+
target_python = os.path.join(self.env_dir, self.bindir, python_exe)
758+
cmd = [target_python, '-S', '-c', 'import sys; print(sys.path)']
752759
out, err = check_output(cmd)
753760
self.assertTrue(zip_landmark.encode() in out)
754761

0 commit comments

Comments
 (0)