Coverage for tests / test_completion / test_completion_show.py: 100%
32 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-09 12:36 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-09 12:36 +0000
1import os 1abcdefgh
2import subprocess 1abcdefgh
3import sys 1abcdefgh
4from unittest import mock 1abcdefgh
6import typer 1abcdefgh
7import typer.completion 1abcdefgh
8from typer.testing import CliRunner 1abcdefgh
10from docs_src.typer_app import tutorial001_py39 as mod 1abcdefgh
12runner = CliRunner() 1abcdefgh
13app = mod.app 1abcdefgh
16def test_completion_show_no_shell(): 1abcdefgh
17 result = subprocess.run( 1abcdefgh
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 1abcdefgh
29def test_completion_show_bash(): 1abcdefgh
30 result = subprocess.run( 1abcdefgh
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 ( 1abcdefgh
48 "complete -o default -F _tutorial001_py39py_completion tutorial001_py39.py"
49 in result.stdout
50 )
53def test_completion_source_zsh(): 1abcdefgh
54 result = subprocess.run( 1abcdefgh
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 "compdef _tutorial001_py39py_completion tutorial001_py39.py" in result.stdout 1abcdefgh
74def test_completion_source_fish(): 1abcdefgh
75 result = subprocess.run( 1abcdefgh
76 [
77 sys.executable,
78 "-m",
79 "coverage",
80 "run",
81 mod.__file__,
82 "--show-completion",
83 "fish",
84 ],
85 capture_output=True,
86 encoding="utf-8",
87 env={
88 **os.environ,
89 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
90 },
91 )
92 assert "complete --command tutorial001_py39.py --no-files" in result.stdout 1abcdefgh
95def test_completion_source_powershell(): 1abcdefgh
96 result = subprocess.run( 1abcdefgh
97 [
98 sys.executable,
99 "-m",
100 "coverage",
101 "run",
102 mod.__file__,
103 "--show-completion",
104 "powershell",
105 ],
106 capture_output=True,
107 encoding="utf-8",
108 env={
109 **os.environ,
110 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
111 },
112 )
113 assert ( 1abcdefgh
114 "Register-ArgumentCompleter -Native -CommandName tutorial001_py39.py -ScriptBlock $scriptblock"
115 in result.stdout
116 )
119def test_completion_source_pwsh(): 1abcdefgh
120 result = subprocess.run( 1abcdefgh
121 [
122 sys.executable,
123 "-m",
124 "coverage",
125 "run",
126 mod.__file__,
127 "--show-completion",
128 "pwsh",
129 ],
130 capture_output=True,
131 encoding="utf-8",
132 env={
133 **os.environ,
134 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
135 },
136 )
137 assert ( 1abcdefgh
138 "Register-ArgumentCompleter -Native -CommandName tutorial001_py39.py -ScriptBlock $scriptblock"
139 in result.stdout
140 )
143def test_completion_show_invalid_shell(): 1abcdefgh
144 with mock.patch.object(typer.completion, "_get_shell_name", return_value="xshell"): 1abcdefgh
145 result = runner.invoke(app, ["--show-completion"]) 1abcdefgh
146 assert "Shell xshell not supported" in result.output 1abcdefgh