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