Coverage for tests / test_completion / test_completion.py: 100%
60 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-18 11:12 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-18 11:12 +0000
1import os 1abcdefghij
2import subprocess 1abcdefghij
3import sys 1abcdefghij
4from pathlib import Path 1abcdefghij
6from typer._click.shell_completion import CompletionItem 1abcdefghij
8from docs_src.typer_app import tutorial001 as mod 1abcdefghij
10from ..utils import needs_bash, needs_linux, requires_completion_permission 1abcdefghij
11from . import completion_option_then_argument as mod_option_arg 1abcdefghij
14@needs_bash 1abcdefghij
15@needs_linux 1abcdefghij
16def test_show_completion(): 1abcdefghij
17 result = subprocess.run( 1abcd
18 [
19 "bash",
20 "-c",
21 f"'{sys.executable}' -m coverage run '{mod.__file__}' --show-completion",
22 ],
23 capture_output=True,
24 encoding="utf-8",
25 env={**os.environ, "SHELL": "/bin/bash", "_TYPER_COMPLETE_TESTING": "True"},
26 )
27 assert "_TUTORIAL001.PY_COMPLETE=complete_bash" in result.stdout 1abcd
30@needs_bash 1abcdefghij
31@needs_linux 1abcdefghij
32@requires_completion_permission 1abcdefghij
33def test_install_completion(): 1abcdefghij
34 bash_completion_path: Path = Path.home() / ".bashrc" 1abcd
35 text = "" 1abcd
36 if bash_completion_path.is_file(): # pragma: no cover 1abcd
37 text = bash_completion_path.read_text() 1abcd
38 result = subprocess.run( 1abcd
39 [
40 "bash",
41 "-c",
42 f"'{sys.executable}' -m coverage run '{mod.__file__}' --install-completion",
43 ],
44 capture_output=True,
45 encoding="utf-8",
46 env={**os.environ, "SHELL": "/bin/bash", "_TYPER_COMPLETE_TESTING": "True"},
47 )
48 new_text = bash_completion_path.read_text() 1abcd
49 bash_completion_path.write_text(text) 1abcd
50 assert "source" in new_text 1abcd
51 assert str(Path(".bash_completions/tutorial001.py.sh")) in new_text 1abcd
52 assert "completion installed in" in result.stdout 1abcd
53 assert "Completion will take effect once you restart the terminal" in result.stdout 1abcd
56def test_completion_invalid_instruction(): 1abcdefghij
57 result = subprocess.run( 1abcdefghij
58 [sys.executable, "-m", "coverage", "run", mod.__file__],
59 capture_output=True,
60 encoding="utf-8",
61 env={
62 **os.environ,
63 "_TUTORIAL001.PY_COMPLETE": "sourcebash",
64 },
65 )
66 assert result.returncode != 0 1abcdefghij
67 assert "Invalid completion instruction." in result.stderr 1abcdefghij
70def test_completion_source_bash(): 1abcdefghij
71 result = subprocess.run( 1abcdefghij
72 [sys.executable, "-m", "coverage", "run", mod.__file__],
73 capture_output=True,
74 encoding="utf-8",
75 env={
76 **os.environ,
77 "_TUTORIAL001.PY_COMPLETE": "source_bash",
78 },
79 )
80 assert ( 1abcdefghij
81 "complete -o default -F _tutorial001py_completion tutorial001.py"
82 in result.stdout
83 )
86def test_completion_source_invalid_shell(): 1abcdefghij
87 result = subprocess.run( 1abcdefghij
88 [sys.executable, "-m", "coverage", "run", mod.__file__],
89 capture_output=True,
90 encoding="utf-8",
91 env={
92 **os.environ,
93 "_TUTORIAL001.PY_COMPLETE": "source_xxx",
94 },
95 )
96 assert "Shell xxx not supported." in result.stderr 1abcdefghij
99def test_completion_source_invalid_instruction(): 1abcdefghij
100 result = subprocess.run( 1abcdefghij
101 [sys.executable, "-m", "coverage", "run", mod.__file__],
102 capture_output=True,
103 encoding="utf-8",
104 env={
105 **os.environ,
106 "_TUTORIAL001.PY_COMPLETE": "explode_bash",
107 },
108 )
109 assert 'Completion instruction "explode" not supported.' in result.stderr 1abcdefghij
112def test_completion_source_zsh(): 1abcdefghij
113 result = subprocess.run( 1abcdefghij
114 [sys.executable, "-m", "coverage", "run", mod.__file__],
115 capture_output=True,
116 encoding="utf-8",
117 env={
118 **os.environ,
119 "_TUTORIAL001.PY_COMPLETE": "source_zsh",
120 },
121 )
122 assert "compdef _tutorial001py_completion tutorial001.py" in result.stdout 1abcdefghij
125def test_completion_source_fish(): 1abcdefghij
126 result = subprocess.run( 1abcdefghij
127 [sys.executable, "-m", "coverage", "run", mod.__file__],
128 capture_output=True,
129 encoding="utf-8",
130 env={
131 **os.environ,
132 "_TUTORIAL001.PY_COMPLETE": "source_fish",
133 },
134 )
135 assert "complete --command tutorial001.py --no-files" in result.stdout 1abcdefghij
138def test_completion_source_powershell(): 1abcdefghij
139 result = subprocess.run( 1abcdefghij
140 [sys.executable, "-m", "coverage", "run", mod.__file__],
141 capture_output=True,
142 encoding="utf-8",
143 env={
144 **os.environ,
145 "_TUTORIAL001.PY_COMPLETE": "source_powershell",
146 },
147 )
148 assert ( 1abcdefghij
149 "Register-ArgumentCompleter -Native -CommandName tutorial001.py -ScriptBlock $scriptblock"
150 in result.stdout
151 )
154def test_completion_source_pwsh(): 1abcdefghij
155 result = subprocess.run( 1abcdefghij
156 [sys.executable, "-m", "coverage", "run", mod.__file__],
157 capture_output=True,
158 encoding="utf-8",
159 env={
160 **os.environ,
161 "_TUTORIAL001.PY_COMPLETE": "source_pwsh",
162 },
163 )
164 assert ( 1abcdefghij
165 "Register-ArgumentCompleter -Native -CommandName tutorial001.py -ScriptBlock $scriptblock"
166 in result.stdout
167 )
170def test_completion_option_argument() -> None: 1abcdefghij
171 file_name = Path(mod_option_arg.__file__).name 1abcdefghij
172 result = subprocess.run( 1abcdefghij
173 [sys.executable, "-m", "coverage", "run", mod_option_arg.__file__, " "],
174 capture_output=True,
175 encoding="utf-8",
176 env={
177 **os.environ,
178 f"_{file_name.upper()}_COMPLETE": "complete_bash",
179 "COMP_WORDS": f"{file_name} --name chosen ",
180 "COMP_CWORD": "3",
181 },
182 )
183 assert "arg-choice" in result.stdout 1abcdefghij
184 assert "opt-choice" not in result.stdout 1abcdefghij
187def test_completion_item_getattr() -> None: 1abcdefghij
188 item = CompletionItem("demo", source="envvar") 1abcdefghij
190 assert item.source == "envvar" 1abcdefghij
191 assert item.missing is None 1abcdefghij