Coverage for tests/test_completion/test_completion.py: 100%
49 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-03-10 00:15 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-03-10 00:15 +0000
1import os 1cabdefghi
2import subprocess 1cabdefghi
3import sys 1cabdefghi
4from pathlib import Path 1cabdefghi
6from docs_src.commands.index import tutorial001 as mod 1cabdefghi
8from ..utils import needs_bash, needs_linux, requires_completion_permission 1cabdefghi
11@needs_bash 1cabdefghi
12@needs_linux 1cabdefghi
13def test_show_completion(): 1abdefghi
14 result = subprocess.run( 1cab
15 [
16 "bash",
17 "-c",
18 f"'{sys.executable}' -m coverage run '{mod.__file__}' --show-completion",
19 ],
20 capture_output=True,
21 encoding="utf-8",
22 env={**os.environ, "SHELL": "/bin/bash", "_TYPER_COMPLETE_TESTING": "True"},
23 )
24 assert "_TUTORIAL001.PY_COMPLETE=complete_bash" in result.stdout 1cab
27@needs_bash 1cabdefghi
28@needs_linux 1cabdefghi
29@requires_completion_permission 1cabdefghi
30def test_install_completion(): 1abdefghi
31 bash_completion_path: Path = Path.home() / ".bashrc" 1cab
32 text = "" 1cab
33 if bash_completion_path.is_file(): # pragma: no cover 1cab
34 text = bash_completion_path.read_text() 1cab
35 result = subprocess.run( 1cab
36 [
37 "bash",
38 "-c",
39 f"'{sys.executable}' -m coverage run '{mod.__file__}' --install-completion",
40 ],
41 capture_output=True,
42 encoding="utf-8",
43 env={**os.environ, "SHELL": "/bin/bash", "_TYPER_COMPLETE_TESTING": "True"},
44 )
45 new_text = bash_completion_path.read_text() 1cab
46 bash_completion_path.write_text(text) 1cab
47 assert "source" in new_text 1cab
48 assert str(Path(".bash_completions/tutorial001.py.sh")) in new_text 1cab
49 assert "completion installed in" in result.stdout 1cab
50 assert "Completion will take effect once you restart the terminal" in result.stdout 1cab
53def test_completion_invalid_instruction(): 1cabdefghi
54 result = subprocess.run( 1cabdefghi
55 [sys.executable, "-m", "coverage", "run", mod.__file__],
56 capture_output=True,
57 encoding="utf-8",
58 env={
59 **os.environ,
60 "_TUTORIAL001.PY_COMPLETE": "sourcebash",
61 },
62 )
63 assert result.returncode != 0 1cabdefghi
64 assert "Invalid completion instruction." in result.stderr 1cabdefghi
67def test_completion_source_bash(): 1cabdefghi
68 result = subprocess.run( 1cabdefghi
69 [sys.executable, "-m", "coverage", "run", mod.__file__],
70 capture_output=True,
71 encoding="utf-8",
72 env={
73 **os.environ,
74 "_TUTORIAL001.PY_COMPLETE": "source_bash",
75 },
76 )
77 assert ( 1cabdefghi
78 "complete -o default -F _tutorial001py_completion tutorial001.py"
79 in result.stdout
80 )
83def test_completion_source_invalid_shell(): 1cabdefghi
84 result = subprocess.run( 1cabdefghi
85 [sys.executable, "-m", "coverage", "run", mod.__file__],
86 capture_output=True,
87 encoding="utf-8",
88 env={
89 **os.environ,
90 "_TUTORIAL001.PY_COMPLETE": "source_xxx",
91 },
92 )
93 assert "Shell xxx not supported." in result.stderr 1cabdefghi
96def test_completion_source_invalid_instruction(): 1cabdefghi
97 result = subprocess.run( 1cabdefghi
98 [sys.executable, "-m", "coverage", "run", mod.__file__],
99 capture_output=True,
100 encoding="utf-8",
101 env={
102 **os.environ,
103 "_TUTORIAL001.PY_COMPLETE": "explode_bash",
104 },
105 )
106 assert 'Completion instruction "explode" not supported.' in result.stderr 1cabdefghi
109def test_completion_source_zsh(): 1cabdefghi
110 result = subprocess.run( 1cabdefghi
111 [sys.executable, "-m", "coverage", "run", mod.__file__],
112 capture_output=True,
113 encoding="utf-8",
114 env={
115 **os.environ,
116 "_TUTORIAL001.PY_COMPLETE": "source_zsh",
117 },
118 )
119 assert "compdef _tutorial001py_completion tutorial001.py" in result.stdout 1cabdefghi
122def test_completion_source_fish(): 1cabdefghi
123 result = subprocess.run( 1cabdefghi
124 [sys.executable, "-m", "coverage", "run", mod.__file__],
125 capture_output=True,
126 encoding="utf-8",
127 env={
128 **os.environ,
129 "_TUTORIAL001.PY_COMPLETE": "source_fish",
130 },
131 )
132 assert "complete --command tutorial001.py --no-files" in result.stdout 1cabdefghi
135def test_completion_source_powershell(): 1cabdefghi
136 result = subprocess.run( 1cabdefghi
137 [sys.executable, "-m", "coverage", "run", mod.__file__],
138 capture_output=True,
139 encoding="utf-8",
140 env={
141 **os.environ,
142 "_TUTORIAL001.PY_COMPLETE": "source_powershell",
143 },
144 )
145 assert ( 1cabdefghi
146 "Register-ArgumentCompleter -Native -CommandName tutorial001.py -ScriptBlock $scriptblock"
147 in result.stdout
148 )
151def test_completion_source_pwsh(): 1cabdefghi
152 result = subprocess.run( 1cabdefghi
153 [sys.executable, "-m", "coverage", "run", mod.__file__],
154 capture_output=True,
155 encoding="utf-8",
156 env={
157 **os.environ,
158 "_TUTORIAL001.PY_COMPLETE": "source_pwsh",
159 },
160 )
161 assert ( 1cabdefghi
162 "Register-ArgumentCompleter -Native -CommandName tutorial001.py -ScriptBlock $scriptblock"
163 in result.stdout
164 )