Skip to content

Commit 7a014f4

Browse files
authored
gh-109940: Respect VIRTUAL_ENV_DISABLE_PROMPT in activate.bat (GH-151215)
1 parent ff64d8d commit 7a014f4

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

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

16-
@set "_OLD_VIRTUAL_PROMPT=%PROMPT%"
17-
@set "PROMPT=(__VENV_PROMPT__) %PROMPT%"
16+
@if not defined VIRTUAL_ENV_DISABLE_PROMPT @set "_OLD_VIRTUAL_PROMPT=%PROMPT%"
17+
@if not defined VIRTUAL_ENV_DISABLE_PROMPT @set "PROMPT=(__VENV_PROMPT__) %PROMPT%"
1818

1919
@if defined PYTHONHOME @set _OLD_VIRTUAL_PYTHONHOME=%PYTHONHOME%
2020
@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)