Coverage for tests/test_completion/test_completion_install.py: 100%
82 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-13 11:07 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-13 11:07 +0000
1import os 1habfgcde
2import subprocess 1habfgcde
3import sys 1habfgcde
4from pathlib import Path 1habfgcde
5from unittest import mock 1habfgcde
7import shellingham 1habfgcde
8import typer 1habfgcde
9from typer.testing import CliRunner 1habfgcde
11from docs_src.commands.index import tutorial001 as mod 1habfgcde
13from ..utils import requires_completion_permission 1habfgcde
15runner = CliRunner() 1habfgcde
16app = typer.Typer() 1habfgcde
17app.command()(mod.main) 1habfgcde
20@requires_completion_permission 1habfgcde
21def test_completion_install_no_shell(): 1abfgcde
22 result = subprocess.run( 1habfgcde
23 [sys.executable, "-m", "coverage", "run", mod.__file__, "--install-completion"],
24 capture_output=True,
25 encoding="utf-8",
26 env={
27 **os.environ,
28 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
29 },
30 )
31 assert "Option '--install-completion' requires an argument" in result.stderr 1habfgcde
34@requires_completion_permission 1habfgcde
35def test_completion_install_bash(): 1abfgcde
36 bash_completion_path: Path = Path.home() / ".bashrc" 1habfgcde
37 text = "" 1habfgcde
38 if bash_completion_path.is_file(): 1habfgcde
39 text = bash_completion_path.read_text() 1habcde
40 result = subprocess.run( 1habfgcde
41 [
42 sys.executable,
43 "-m",
44 "coverage",
45 "run",
46 mod.__file__,
47 "--install-completion",
48 "bash",
49 ],
50 capture_output=True,
51 encoding="utf-8",
52 env={
53 **os.environ,
54 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
55 },
56 )
57 new_text = bash_completion_path.read_text() 1habfgcde
58 bash_completion_path.write_text(text) 1habfgcde
59 install_source = Path(".bash_completions/tutorial001.py.sh") 1habfgcde
60 assert str(install_source) not in text 1habfgcde
61 assert str(install_source) in new_text 1habfgcde
62 assert "completion installed in" in result.stdout 1habfgcde
63 assert "Completion will take effect once you restart the terminal" in result.stdout 1habfgcde
64 install_source_path = Path.home() / install_source 1habfgcde
65 assert install_source_path.is_file() 1habfgcde
66 install_content = install_source_path.read_text() 1habfgcde
67 install_source_path.unlink() 1habfgcde
68 assert ( 1habfgcde
69 "complete -o default -F _tutorial001py_completion tutorial001.py"
70 in install_content
71 )
74@requires_completion_permission 1habfgcde
75def test_completion_install_zsh(): 1abfgcde
76 completion_path: Path = Path.home() / ".zshrc" 1habfgcde
77 text = "" 1habfgcde
78 if not completion_path.is_file(): # pragma: no cover 1habfgcde
79 completion_path.write_text('echo "custom .zshrc"') 1habfgcde
80 if completion_path.is_file(): 1habfgcde
81 text = completion_path.read_text() 1habfgcde
82 result = subprocess.run( 1habfgcde
83 [
84 sys.executable,
85 "-m",
86 "coverage",
87 "run",
88 mod.__file__,
89 "--install-completion",
90 "zsh",
91 ],
92 capture_output=True,
93 encoding="utf-8",
94 env={
95 **os.environ,
96 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
97 },
98 )
99 new_text = completion_path.read_text() 1habfgcde
100 completion_path.write_text(text) 1habfgcde
101 zfunc_fragment = "fpath+=~/.zfunc" 1habfgcde
102 assert zfunc_fragment in new_text 1habfgcde
103 assert "completion installed in" in result.stdout 1habfgcde
104 assert "Completion will take effect once you restart the terminal" in result.stdout 1habfgcde
105 install_source_path = Path.home() / ".zfunc/_tutorial001.py" 1habfgcde
106 assert install_source_path.is_file() 1habfgcde
107 install_content = install_source_path.read_text() 1habfgcde
108 install_source_path.unlink() 1habfgcde
109 assert "compdef _tutorial001py_completion tutorial001.py" in install_content 1habfgcde
112@requires_completion_permission 1habfgcde
113def test_completion_install_fish(): 1abfgcde
114 script_path = Path(mod.__file__) 1habfgcde
115 completion_path: Path = ( 1abfgcde
116 Path.home() / f".config/fish/completions/{script_path.name}.fish"
117 )
118 result = subprocess.run( 1habfgcde
119 [
120 sys.executable,
121 "-m",
122 "coverage",
123 "run",
124 mod.__file__,
125 "--install-completion",
126 "fish",
127 ],
128 capture_output=True,
129 encoding="utf-8",
130 env={
131 **os.environ,
132 "_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION": "True",
133 },
134 )
135 new_text = completion_path.read_text() 1habfgcde
136 completion_path.unlink() 1habfgcde
137 assert "complete --command tutorial001.py" in new_text 1habfgcde
138 assert "completion installed in" in result.stdout 1habfgcde
139 assert "Completion will take effect once you restart the terminal" in result.stdout 1habfgcde
142@requires_completion_permission 1habfgcde
143def test_completion_install_powershell(): 1abfgcde
144 completion_path: Path = ( 1abfgcde
145 Path.home() / ".config/powershell/Microsoft.PowerShell_profile.ps1"
146 )
147 completion_path_bytes = f"{completion_path}\n".encode("windows-1252") 1habfgcde
148 text = "" 1habfgcde
149 if completion_path.is_file(): # pragma: no cover 1habfgcde
150 text = completion_path.read_text()
152 with mock.patch.object( 1habfgcde
153 shellingham, "detect_shell", return_value=("pwsh", "/usr/bin/pwsh")
154 ):
155 with mock.patch.object( 1habfgcde
156 subprocess,
157 "run",
158 return_value=subprocess.CompletedProcess(
159 ["pwsh"], returncode=0, stdout=completion_path_bytes
160 ),
161 ):
162 result = runner.invoke(app, ["--install-completion"]) 1habfgcde
163 install_script = "Register-ArgumentCompleter -Native -CommandName mocked-typer-testing-app -ScriptBlock $scriptblock" 1habfgcde
164 parent: Path = completion_path.parent 1habfgcde
165 parent.mkdir(parents=True, exist_ok=True) 1habfgcde
166 completion_path.write_text(install_script) 1habfgcde
167 new_text = completion_path.read_text() 1habfgcde
168 completion_path.write_text(text) 1habfgcde
169 assert install_script not in text 1habfgcde
170 assert install_script in new_text 1habfgcde
171 assert "completion installed in" in result.stdout 1habfgcde
172 assert "Completion will take effect once you restart the terminal" in result.stdout 1habfgcde