Skip to content

Commit b0a90a9

Browse files
authored
[3.14] gh-109940: Respect VIRTUAL_ENV_DISABLE_PROMPT in activate.bat (GH-151215) (GH-151264)
(cherry picked from commit 7a014f4)
1 parent 8c92248 commit b0a90a9

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

Lib/test/test_venv.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,51 @@ def test_unicode_in_batch_file(self):
598598
)
599599
self.assertEqual(out.strip(), '0')
600600

601+
@unittest.skipUnless(os.name == 'nt', 'only relevant on Windows')
602+
def test_activate_bat_respects_disable_prompt(self):
603+
rmtree(self.env_dir)
604+
env_dir = os.path.join(os.path.realpath(self.env_dir), 'venv')
605+
builder = venv.EnvBuilder(clear=True)
606+
builder.create(env_dir)
607+
activate = os.path.join(env_dir, self.bindir, 'activate.bat')
608+
test_batch = os.path.join(self.env_dir, 'test_disable_prompt.bat')
609+
with open(test_batch, "w") as f:
610+
f.write('@echo off\n'
611+
'set "PROMPT=base$G"\n'
612+
'set "VIRTUAL_ENV_DISABLE_PROMPT=1"\n'
613+
f'call "{activate}"\n'
614+
'echo ACTIVE_PROMPT:%PROMPT%\n'
615+
'echo VIRTUAL_ENV:%VIRTUAL_ENV%\n'
616+
'set "PROMPT=changed$G"\n'
617+
'call deactivate\n'
618+
'echo FINAL_PROMPT:%PROMPT%\n')
619+
out, err = check_output([test_batch])
620+
lines = out.splitlines()
621+
self.assertEqual(lines[0], b'ACTIVE_PROMPT:base$G')
622+
self.assertEndsWith(lines[1], os.fsencode(env_dir))
623+
self.assertEqual(lines[2], b'FINAL_PROMPT:changed$G')
624+
625+
@unittest.skipUnless(os.name == 'nt', 'only relevant on Windows')
626+
def test_activate_bat_prefixes_prompt_by_default(self):
627+
rmtree(self.env_dir)
628+
env_dir = os.path.join(os.path.realpath(self.env_dir), 'venv')
629+
builder = venv.EnvBuilder(clear=True)
630+
builder.create(env_dir)
631+
activate = os.path.join(env_dir, self.bindir, 'activate.bat')
632+
test_batch = os.path.join(self.env_dir, 'test_enable_prompt.bat')
633+
with open(test_batch, "w") as f:
634+
f.write('@echo off\n'
635+
'set "PROMPT=base) $G"\n'
636+
'set "VIRTUAL_ENV_DISABLE_PROMPT="\n'
637+
f'call "{activate}"\n'
638+
'echo ACTIVE_PROMPT:%PROMPT%\n'
639+
'call deactivate\n'
640+
'echo FINAL_PROMPT:%PROMPT%\n')
641+
out, err = check_output([test_batch])
642+
lines = out.splitlines()
643+
self.assertEqual(lines[0], b'ACTIVE_PROMPT:(venv) base) $G')
644+
self.assertEqual(lines[1], b'FINAL_PROMPT:base) $G')
645+
601646
@unittest.skipUnless(os.name == 'nt' and can_symlink(),
602647
'symlinks on Windows')
603648
def test_failed_symlink(self):

Lib/venv/scripts/nt/activate.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ if not defined PROMPT set PROMPT=$P$G
1515
if defined _OLD_VIRTUAL_PROMPT set PROMPT=%_OLD_VIRTUAL_PROMPT%
1616
if defined _OLD_VIRTUAL_PYTHONHOME set PYTHONHOME=%_OLD_VIRTUAL_PYTHONHOME%
1717

18-
set "_OLD_VIRTUAL_PROMPT=%PROMPT%"
19-
set "PROMPT=(__VENV_PROMPT__) %PROMPT%"
18+
if not defined VIRTUAL_ENV_DISABLE_PROMPT set "_OLD_VIRTUAL_PROMPT=%PROMPT%"
19+
if not defined VIRTUAL_ENV_DISABLE_PROMPT set "PROMPT=(__VENV_PROMPT__) %PROMPT%"
2020

2121
if defined PYTHONHOME set _OLD_VIRTUAL_PYTHONHOME=%PYTHONHOME%
2222
set PYTHONHOME=
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix Windows :mod:`venv` activation in ``cmd.exe`` to respect
2+
``VIRTUAL_ENV_DISABLE_PROMPT``.

0 commit comments

Comments
 (0)