| 1 |
"""Tests for completion installation functionality.""" |
| 2 |
|
| 3 |
import sys |
| 4 |
from unittest.mock import patch |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from cyclopts import App |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture |
| 12 |
def temp_home(tmp_path, monkeypatch): |
| 13 |
"""Create a temporary home directory for testing.""" |
| 14 |
if sys.platform == "win32": |
| 15 |
monkeypatch.setenv("USERPROFILE", str(tmp_path)) |
| 16 |
else: |
| 17 |
monkeypatch.setenv("HOME", str(tmp_path)) |
| 18 |
monkeypatch.delenv("ZSH", raising=False) |
| 19 |
return tmp_path |
| 20 |
|
| 21 |
|
| 22 |
@pytest.fixture |
| 23 |
def omz_dir(temp_home, monkeypatch): |
| 24 |
"""Create a fake oh-my-zsh installation with $ZSH set.""" |
| 25 |
omz = temp_home / ".oh-my-zsh" |
| 26 |
(omz / "custom").mkdir(parents=True) |
| 27 |
monkeypatch.setenv("ZSH", str(omz)) |
| 28 |
return omz |
| 29 |
|
| 30 |
|
| 31 |
def test_install_completion_bash_add_to_startup_true(temp_home): |
| 32 |
"""Test that add_to_startup=True adds source line to bashrc.""" |
| 33 |
app = App(name="testapp") |
| 34 |
bashrc = temp_home / ".bashrc" |
| 35 |
|
| 36 |
install_path = app.install_completion(shell="bash", add_to_startup=True) |
| 37 |
|
| 38 |
assert install_path.exists() |
| 39 |
assert bashrc.exists() |
| 40 |
|
| 41 |
bashrc_content = bashrc.read_text() |
| 42 |
assert "# Load testapp completion" in bashrc_content |
| 43 |
assert f'[ -f "{install_path}" ] && . "{install_path}"' in bashrc_content |
| 44 |
|
| 45 |
|
| 46 |
def test_install_completion_bash_add_to_startup_false(temp_home): |
| 47 |
"""Test that add_to_startup=False does not modify bashrc.""" |
| 48 |
app = App(name="testapp") |
| 49 |
bashrc = temp_home / ".bashrc" |
| 50 |
|
| 51 |
install_path = app.install_completion(shell="bash", add_to_startup=False) |
| 52 |
|
| 53 |
assert install_path.exists() |
| 54 |
assert not bashrc.exists() |
| 55 |
|
| 56 |
|
| 57 |
def test_install_completion_bash_add_to_startup_idempotent(temp_home): |
| 58 |
"""Test that running install_completion multiple times doesn't duplicate bashrc entries.""" |
| 59 |
app = App(name="testapp") |
| 60 |
bashrc = temp_home / ".bashrc" |
| 61 |
|
| 62 |
app.install_completion(shell="bash", add_to_startup=True) |
| 63 |
first_content = bashrc.read_text() |
| 64 |
|
| 65 |
app.install_completion(shell="bash", add_to_startup=True) |
| 66 |
second_content = bashrc.read_text() |
| 67 |
|
| 68 |
assert first_content == second_content |
| 69 |
assert first_content.count("# Load testapp completion") == 1 |
| 70 |
|
| 71 |
|
| 72 |
def test_install_completion_bash_add_to_startup_preserves_existing(temp_home): |
| 73 |
"""Test that add_to_startup=True preserves existing bashrc content.""" |
| 74 |
app = App(name="testapp") |
| 75 |
bashrc = temp_home / ".bashrc" |
| 76 |
|
| 77 |
existing_content = "# Existing config\nexport PATH=/usr/local/bin:$PATH\n" |
| 78 |
bashrc.write_text(existing_content) |
| 79 |
|
| 80 |
app.install_completion(shell="bash", add_to_startup=True) |
| 81 |
|
| 82 |
bashrc_content = bashrc.read_text() |
| 83 |
assert existing_content in bashrc_content |
| 84 |
assert "# Load testapp completion" in bashrc_content |
| 85 |
|
| 86 |
|
| 87 |
def test_install_completion_zsh_add_to_startup_true(temp_home): |
| 88 |
"""Test that add_to_startup=True adds fpath line to zshrc.""" |
| 89 |
app = App(name="testapp") |
| 90 |
zshrc = temp_home / ".zshrc" |
| 91 |
|
| 92 |
install_path = app.install_completion(shell="zsh", add_to_startup=True) |
| 93 |
|
| 94 |
assert install_path.exists() |
| 95 |
assert zshrc.exists() |
| 96 |
|
| 97 |
zshrc_content = zshrc.read_text() |
| 98 |
completion_dir = install_path.parent |
| 99 |
assert "# testapp completions" in zshrc_content |
| 100 |
assert f"fpath=({completion_dir} $fpath)" in zshrc_content |
| 101 |
|
| 102 |
|
| 103 |
def test_install_completion_zsh_add_to_startup_false(temp_home): |
| 104 |
"""Test that add_to_startup=False does not modify zshrc.""" |
| 105 |
app = App(name="testapp") |
| 106 |
zshrc = temp_home / ".zshrc" |
| 107 |
|
| 108 |
install_path = app.install_completion(shell="zsh", add_to_startup=False) |
| 109 |
|
| 110 |
assert install_path.exists() |
| 111 |
assert not zshrc.exists() |
| 112 |
|
| 113 |
|
| 114 |
def test_install_completion_zsh_add_to_startup_idempotent(temp_home): |
| 115 |
"""Test that running install_completion multiple times doesn't duplicate zshrc entries.""" |
| 116 |
app = App(name="testapp") |
| 117 |
zshrc = temp_home / ".zshrc" |
| 118 |
|
| 119 |
app.install_completion(shell="zsh", add_to_startup=True) |
| 120 |
first_content = zshrc.read_text() |
| 121 |
|
| 122 |
app.install_completion(shell="zsh", add_to_startup=True) |
| 123 |
second_content = zshrc.read_text() |
| 124 |
|
| 125 |
assert first_content == second_content |
| 126 |
assert first_content.count("# testapp completions") == 1 |
| 127 |
|
| 128 |
|
| 129 |
def test_install_completion_custom_output_path(temp_home): |
| 130 |
"""Test that custom output path works with add_to_startup.""" |
| 131 |
app = App(name="testapp") |
| 132 |
custom_path = temp_home / "custom" / "completion.sh" |
| 133 |
bashrc = temp_home / ".bashrc" |
| 134 |
|
| 135 |
install_path = app.install_completion(shell="bash", output=custom_path, add_to_startup=True) |
| 136 |
|
| 137 |
assert install_path == custom_path |
| 138 |
assert install_path.exists() |
| 139 |
assert bashrc.exists() |
| 140 |
|
| 141 |
bashrc_content = bashrc.read_text() |
| 142 |
assert str(custom_path) in bashrc_content |
| 143 |
|
| 144 |
|
| 145 |
def test_register_install_completion_command_default_add_to_startup(temp_home): |
| 146 |
"""Test that register_install_completion_command defaults to add_to_startup=True.""" |
| 147 |
app = App(name="testapp") |
| 148 |
app.register_install_completion_command() |
| 149 |
|
| 150 |
bashrc = temp_home / ".bashrc" |
| 151 |
|
| 152 |
with patch("sys.exit"): |
| 153 |
try: |
| 154 |
app(["--install-completion", "--shell", "bash"], exit_on_error=False) |
| 155 |
except SystemExit: |
| 156 |
pass |
| 157 |
|
| 158 |
assert bashrc.exists() |
| 159 |
bashrc_content = bashrc.read_text() |
| 160 |
assert "# Load testapp completion" in bashrc_content |
| 161 |
|
| 162 |
|
| 163 |
def test_register_install_completion_command_add_to_startup_false(temp_home): |
| 164 |
"""Test that register_install_completion_command respects add_to_startup=False.""" |
| 165 |
app = App(name="testapp") |
| 166 |
app.register_install_completion_command(add_to_startup=False) |
| 167 |
|
| 168 |
bashrc = temp_home / ".bashrc" |
| 169 |
|
| 170 |
with patch("sys.exit"): |
| 171 |
try: |
| 172 |
app(["--install-completion", "--shell", "bash"], exit_on_error=False) |
| 173 |
except SystemExit: |
| 174 |
pass |
| 175 |
|
| 176 |
assert not bashrc.exists() |
| 177 |
|
| 178 |
|
| 179 |
def test_install_completion_path_with_spaces(temp_home): |
| 180 |
"""Test that paths with spaces are properly quoted in RC file.""" |
| 181 |
app = App(name="testapp") |
| 182 |
custom_path = temp_home / "my scripts" / "completion.sh" |
| 183 |
bashrc = temp_home / ".bashrc" |
| 184 |
|
| 185 |
install_path = app.install_completion(shell="bash", output=custom_path, add_to_startup=True) |
| 186 |
|
| 187 |
assert install_path.exists() |
| 188 |
assert bashrc.exists() |
| 189 |
|
| 190 |
bashrc_content = bashrc.read_text() |
| 191 |
assert f'[ -f "{custom_path}" ] && . "{custom_path}"' in bashrc_content |
| 192 |
|
| 193 |
|
| 194 |
def test_register_install_completion_command_custom_help(): |
| 195 |
"""Test that register_install_completion_command respects custom help parameter.""" |
| 196 |
app = App(name="testapp") |
| 197 |
custom_help = "My custom installation help text." |
| 198 |
app.register_install_completion_command(help=custom_help) |
| 199 |
|
| 200 |
|
| 201 |
install_cmd_app = app["--install-completion"] |
| 202 |
|
| 203 |
assert install_cmd_app.help == custom_help |
| 204 |
|
| 205 |
|
| 206 |
def test_install_completion_fish(temp_home): |
| 207 |
"""Test that fish completion installs correctly.""" |
| 208 |
app = App(name="testapp") |
| 209 |
|
| 210 |
install_path = app.install_completion(shell="fish", add_to_startup=False) |
| 211 |
|
| 212 |
assert install_path.exists() |
| 213 |
assert install_path.name == "testapp.fish" |
| 214 |
assert install_path.parent == temp_home / ".config" / "fish" / "completions" |
| 215 |
|
| 216 |
|
| 217 |
def test_install_completion_command_shell_detection_error(temp_home, monkeypatch, capsys): |
| 218 |
"""Test that install-completion command handles shell detection errors.""" |
| 219 |
from cyclopts.completion.detect import ShellDetectionError |
| 220 |
|
| 221 |
app = App(name="testapp") |
| 222 |
app.register_install_completion_command() |
| 223 |
|
| 224 |
def mock_detect_shell(): |
| 225 |
raise ShellDetectionError("Cannot detect shell") |
| 226 |
|
| 227 |
monkeypatch.setattr("cyclopts.completion.detect.detect_shell", mock_detect_shell) |
| 228 |
|
| 229 |
with pytest.raises(SystemExit) as exc_info: |
| 230 |
app(["--install-completion"], exit_on_error=False) |
| 231 |
|
| 232 |
assert exc_info.value.code == 1 |
| 233 |
captured = capsys.readouterr() |
| 234 |
assert "Could not auto-detect shell" in captured.err |
| 235 |
assert "Please specify --shell explicitly" in captured.err |
| 236 |
|
| 237 |
|
| 238 |
def test_install_completion_command_zsh_with_add_to_startup(temp_home, monkeypatch, capsys): |
| 239 |
"""Test that install-completion command prints zsh instructions with add_to_startup.""" |
| 240 |
app = App(name="testapp") |
| 241 |
app.register_install_completion_command(add_to_startup=True) |
| 242 |
|
| 243 |
monkeypatch.setattr("cyclopts.completion.detect.detect_shell", lambda: "zsh") |
| 244 |
|
| 245 |
with patch("sys.exit"): |
| 246 |
try: |
| 247 |
app(["--install-completion"], exit_on_error=False) |
| 248 |
except SystemExit: |
| 249 |
pass |
| 250 |
|
| 251 |
captured = capsys.readouterr() |
| 252 |
assert "Completion script installed" in captured.out |
| 253 |
assert "fpath" in captured.out |
| 254 |
assert ".zshrc" in captured.out |
| 255 |
assert "exec zsh" in captured.out |
| 256 |
|
| 257 |
|
| 258 |
def test_install_completion_command_zsh_without_add_to_startup(temp_home, monkeypatch, capsys): |
| 259 |
"""Test that install-completion command prints zsh instructions without add_to_startup.""" |
| 260 |
app = App(name="testapp") |
| 261 |
app.register_install_completion_command(add_to_startup=False) |
| 262 |
|
| 263 |
monkeypatch.setattr("cyclopts.completion.detect.detect_shell", lambda: "zsh") |
| 264 |
|
| 265 |
with patch("sys.exit"): |
| 266 |
try: |
| 267 |
app(["--install-completion"], exit_on_error=False) |
| 268 |
except SystemExit: |
| 269 |
pass |
| 270 |
|
| 271 |
captured = capsys.readouterr() |
| 272 |
assert "Completion script installed" in captured.out |
| 273 |
assert "ensure" in captured.out.lower() and "$fpath" in captured.out |
| 274 |
assert "fpath=" in captured.out |
| 275 |
assert "autoload -Uz compinit" in captured.out |
| 276 |
assert "exec zsh" in captured.out |
| 277 |
|
| 278 |
|
| 279 |
def test_install_completion_command_bash_with_add_to_startup(temp_home, monkeypatch, capsys): |
| 280 |
"""Test that install-completion command prints bash instructions with add_to_startup.""" |
| 281 |
app = App(name="testapp") |
| 282 |
app.register_install_completion_command(add_to_startup=True) |
| 283 |
|
| 284 |
monkeypatch.setattr("cyclopts.completion.detect.detect_shell", lambda: "bash") |
| 285 |
|
| 286 |
with patch("sys.exit"): |
| 287 |
try: |
| 288 |
app(["--install-completion"], exit_on_error=False) |
| 289 |
except SystemExit: |
| 290 |
pass |
| 291 |
|
| 292 |
captured = capsys.readouterr() |
| 293 |
assert "Completion script installed" in captured.out |
| 294 |
assert "Added completion loader to" in captured.out |
| 295 |
assert ".bashrc" in captured.out |
| 296 |
assert "source ~/.bashrc" in captured.out |
| 297 |
|
| 298 |
|
| 299 |
def test_install_completion_command_bash_without_add_to_startup(temp_home, monkeypatch, capsys): |
| 300 |
"""Test that install-completion command prints bash instructions without add_to_startup.""" |
| 301 |
app = App(name="testapp") |
| 302 |
app.register_install_completion_command(add_to_startup=False) |
| 303 |
|
| 304 |
monkeypatch.setattr("cyclopts.completion.detect.detect_shell", lambda: "bash") |
| 305 |
|
| 306 |
with patch("sys.exit"): |
| 307 |
try: |
| 308 |
app(["--install-completion"], exit_on_error=False) |
| 309 |
except SystemExit: |
| 310 |
pass |
| 311 |
|
| 312 |
captured = capsys.readouterr() |
| 313 |
assert "Completion script installed" in captured.out |
| 314 |
assert "automatically loaded by bash-completion" in captured.out |
| 315 |
assert "bash-completion is installed" in captured.out |
| 316 |
assert "exec bash" in captured.out |
| 317 |
|
| 318 |
|
| 319 |
def test_install_completion_command_fish(temp_home, monkeypatch, capsys): |
| 320 |
"""Test that install-completion command prints fish instructions.""" |
| 321 |
app = App(name="testapp") |
| 322 |
app.register_install_completion_command() |
| 323 |
|
| 324 |
monkeypatch.setattr("cyclopts.completion.detect.detect_shell", lambda: "fish") |
| 325 |
|
| 326 |
with patch("sys.exit"): |
| 327 |
try: |
| 328 |
app(["--install-completion"], exit_on_error=False) |
| 329 |
except SystemExit: |
| 330 |
pass |
| 331 |
|
| 332 |
captured = capsys.readouterr() |
| 333 |
assert "Completion script installed" in captured.out |
| 334 |
assert "automatically loaded in fish" in captured.out |
| 335 |
assert "source ~/.config/fish/config.fish" in captured.out |
| 336 |
|
| 337 |
|
| 338 |
def test_install_completion_zsh_ohmyzsh_default_path(omz_dir): |
| 339 |
"""Test that $ZSH set with valid dir installs to $ZSH/custom/completions/_cyclopts_testapp.""" |
| 340 |
app = App(name="testapp") |
| 341 |
install_path = app.install_completion(shell="zsh", add_to_startup=True) |
| 342 |
|
| 343 |
assert install_path == omz_dir / "custom" / "completions" / "_cyclopts_testapp" |
| 344 |
assert install_path.exists() |
| 345 |
|
| 346 |
|
| 347 |
def test_install_completion_zsh_ohmyzsh_zsh_custom_env(omz_dir, temp_home, monkeypatch): |
| 348 |
"""Test that $ZSH_CUSTOM takes precedence over $ZSH/custom.""" |
| 349 |
custom_dir = temp_home / "my-custom-omz" |
| 350 |
custom_dir.mkdir() |
| 351 |
monkeypatch.setenv("ZSH_CUSTOM", str(custom_dir)) |
| 352 |
|
| 353 |
app = App(name="testapp") |
| 354 |
install_path = app.install_completion(shell="zsh", add_to_startup=True) |
| 355 |
|
| 356 |
assert install_path == custom_dir / "completions" / "_cyclopts_testapp" |
| 357 |
assert install_path.exists() |
| 358 |
|
| 359 |
|
| 360 |
def test_install_completion_zsh_ohmyzsh_no_zshrc_modification(omz_dir, temp_home): |
| 361 |
"""Test that .zshrc is not created/modified when oh-my-zsh detected.""" |
| 362 |
zshrc = temp_home / ".zshrc" |
| 363 |
app = App(name="testapp") |
| 364 |
app.install_completion(shell="zsh", add_to_startup=True) |
| 365 |
|
| 366 |
assert not zshrc.exists() |
| 367 |
|
| 368 |
|
| 369 |
def test_install_completion_zsh_ohmyzsh_dir_missing(temp_home, monkeypatch): |
| 370 |
"""Test that $ZSH pointing to nonexistent dir falls back to vanilla path.""" |
| 371 |
monkeypatch.setenv("ZSH", str(temp_home / "nonexistent")) |
| 372 |
|
| 373 |
app = App(name="testapp") |
| 374 |
install_path = app.install_completion(shell="zsh", add_to_startup=False) |
| 375 |
|
| 376 |
expected = temp_home / ".zsh" / "completions" / "_cyclopts_testapp" |
| 377 |
assert install_path == expected |
| 378 |
assert install_path.exists() |
| 379 |
|
| 380 |
|
| 381 |
def test_install_completion_zsh_add_to_startup_prepends(temp_home): |
| 382 |
"""Test that for vanilla zsh, fpath line appears before existing .zshrc content.""" |
| 383 |
app = App(name="testapp") |
| 384 |
zshrc = temp_home / ".zshrc" |
| 385 |
|
| 386 |
existing_content = "# Existing config\nautoload -Uz compinit && compinit\n" |
| 387 |
zshrc.write_text(existing_content) |
| 388 |
|
| 389 |
app.install_completion(shell="zsh", add_to_startup=True) |
| 390 |
|
| 391 |
zshrc_content = zshrc.read_text() |
| 392 |
fpath_pos = zshrc_content.index("fpath=") |
| 393 |
existing_pos = zshrc_content.index("# Existing config") |
| 394 |
assert fpath_pos < existing_pos, "fpath line should be prepended before existing content" |
| 395 |
|
| 396 |
|
| 397 |
def test_install_completion_zsh_ohmyzsh_message(omz_dir, monkeypatch, capsys): |
| 398 |
"""Test that printed output mentions oh-my-zsh and doesn't mention .zshrc.""" |
| 399 |
app = App(name="testapp") |
| 400 |
app.register_install_completion_command(add_to_startup=True) |
| 401 |
|
| 402 |
monkeypatch.setattr("cyclopts.completion.detect.detect_shell", lambda: "zsh") |
| 403 |
|
| 404 |
with patch("sys.exit"): |
| 405 |
try: |
| 406 |
app(["--install-completion"], exit_on_error=False) |
| 407 |
except SystemExit: |
| 408 |
pass |
| 409 |
|
| 410 |
captured = capsys.readouterr() |
| 411 |
assert "oh-my-zsh" in captured.out |
| 412 |
assert ".zshrc" not in captured.out |
| 413 |
assert "exec zsh" in captured.out |
| 414 |
|
| 415 |
|
| 416 |
def test_install_completion_bash_add_to_startup_appends(temp_home): |
| 417 |
"""Regression test: bash still appends to .bashrc.""" |
| 418 |
app = App(name="testapp") |
| 419 |
bashrc = temp_home / ".bashrc" |
| 420 |
|
| 421 |
existing_content = "# Existing bash config\nexport PATH=/usr/local/bin:$PATH\n" |
| 422 |
bashrc.write_text(existing_content) |
| 423 |
|
| 424 |
app.install_completion(shell="bash", add_to_startup=True) |
| 425 |
|
| 426 |
bashrc_content = bashrc.read_text() |
| 427 |
existing_pos = bashrc_content.index("# Existing bash config") |
| 428 |
completion_pos = bashrc_content.index("# Load testapp completion") |
| 429 |
assert existing_pos < completion_pos, "bash completion should be appended after existing content" |