-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
test_batch.py
82 lines (65 loc) · 2.99 KB
/
test_batch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from __future__ import annotations
import pytest
from virtualenv.activation import BatchActivator
@pytest.mark.usefixtures("activation_python")
def test_batch(activation_tester_class, activation_tester, tmp_path):
version_script = tmp_path / "version.bat"
version_script.write_text("ver", encoding="utf-8")
class Batch(activation_tester_class):
def __init__(self, session) -> None:
super().__init__(BatchActivator, session, None, "activate.bat", "bat")
self._version_cmd = [str(version_script)]
self._invoke_script = []
self.deactivate = "call deactivate"
self.activate_cmd = "call"
self.pydoc_call = f"call {self.pydoc_call}"
self.unix_line_ending = False
def _get_test_lines(self, activate_script):
return ["@echo off", *super()._get_test_lines(activate_script)]
def quote(self, s):
if '"' in s or " " in s:
text = s.replace('"', r"\"")
return f'"{text}"'
return s
def print_prompt(self):
return 'echo "%PROMPT%"'
activation_tester(Batch)
@pytest.mark.usefixtures("activation_python")
def test_batch_output(activation_tester_class, activation_tester, tmp_path):
version_script = tmp_path / "version.bat"
version_script.write_text("ver", encoding="utf-8")
class Batch(activation_tester_class):
def __init__(self, session) -> None:
super().__init__(BatchActivator, session, None, "activate.bat", "bat")
self._version_cmd = [str(version_script)]
self._invoke_script = []
self.deactivate = "call deactivate"
self.activate_cmd = "call"
self.pydoc_call = f"call {self.pydoc_call}"
self.unix_line_ending = False
def _get_test_lines(self, activate_script):
"""
Build intermediary script which will be then called.
In the script just activate environment, call echo to get current
echo setting, and then deactivate. This ensures that echo setting
is preserved and no unwanted output appears.
"""
intermediary_script_path = str(tmp_path / "intermediary.bat")
activate_script_quoted = self.quote(str(activate_script))
return [
"@echo on",
f"@echo @call {activate_script_quoted} > {intermediary_script_path}",
f"@echo @echo >> {intermediary_script_path}",
f"@echo @deactivate >> {intermediary_script_path}",
f"@call {intermediary_script_path}",
]
def assert_output(self, out, raw, tmp_path): # noqa: ARG002
assert out[0] == "ECHO is on.", raw
def quote(self, s):
if '"' in s or " " in s:
text = s.replace('"', r"\"")
return f'"{text}"'
return s
def print_prompt(self):
return 'echo "%PROMPT%"'
activation_tester(Batch)