/
test_completion_install.py
157 lines (141 loc) · 5.43 KB
/
test_completion_install.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
import os
import subprocess
from pathlib import Path
from unittest import mock
import shellingham
import typer
from typer.testing import CliRunner
from docs_src.commands.index import tutorial001 as mod
runner = CliRunner()
app = typer.Typer()
app.command()(mod.main)
def test_completion_install_no_shell():
result = subprocess.run(
["coverage", "run", mod.__file__, "--install-completion"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={
**os.environ,
"_TYPER_COMPLETE_TESTING": "True",
"_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
},
)
# TODO: when deprecating Click 7, remove second option
assert (
"Option '--install-completion' requires an argument" in result.stderr
or "--install-completion option requires an argument" in result.stderr
)
def test_completion_install_bash():
bash_completion_path: Path = Path.home() / ".bashrc"
text = ""
if bash_completion_path.is_file():
text = bash_completion_path.read_text()
result = subprocess.run(
["coverage", "run", mod.__file__, "--install-completion", "bash"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={
**os.environ,
"_TYPER_COMPLETE_TESTING": "True",
"_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
},
)
new_text = bash_completion_path.read_text()
bash_completion_path.write_text(text)
install_source = ".bash_completions/tutorial001.py.sh"
assert install_source not in text
assert install_source in new_text
assert "completion installed in" in result.stdout
assert "Completion will take effect once you restart the terminal" in result.stdout
install_source_path = Path.home() / install_source
assert install_source_path.is_file()
install_content = install_source_path.read_text()
install_source_path.unlink()
assert (
"complete -o default -F _tutorial001py_completion tutorial001.py"
in install_content
)
def test_completion_install_zsh():
completion_path: Path = Path.home() / ".zshrc"
text = ""
if not completion_path.is_file(): # pragma: nocover
completion_path.write_text('echo "custom .zshrc"')
if completion_path.is_file():
text = completion_path.read_text()
result = subprocess.run(
["coverage", "run", mod.__file__, "--install-completion", "zsh"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={
**os.environ,
"_TYPER_COMPLETE_TESTING": "True",
"_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
},
)
new_text = completion_path.read_text()
completion_path.write_text(text)
zfunc_fragment = "fpath+=~/.zfunc"
assert zfunc_fragment in new_text
assert "completion installed in" in result.stdout
assert "Completion will take effect once you restart the terminal" in result.stdout
install_source_path = Path.home() / ".zfunc/_tutorial001.py"
assert install_source_path.is_file()
install_content = install_source_path.read_text()
install_source_path.unlink()
assert "compdef _tutorial001py_completion tutorial001.py" in install_content
def test_completion_install_fish():
script_path = Path(mod.__file__)
completion_path: Path = (
Path.home() / f".config/fish/completions/{script_path.name}.fish"
)
result = subprocess.run(
["coverage", "run", mod.__file__, "--install-completion", "fish"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
env={
**os.environ,
"_TYPER_COMPLETE_TESTING": "True",
"_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
},
)
new_text = completion_path.read_text()
completion_path.unlink()
assert "complete --command tutorial001.py" in new_text
assert "completion installed in" in result.stdout
assert "Completion will take effect once you restart the terminal" in result.stdout
runner = CliRunner()
app = typer.Typer()
app.command()(mod.main)
def test_completion_install_powershell():
completion_path: Path = (
Path.home() / f".config/powershell/Microsoft.PowerShell_profile.ps1"
)
completion_path_bytes = f"{completion_path}\n".encode("windows-1252")
text = ""
if completion_path.is_file(): # pragma: nocover
text = completion_path.read_text()
with mock.patch.object(
shellingham, "detect_shell", return_value=("pwsh", "/usr/bin/pwsh")
):
with mock.patch.object(
subprocess,
"run",
return_value=subprocess.CompletedProcess(
["pwsh"], returncode=0, stdout=completion_path_bytes
),
):
result = runner.invoke(app, ["--install-completion"])
install_script = "Register-ArgumentCompleter -Native -CommandName mocked-typer-testing-app -ScriptBlock $scriptblock"
parent: Path = completion_path.parent
parent.mkdir(parents=True, exist_ok=True)
completion_path.write_text(install_script)
new_text = completion_path.read_text()
completion_path.write_text(text)
assert install_script not in text
assert install_script in new_text
assert "completion installed in" in result.stdout
assert "Completion will take effect once you restart the terminal" in result.stdout