Skip to content

Commit b3e2b6f

Browse files
wiktorinoxgaborbernatpre-commit-ci[bot]
authored
No longer forcibly echo off during windows batch activation (#2801)
Co-authored-by: Bernát Gábor <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent fd6c16b commit b3e2b6f

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

docs/changelog/2801.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
no longer forcibly echo off during windows batch activation

src/virtualenv/activation/batch/activate.bat

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
@REM This file is UTF-8 encoded, so we need to update the current code page while executing it
2-
@echo off
3-
@for /f "tokens=2 delims=:." %%a in ('"%SystemRoot%\System32\chcp.com"') do (
4-
@set _OLD_CODEPAGE=%%a
5-
)
2+
@for /f "tokens=2 delims=:." %%a in ('"%SystemRoot%\System32\chcp.com"') do @set _OLD_CODEPAGE=%%a
3+
64
@if defined _OLD_CODEPAGE (
75
"%SystemRoot%\System32\chcp.com" 65001 > nul
86
)

tests/unit/activation/test_batch.py

+47
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,50 @@ def print_prompt(self):
3333
return 'echo "%PROMPT%"'
3434

3535
activation_tester(Batch)
36+
37+
38+
@pytest.mark.usefixtures("activation_python")
39+
def test_batch_output(activation_tester_class, activation_tester, tmp_path):
40+
version_script = tmp_path / "version.bat"
41+
version_script.write_text("ver", encoding="utf-8")
42+
43+
class Batch(activation_tester_class):
44+
def __init__(self, session) -> None:
45+
super().__init__(BatchActivator, session, None, "activate.bat", "bat")
46+
self._version_cmd = [str(version_script)]
47+
self._invoke_script = []
48+
self.deactivate = "call deactivate"
49+
self.activate_cmd = "call"
50+
self.pydoc_call = f"call {self.pydoc_call}"
51+
self.unix_line_ending = False
52+
53+
def _get_test_lines(self, activate_script):
54+
"""
55+
Build intermediary script which will be then called.
56+
In the script just activate environment, call echo to get current
57+
echo setting, and then deactivate. This ensures that echo setting
58+
is preserved and no unwanted output appears.
59+
"""
60+
intermediary_script_path = str(tmp_path / "intermediary.bat")
61+
activate_script_quoted = self.quote(str(activate_script))
62+
return [
63+
"@echo on",
64+
f"@echo @call {activate_script_quoted} > {intermediary_script_path}",
65+
f"@echo @echo >> {intermediary_script_path}",
66+
f"@echo @deactivate >> {intermediary_script_path}",
67+
f"@call {intermediary_script_path}",
68+
]
69+
70+
def assert_output(self, out, raw, tmp_path): # noqa: ARG002
71+
assert out[0] == "ECHO is on.", raw
72+
73+
def quote(self, s):
74+
if '"' in s or " " in s:
75+
text = s.replace('"', r"\"")
76+
return f'"{text}"'
77+
return s
78+
79+
def print_prompt(self):
80+
return 'echo "%PROMPT%"'
81+
82+
activation_tester(Batch)

0 commit comments

Comments
 (0)