Skip to content

Commit 5c83037

Browse files
[3.15] gh-109940: Respect VIRTUAL_ENV_DISABLE_PROMPT in activate.bat (GH-151215) (GH-151225)
Co-authored-by: Harjoth Khara <harjoth.khara@gmail.com>
1 parent 54ee910 commit 5c83037

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

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