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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
#!/usr/bin/env python3
"""Tests for the GitLab CLI MCP server implementation."""
import json
from unittest.mock import MagicMock, patch
from mcp_server_gitlab_glab.server import GitLabServer
class TestGitLabServer:
"""Tests for the GitLabServer class."""
def test_init(self) -> None:
"""Test initialization of GitLabServer."""
server = GitLabServer()
assert hasattr(server, "auth_message")
assert "glab auth login" in server.auth_message
@patch("subprocess.run")
def test_execute_glab_command_success(self, mock_run: MagicMock) -> None:
"""Test successful execution of a glab command."""
# Mock successful command execution
mock_process = MagicMock()
mock_process.returncode = 0
mock_process.stdout = "command output"
mock_process.stderr = ""
mock_run.return_value = mock_process
server = GitLabServer()
working_dir = "/test/directory"
success, result = server.execute_glab_command(["--version"], working_dir)
assert success is True
assert result == "command output"
mock_run.assert_called_once_with(
["glab", "--version"],
capture_output=True,
text=True,
check=False,
cwd=working_dir,
)
@patch("subprocess.run")
def test_execute_glab_command_failure(self, mock_run: MagicMock) -> None:
"""Test failed execution of a glab command."""
# Mock failed command execution
mock_process = MagicMock()
mock_process.returncode = 1
mock_process.stdout = ""
mock_process.stderr = "command failed"
mock_run.return_value = mock_process
server = GitLabServer()
working_dir = "/test/directory"
success, result = server.execute_glab_command(["--version"], working_dir)
assert success is False
assert result == {"error": "command failed"}
mock_run.assert_called_once_with(
["glab", "--version"],
capture_output=True,
text=True,
check=False,
cwd=working_dir,
)
@patch("subprocess.run")
def test_execute_glab_command_auth_error(self, mock_run: MagicMock) -> None:
"""Test authentication error during glab command execution."""
# Mock authentication error
mock_process = MagicMock()
mock_process.returncode = 1
mock_process.stdout = ""
mock_process.stderr = "authentication required"
mock_run.return_value = mock_process
server = GitLabServer()
working_dir = "/test/directory"
success, result = server.execute_glab_command(["api", "/projects"], working_dir)
assert success is False
assert "error" in result
assert "auth login" in result["error"]
mock_run.assert_called_once_with(
["glab", "api", "/projects"],
capture_output=True,
text=True,
check=False,
cwd=working_dir,
)
@patch("subprocess.run")
def test_execute_glab_command_not_found(self, mock_run: MagicMock) -> None:
"""Test glab command not found error."""
# Mock FileNotFoundError
mock_run.side_effect = FileNotFoundError("No such file or directory: 'glab'")
server = GitLabServer()
working_dir = "/test/directory"
success, result = server.execute_glab_command(["--version"], working_dir)
assert success is False
assert "error" in result
assert "glab command not found" in result["error"]
@patch("subprocess.run")
def test_execute_glab_api_command_success(self, mock_run: MagicMock) -> None:
"""Test successful execution of a glab api command with JSON response."""
# Mock successful API command execution with JSON response
mock_process = MagicMock()
mock_process.returncode = 0
mock_process.stdout = json.dumps([{"id": 1, "name": "test-project"}])
mock_process.stderr = ""
mock_run.return_value = mock_process
server = GitLabServer()
working_dir = "/test/directory"
success, result = server.execute_glab_command(["api", "/projects"], working_dir)
assert success is True
assert isinstance(result, list)
assert len(result) == 1
assert result[0]["id"] == 1
assert result[0]["name"] == "test-project"
mock_run.assert_called_once_with(
["glab", "api", "/projects"],
capture_output=True,
text=True,
check=False,
cwd=working_dir,
)
@patch("subprocess.run")
def test_execute_glab_api_command_invalid_json(self, mock_run: MagicMock) -> None:
"""Test glab api command with invalid JSON response."""
# Mock API command execution with invalid JSON response
mock_process = MagicMock()
mock_process.returncode = 0
mock_process.stdout = "invalid json"
mock_process.stderr = ""
mock_run.return_value = mock_process
server = GitLabServer()
working_dir = "/test/directory"
success, result = server.execute_glab_command(["api", "/projects"], working_dir)
assert success is False
assert "error" in result
assert "Failed to parse JSON response" in result["error"]
@patch.object(GitLabServer, "execute_glab_command")
def test_check_availability_success(self, mock_execute: MagicMock) -> None:
"""Test successful check_availability."""
# Mock successful command execution
mock_execute.return_value = (True, "glab version 1.0.0")
server = GitLabServer()
working_dir = "/test/directory"
result = server.check_availability(working_dir)
assert result["available"] is True
assert result["version"] == "glab version 1.0.0"
mock_execute.assert_called_once_with(["--version"], working_dir)
@patch.object(GitLabServer, "execute_glab_command")
def test_check_availability_failure(self, mock_execute: MagicMock) -> None:
"""Test failed check_availability."""
# Mock failed command execution
mock_execute.return_value = (False, {"error": "glab command not found"})
server = GitLabServer()
working_dir = "/test/directory"
result = server.check_availability(working_dir)
assert result["available"] is False
assert result["error"] == "glab command not found"
mock_execute.assert_called_once_with(["--version"], working_dir)
@patch.object(GitLabServer, "execute_glab_command")
def test_find_project_success(self, mock_execute: MagicMock) -> None:
"""Test successful find_project with a single project."""
# Mock successful API response with a project
mock_execute.return_value = (
True,
[
{
"id": 1,
"name": "test-project",
"path_with_namespace": "group/test-project",
"web_url": "https://gitlab.com/group/test-project",
"description": "A test project",
}
],
)
server = GitLabServer()
working_dir = "/test/directory"
result = server.find_project("test-project", working_dir)
assert isinstance(result, list)
assert len(result) == 1
assert result[0]["id"] == 1
assert result[0]["name"] == "test-project"
assert result[0]["path_with_namespace"] == "group/test-project"
assert result[0]["web_url"] == "https://gitlab.com/group/test-project"
assert result[0]["description"] == "A test project"
mock_execute.assert_called_once_with(
["api", "/projects?search=test-project"], working_dir
)
@patch.object(GitLabServer, "execute_glab_command")
def test_find_project_multiple_results(self, mock_execute: MagicMock) -> None:
"""Test successful find_project with multiple projects."""
# Mock successful API response with multiple projects
mock_execute.return_value = (
True,
[
{
"id": 1,
"name": "test-project",
"path_with_namespace": "group/test-project",
"web_url": "https://gitlab.com/group/test-project",
"description": "A test project",
},
{
"id": 2,
"name": "test-project-2",
"path_with_namespace": "group/test-project-2",
"web_url": "https://gitlab.com/group/test-project-2",
"description": "Another test project",
},
{
"id": 3,
"name": "test-project-3",
"path_with_namespace": "group/test-project-3",
"web_url": "https://gitlab.com/group/test-project-3",
"description": "Yet another test project",
},
],
)
server = GitLabServer()
working_dir = "/test/directory"
result = server.find_project("test-project", working_dir)
assert isinstance(result, list)
assert len(result) == 3
# Check first project
assert result[0]["id"] == 1
assert result[0]["name"] == "test-project"
assert result[0]["path_with_namespace"] == "group/test-project"
# Check second project
assert result[1]["id"] == 2
assert result[1]["name"] == "test-project-2"
assert result[1]["path_with_namespace"] == "group/test-project-2"
# Check third project
assert result[2]["id"] == 3
assert result[2]["name"] == "test-project-3"
assert result[2]["path_with_namespace"] == "group/test-project-3"
mock_execute.assert_called_once_with(
["api", "/projects?search=test-project"], working_dir
)
@patch.object(GitLabServer, "execute_glab_command")
def test_find_project_not_found(self, mock_execute: MagicMock) -> None:
"""Test find_project with no results."""
# Mock API response with no projects
mock_execute.return_value = (True, [])
server = GitLabServer()
working_dir = "/test/directory"
result = server.find_project("nonexistent-project", working_dir)
assert "error" in result
assert "not found" in result["error"]
mock_execute.assert_called_once_with(
["api", "/projects?search=nonexistent-project"], working_dir
)
@patch.object(GitLabServer, "execute_glab_command")
def test_find_project_api_error(self, mock_execute: MagicMock) -> None:
"""Test find_project with API error."""
# Mock API error
mock_execute.return_value = (False, {"error": "API error"})
server = GitLabServer()
working_dir = "/test/directory"
result = server.find_project("test-project", working_dir)
assert "error" in result
assert result["error"] == "API error"
mock_execute.assert_called_once_with(
["api", "/projects?search=test-project"], working_dir
)
@patch("subprocess.run")
def test_working_directory_is_used(self, mock_run: MagicMock) -> None:
"""Test that the working directory is correctly passed to subprocess.run."""
# Mock successful command execution
mock_process = MagicMock()
mock_process.returncode = 0
mock_process.stdout = "command output"
mock_process.stderr = ""
mock_run.return_value = mock_process
server = GitLabServer()
# Test with different working directories
working_dirs = [
"/home/user/project",
"/tmp/gitlab",
"/var/www/html",
]
for working_dir in working_dirs:
server.execute_glab_command(["status"], working_dir)
mock_run.assert_called_with(
["glab", "status"],
capture_output=True,
text=True,
check=False,
cwd=working_dir,
)
# Verify the number of calls
assert mock_run.call_count == len(working_dirs)
|