| 1 |
"""Tests for shell detection functionality.""" |
| 2 |
|
| 3 |
import subprocess |
| 4 |
from unittest.mock import Mock |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from cyclopts.completion import ShellDetectionError, detect_shell |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture |
| 12 |
def clean_shell_env(monkeypatch): |
| 13 |
"""Remove all shell version environment variables to ensure clean test environment.""" |
| 14 |
monkeypatch.delenv("ZSH_VERSION", raising=False) |
| 15 |
monkeypatch.delenv("BASH_VERSION", raising=False) |
| 16 |
monkeypatch.delenv("FISH_VERSION", raising=False) |
| 17 |
|
| 18 |
|
| 19 |
@pytest.mark.parametrize( |
| 20 |
("env_var", "version", "expected"), |
| 21 |
[ |
| 22 |
("ZSH_VERSION", "5.8", "zsh"), |
| 23 |
("BASH_VERSION", "5.0.0", "bash"), |
| 24 |
("FISH_VERSION", "3.3.0", "fish"), |
| 25 |
], |
| 26 |
) |
| 27 |
def test_detect_shell_via_version_variable(monkeypatch, env_var, version, expected): |
| 28 |
"""Test detection of shell via version environment variables.""" |
| 29 |
monkeypatch.setenv(env_var, version) |
| 30 |
assert detect_shell() == expected |
| 31 |
|
| 32 |
|
| 33 |
@pytest.mark.parametrize( |
| 34 |
("primary_var", "primary_version", "secondary_var", "secondary_version", "expected"), |
| 35 |
[ |
| 36 |
("ZSH_VERSION", "5.8", "BASH_VERSION", "5.0.0", "zsh"), |
| 37 |
("BASH_VERSION", "5.0.0", "FISH_VERSION", "3.3.0", "bash"), |
| 38 |
], |
| 39 |
) |
| 40 |
def test_version_variable_priority( |
| 41 |
monkeypatch, primary_var, primary_version, secondary_var, secondary_version, expected |
| 42 |
): |
| 43 |
"""Test that higher priority version variables take precedence.""" |
| 44 |
monkeypatch.setenv(primary_var, primary_version) |
| 45 |
monkeypatch.setenv(secondary_var, secondary_version) |
| 46 |
assert detect_shell() == expected |
| 47 |
|
| 48 |
|
| 49 |
def _mock_failed_subprocess(monkeypatch): |
| 50 |
"""Helper to mock failed subprocess for testing fallback behavior.""" |
| 51 |
mock_result = Mock() |
| 52 |
mock_result.returncode = 1 |
| 53 |
mock_result.stdout = "" |
| 54 |
monkeypatch.setattr(subprocess, "run", lambda *args, **kwargs: mock_result) |
| 55 |
|
| 56 |
|
| 57 |
def test_detect_shell_no_detection_methods_available(clean_shell_env, monkeypatch): |
| 58 |
"""Test that ShellDetectionError is raised when no detection methods succeed.""" |
| 59 |
monkeypatch.delenv("SHELL", raising=False) |
| 60 |
_mock_failed_subprocess(monkeypatch) |
| 61 |
|
| 62 |
with pytest.raises(ShellDetectionError) as exc_info: |
| 63 |
detect_shell() |
| 64 |
|
| 65 |
assert "Unable to detect shell type" in str(exc_info.value) |
| 66 |
|
| 67 |
|
| 68 |
def test_detect_shell_empty_string_not_detected(monkeypatch): |
| 69 |
"""Test that empty string environment variables don't trigger detection.""" |
| 70 |
monkeypatch.setenv("ZSH_VERSION", "") |
| 71 |
monkeypatch.setenv("BASH_VERSION", "") |
| 72 |
monkeypatch.setenv("FISH_VERSION", "") |
| 73 |
monkeypatch.delenv("SHELL", raising=False) |
| 74 |
_mock_failed_subprocess(monkeypatch) |
| 75 |
|
| 76 |
with pytest.raises(ShellDetectionError): |
| 77 |
detect_shell() |
| 78 |
|
| 79 |
|
| 80 |
@pytest.mark.parametrize( |
| 81 |
("shell_path", "expected"), |
| 82 |
[ |
| 83 |
("/bin/zsh", "zsh"), |
| 84 |
("/usr/bin/bash", "bash"), |
| 85 |
("/usr/local/bin/fish", "fish"), |
| 86 |
("/bin/ZSH", "zsh"), |
| 87 |
("/opt/homebrew/bin/fish", "fish"), |
| 88 |
], |
| 89 |
) |
| 90 |
def test_detect_shell_fallback_to_shell_variable(clean_shell_env, monkeypatch, shell_path, expected): |
| 91 |
"""Test fallback to SHELL variable for various paths.""" |
| 92 |
monkeypatch.setenv("SHELL", shell_path) |
| 93 |
_mock_failed_subprocess(monkeypatch) |
| 94 |
|
| 95 |
assert detect_shell() == expected |
| 96 |
|
| 97 |
|
| 98 |
def test_version_variable_takes_priority_over_shell(monkeypatch): |
| 99 |
"""Test that version variables take priority over SHELL variable.""" |
| 100 |
monkeypatch.setenv("BASH_VERSION", "5.0.0") |
| 101 |
monkeypatch.setenv("SHELL", "/bin/zsh") |
| 102 |
|
| 103 |
assert detect_shell() == "bash" |
| 104 |
|
| 105 |
|
| 106 |
def test_detect_shell_fallback_unsupported_shell(clean_shell_env, monkeypatch): |
| 107 |
"""Test that unsupported shells in SHELL variable raise error.""" |
| 108 |
monkeypatch.setenv("SHELL", "/bin/ksh") |
| 109 |
_mock_failed_subprocess(monkeypatch) |
| 110 |
|
| 111 |
with pytest.raises(ShellDetectionError): |
| 112 |
detect_shell() |
| 113 |
|
| 114 |
|
| 115 |
@pytest.mark.parametrize( |
| 116 |
("parent_process_name", "expected"), |
| 117 |
[ |
| 118 |
("zsh", "zsh"), |
| 119 |
("/bin/bash", "bash"), |
| 120 |
("/usr/local/bin/fish", "fish"), |
| 121 |
("-zsh", "zsh"), |
| 122 |
("login_bash", "bash"), |
| 123 |
], |
| 124 |
) |
| 125 |
def test_detect_shell_via_parent_process(clean_shell_env, monkeypatch, parent_process_name, expected): |
| 126 |
"""Test detection via parent process when version variables are not available.""" |
| 127 |
monkeypatch.delenv("SHELL", raising=False) |
| 128 |
|
| 129 |
mock_result = Mock() |
| 130 |
mock_result.returncode = 0 |
| 131 |
mock_result.stdout = parent_process_name |
| 132 |
monkeypatch.setattr(subprocess, "run", lambda *args, **kwargs: mock_result) |
| 133 |
|
| 134 |
assert detect_shell() == expected |
| 135 |
|
| 136 |
|
| 137 |
def test_detect_shell_subprocess_timeout_falls_back_to_shell_var(clean_shell_env, monkeypatch): |
| 138 |
"""Test that subprocess timeout falls back to SHELL variable.""" |
| 139 |
monkeypatch.setenv("SHELL", "/bin/zsh") |
| 140 |
|
| 141 |
def mock_subprocess_run(*args, **kwargs): |
| 142 |
raise subprocess.TimeoutExpired(cmd=args[0], timeout=1) |
| 143 |
|
| 144 |
monkeypatch.setattr(subprocess, "run", mock_subprocess_run) |
| 145 |
|
| 146 |
assert detect_shell() == "zsh" |
| 147 |
|
| 148 |
|
| 149 |
def test_detect_shell_subprocess_error_falls_back_to_shell_var(clean_shell_env, monkeypatch): |
| 150 |
"""Test that subprocess errors fall back to SHELL variable.""" |
| 151 |
monkeypatch.setenv("SHELL", "/bin/bash") |
| 152 |
|
| 153 |
def mock_subprocess_run(*args, **kwargs): |
| 154 |
raise FileNotFoundError("ps command not found") |
| 155 |
|
| 156 |
monkeypatch.setattr(subprocess, "run", mock_subprocess_run) |
| 157 |
|
| 158 |
assert detect_shell() == "bash" |
| 159 |
|
| 160 |
|
| 161 |
def test_detect_shell_subprocess_returns_unsupported_shell_falls_back(clean_shell_env, monkeypatch): |
| 162 |
"""Test that unsupported parent process name falls back to SHELL variable.""" |
| 163 |
monkeypatch.setenv("SHELL", "/bin/fish") |
| 164 |
|
| 165 |
mock_result = Mock() |
| 166 |
mock_result.returncode = 0 |
| 167 |
mock_result.stdout = "ksh" |
| 168 |
monkeypatch.setattr(subprocess, "run", lambda *args, **kwargs: mock_result) |
| 169 |
|
| 170 |
assert detect_shell() == "fish" |