Coverage for tests / test_completion / test_completion_show.py: 100%
32 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-26 21:46 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-26 21:46 +0000
1import os 1abcdefg
2import subprocess 1abcdefg
3import sys 1abcdefg
4from unittest import mock 1abcdefg
6import typer 1abcdefg
7import typer.completion 1abcdefg
8from typer.testing import CliRunner 1abcdefg
10from docs_src.typer_app import tutorial001_py310 as mod 1abcdefg
12runner = CliRunner() 1abcdefg
13app = mod.app 1abcdefg
16def test_completion_show_no_shell(): 1abcdefg
17 result = subprocess.run( 1abcdefg
18 [sys.executable, "-m", "coverage", "run", mod.__file__, "--show-completion"],
19 capture_output=True,
20 encoding="utf-8",
21 env={
22 **os.environ,
23 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
24 },
25 )
26 assert "Option '--show-completion' requires an argument" in result.stderr 1abcdefg
29def test_completion_show_bash(): 1abcdefg
30 result = subprocess.run( 1abcdefg
31 [
32 sys.executable,
33 "-m",
34 "coverage",
35 "run",
36 mod.__file__,
37 "--show-completion",
38 "bash",
39 ],
40 capture_output=True,
41 encoding="utf-8",
42 env={
43 **os.environ,
44 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
45 },
46 )
47 assert ( 1abcdefg
48 "complete -o default -F _tutorial001_py310py_completion tutorial001_py310.py"
49 in result.stdout
50 )
53def test_completion_source_zsh(): 1abcdefg
54 result = subprocess.run( 1abcdefg
55 [
56 sys.executable,
57 "-m",
58 "coverage",
59 "run",
60 mod.__file__,
61 "--show-completion",
62 "zsh",
63 ],
64 capture_output=True,
65 encoding="utf-8",
66 env={
67 **os.environ,
68 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
69 },
70 )
71 assert ( 1abcdefg
72 "compdef _tutorial001_py310py_completion tutorial001_py310.py" in result.stdout
73 )
76def test_completion_source_fish(): 1abcdefg
77 result = subprocess.run( 1abcdefg
78 [
79 sys.executable,
80 "-m",
81 "coverage",
82 "run",
83 mod.__file__,
84 "--show-completion",
85 "fish",
86 ],
87 capture_output=True,
88 encoding="utf-8",
89 env={
90 **os.environ,
91 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
92 },
93 )
94 assert "complete --command tutorial001_py310.py --no-files" in result.stdout 1abcdefg
97def test_completion_source_powershell(): 1abcdefg
98 result = subprocess.run( 1abcdefg
99 [
100 sys.executable,
101 "-m",
102 "coverage",
103 "run",
104 mod.__file__,
105 "--show-completion",
106 "powershell",
107 ],
108 capture_output=True,
109 encoding="utf-8",
110 env={
111 **os.environ,
112 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
113 },
114 )
115 assert ( 1abcdefg
116 "Register-ArgumentCompleter -Native -CommandName tutorial001_py310.py -ScriptBlock $scriptblock"
117 in result.stdout
118 )
121def test_completion_source_pwsh(): 1abcdefg
122 result = subprocess.run( 1abcdefg
123 [
124 sys.executable,
125 "-m",
126 "coverage",
127 "run",
128 mod.__file__,
129 "--show-completion",
130 "pwsh",
131 ],
132 capture_output=True,
133 encoding="utf-8",
134 env={
135 **os.environ,
136 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
137 },
138 )
139 assert ( 1abcdefg
140 "Register-ArgumentCompleter -Native -CommandName tutorial001_py310.py -ScriptBlock $scriptblock"
141 in result.stdout
142 )
145def test_completion_show_invalid_shell(): 1abcdefg
146 with mock.patch.object(typer.completion, "_get_shell_name", return_value="xshell"): 1abcdefg
147 result = runner.invoke(app, ["--show-completion"]) 1abcdefg
148 assert "Shell xshell not supported" in result.output 1abcdefg