/
test_completion.py
175 lines (154 loc) · 5.03 KB
/
test_completion.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import subprocess
import sys
from pathlib import Path
from docs_src.commands.index import tutorial001 as mod
def test_show_completion():
result = subprocess.run(
[
"bash",
"-c",
f"{sys.executable} -m coverage run {mod.__file__} --show-completion",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={**os.environ, "SHELL": "/bin/bash", "_TYPER_COMPLETE_TESTING": "True"},
)
assert "_TUTORIAL001.PY_COMPLETE=complete_bash" in result.stdout
def test_install_completion():
bash_completion_path: Path = Path.home() / ".bashrc"
text = ""
if bash_completion_path.is_file(): # pragma: nocover
text = bash_completion_path.read_text()
result = subprocess.run(
[
"bash",
"-c",
f"{sys.executable} -m coverage run {mod.__file__} --install-completion",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={**os.environ, "SHELL": "/bin/bash", "_TYPER_COMPLETE_TESTING": "True"},
)
new_text = bash_completion_path.read_text()
bash_completion_path.write_text(text)
assert "source" in new_text
assert ".bash_completions/tutorial001.py.sh" in new_text
assert "completion installed in" in result.stdout
assert "Completion will take effect once you restart the terminal" in result.stdout
def test_completion_invalid_instruction():
result = subprocess.run(
["coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={
**os.environ,
"_TUTORIAL001.PY_COMPLETE": "sourcebash",
"_TYPER_COMPLETE_TESTING": "True",
},
)
assert result.returncode != 0
assert "Invalid completion instruction." in result.stderr
def test_completion_source_bash():
result = subprocess.run(
["coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={
**os.environ,
"_TUTORIAL001.PY_COMPLETE": "source_bash",
"_TYPER_COMPLETE_TESTING": "True",
},
)
assert (
"complete -o default -F _tutorial001py_completion tutorial001.py"
in result.stdout
)
def test_completion_source_invalid_shell():
result = subprocess.run(
["coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={
**os.environ,
"_TUTORIAL001.PY_COMPLETE": "source_xxx",
"_TYPER_COMPLETE_TESTING": "True",
},
)
assert "Shell xxx not supported." in result.stderr
def test_completion_source_invalid_instruction():
result = subprocess.run(
["coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={
**os.environ,
"_TUTORIAL001.PY_COMPLETE": "explode_bash",
"_TYPER_COMPLETE_TESTING": "True",
},
)
assert 'Completion instruction "explode" not supported.' in result.stderr
def test_completion_source_zsh():
result = subprocess.run(
["coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={
**os.environ,
"_TUTORIAL001.PY_COMPLETE": "source_zsh",
"_TYPER_COMPLETE_TESTING": "True",
},
)
assert "compdef _tutorial001py_completion tutorial001.py" in result.stdout
def test_completion_source_fish():
result = subprocess.run(
["coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={
**os.environ,
"_TUTORIAL001.PY_COMPLETE": "source_fish",
"_TYPER_COMPLETE_TESTING": "True",
},
)
assert "complete --command tutorial001.py --no-files" in result.stdout
def test_completion_source_powershell():
result = subprocess.run(
["coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={
**os.environ,
"_TUTORIAL001.PY_COMPLETE": "source_powershell",
"_TYPER_COMPLETE_TESTING": "True",
},
)
assert (
"Register-ArgumentCompleter -Native -CommandName tutorial001.py -ScriptBlock $scriptblock"
in result.stdout
)
def test_completion_source_pwsh():
result = subprocess.run(
["coverage", "run", mod.__file__],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={
**os.environ,
"_TUTORIAL001.PY_COMPLETE": "source_pwsh",
"_TYPER_COMPLETE_TESTING": "True",
},
)
assert (
"Register-ArgumentCompleter -Native -CommandName tutorial001.py -ScriptBlock $scriptblock"
in result.stdout
)