Skip to content

Commit 4866da0

Browse files
authored
[3.13] gh-109940: Respect VIRTUAL_ENV_DISABLE_PROMPT in activate.bat (GH-151215) (GH-151263)
1 parent 3a04c62 commit 4866da0

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
@@ -594,6 +594,51 @@ def test_unicode_in_batch_file(self):
594594
)
595595
self.assertEqual(out.strip(), '0')
596596

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