-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_launchers.py
More file actions
200 lines (145 loc) · 7.09 KB
/
Copy pathtest_launchers.py
File metadata and controls
200 lines (145 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""Tests for launcher operations."""
import subprocess
from unittest.mock import Mock
import pytest
from wt.launchers import (
LauncherError,
launch_ide,
launch_terminal,
launch_claude,
_launch_iterm2,
_command_exists,
)
class TestCommandExists:
"""Tests for _command_exists helper function."""
def test_command_exists(self, mocker):
"""Test when command exists."""
mocker.patch("subprocess.run", return_value=Mock(returncode=0))
assert _command_exists("ls") is True
def test_command_does_not_exist(self, mocker):
"""Test when command doesn't exist."""
mocker.patch(
"subprocess.run", side_effect=subprocess.CalledProcessError(1, "which")
)
assert _command_exists("nonexistent") is False
class TestLaunchIDE:
"""Tests for launch_ide function."""
def test_launch_ide_with_executable(self, mocker, tmp_path):
"""Test launching IDE with specified executable."""
mock_exists = mocker.patch("wt.launchers._command_exists", return_value=True)
mock_run = mocker.patch("subprocess.run", return_value=Mock(returncode=0))
mock_print = mocker.patch("builtins.print")
launch_ide(tmp_path, "code")
mock_exists.assert_called_once_with("code")
mock_run.assert_called_once()
args = mock_run.call_args[0][0]
assert args == ["code", str(tmp_path)]
mock_print.assert_called_once()
def test_launch_ide_auto_detect(self, mocker, tmp_path):
"""Test launching IDE with auto-detection."""
def command_exists_side_effect(cmd):
return cmd == "cursor"
mocker.patch(
"wt.launchers._command_exists", side_effect=command_exists_side_effect
)
mock_run = mocker.patch("subprocess.run", return_value=Mock(returncode=0))
mocker.patch("builtins.print")
launch_ide(tmp_path, None)
# Should have found and used 'cursor'
args = mock_run.call_args[0][0]
assert args[0] == "cursor"
def test_launch_ide_no_default_found(self, mocker, tmp_path):
"""Test launching IDE when no default is found."""
mocker.patch("wt.launchers._command_exists", return_value=False)
with pytest.raises(LauncherError, match="No IDE specified"):
launch_ide(tmp_path, None)
def test_launch_ide_not_found(self, mocker, tmp_path):
"""Test launching IDE when executable doesn't exist."""
mocker.patch("wt.launchers._command_exists", return_value=False)
with pytest.raises(LauncherError, match="not found"):
launch_ide(tmp_path, "nonexistent")
def test_launch_ide_failure(self, mocker, tmp_path):
"""Test launching IDE when command fails."""
mocker.patch("wt.launchers._command_exists", return_value=True)
mocker.patch(
"subprocess.run", side_effect=subprocess.CalledProcessError(1, "code")
)
with pytest.raises(LauncherError, match="Failed to launch IDE"):
launch_ide(tmp_path, "code")
class TestLaunchTerminal:
"""Tests for launch_terminal function."""
def test_launch_terminal_macos(self, mocker, tmp_path):
"""Test launching terminal on macOS."""
mocker.patch("platform.system", return_value="Darwin")
mock_launch_iterm2 = mocker.patch("wt.launchers._launch_iterm2")
launch_terminal(tmp_path)
mock_launch_iterm2.assert_called_once_with(tmp_path)
def test_launch_terminal_linux(self, mocker, tmp_path):
"""Test launching terminal on Linux."""
mocker.patch("platform.system", return_value="Linux")
mock_launch_linux = mocker.patch("wt.launchers._launch_linux_terminal")
launch_terminal(tmp_path)
mock_launch_linux.assert_called_once_with(tmp_path)
def test_launch_terminal_unsupported_platform(self, mocker, tmp_path):
"""Test launching terminal on unsupported platform."""
mocker.patch("platform.system", return_value="Windows")
with pytest.raises(LauncherError, match="not supported on Windows"):
launch_terminal(tmp_path)
class TestLaunchITerm2:
"""Tests for _launch_iterm2 function."""
def test_launch_iterm2_success(self, mocker, tmp_path):
"""Test launching iTerm2 successfully."""
mock_run = mocker.patch("subprocess.run", return_value=Mock(returncode=0))
mock_print = mocker.patch("builtins.print")
_launch_iterm2(tmp_path)
mock_run.assert_called_once()
args = mock_run.call_args[0][0]
assert args[0] == "osascript"
assert str(tmp_path) in args[2]
mock_print.assert_called_once()
def test_launch_iterm2_with_command(self, mocker, tmp_path):
"""Test launching iTerm2 with a command."""
mock_run = mocker.patch("subprocess.run", return_value=Mock(returncode=0))
mock_print = mocker.patch("builtins.print")
_launch_iterm2(tmp_path, command="claude")
mock_run.assert_called_once()
args = mock_run.call_args[0][0]
assert args[0] == "osascript"
assert str(tmp_path) in args[2]
assert "claude" in args[2]
mock_print.assert_called_once()
def test_launch_iterm2_failure(self, mocker, tmp_path):
"""Test launching iTerm2 when command fails."""
mocker.patch(
"subprocess.run",
side_effect=subprocess.CalledProcessError(1, "osascript", stderr="error"),
)
with pytest.raises(LauncherError, match="Failed to launch iTerm2"):
_launch_iterm2(tmp_path)
class TestLaunchClaude:
"""Tests for launch_claude function."""
def test_launch_claude_macos(self, mocker, tmp_path):
"""Test launching Claude on macOS."""
mocker.patch("wt.launchers._command_exists", return_value=True)
mocker.patch("platform.system", return_value="Darwin")
mock_launch_iterm2 = mocker.patch("wt.launchers._launch_iterm2")
launch_claude(tmp_path)
mock_launch_iterm2.assert_called_once_with(tmp_path, command="claude")
def test_launch_claude_linux(self, mocker, tmp_path):
"""Test launching Claude on Linux."""
mocker.patch("wt.launchers._command_exists", return_value=True)
mocker.patch("platform.system", return_value="Linux")
mock_launch_linux = mocker.patch("wt.launchers._launch_linux_terminal")
launch_claude(tmp_path)
mock_launch_linux.assert_called_once_with(tmp_path, command="claude")
def test_launch_claude_not_installed(self, mocker, tmp_path):
"""Test launching Claude when CLI is not installed."""
mocker.patch("wt.launchers._command_exists", return_value=False)
with pytest.raises(LauncherError, match="Claude CLI not found"):
launch_claude(tmp_path)
def test_launch_claude_unsupported_platform(self, mocker, tmp_path):
"""Test launching Claude on unsupported platform."""
mocker.patch("wt.launchers._command_exists", return_value=True)
mocker.patch("platform.system", return_value="Windows")
with pytest.raises(LauncherError, match="not supported on Windows"):
launch_claude(tmp_path)