summaryrefslogtreecommitdiff
path: root/servers/gitlab_glab/tests
diff options
context:
space:
mode:
authorDawid Rycerz <dawid@rycerz.xyz>2025-03-28 22:12:57 +0100
committerDawid Rycerz <dawid@rycerz.xyz>2025-03-28 22:12:57 +0100
commit73ee5037001cee54f6364f41e1011099308a15a3 (patch)
tree541e932a3462a050cbc30d82d40e8cf2bfb065b0 /servers/gitlab_glab/tests
parent903f0d9ca388533ab44615e414379fa5b305a7d1 (diff)
Fix glab tool context
Diffstat (limited to 'servers/gitlab_glab/tests')
-rw-r--r--servers/gitlab_glab/tests/test_integration.py6
-rw-r--r--servers/gitlab_glab/tests/test_server.py155
2 files changed, 138 insertions, 23 deletions
diff --git a/servers/gitlab_glab/tests/test_integration.py b/servers/gitlab_glab/tests/test_integration.py
index 3bad1cc..b25e715 100644
--- a/servers/gitlab_glab/tests/test_integration.py
+++ b/servers/gitlab_glab/tests/test_integration.py
@@ -24,6 +24,12 @@ class TestIntegration:
# Verify tools were registered
assert mock_server.tool.call_count == 2
+
+ # Verify that the tool decorator was called with functions that have
+ # working_directory parameter. We can't directly access the decorated functions
+ # in the mock, so we'll check indirectly by verifying that the server was
+ # created
+ assert mock_server is not None
@patch("mcp_server_gitlab_glab.server.FastMCP")
def test_create_server_custom_params(self, mock_fastmcp: MagicMock) -> None:
diff --git a/servers/gitlab_glab/tests/test_server.py b/servers/gitlab_glab/tests/test_server.py
index 1c27ea5..e612173 100644
--- a/servers/gitlab_glab/tests/test_server.py
+++ b/servers/gitlab_glab/tests/test_server.py
@@ -27,7 +27,8 @@ class TestGitLabServer:
mock_run.return_value = mock_process
server = GitLabServer()
- success, result = server.execute_glab_command(["--version"])
+ working_dir = "/test/directory"
+ success, result = server.execute_glab_command(["--version"], working_dir)
assert success is True
assert result == "command output"
@@ -36,6 +37,7 @@ class TestGitLabServer:
capture_output=True,
text=True,
check=False,
+ cwd=working_dir,
)
@patch("subprocess.run")
@@ -49,7 +51,8 @@ class TestGitLabServer:
mock_run.return_value = mock_process
server = GitLabServer()
- success, result = server.execute_glab_command(["--version"])
+ working_dir = "/test/directory"
+ success, result = server.execute_glab_command(["--version"], working_dir)
assert success is False
assert result == {"error": "command failed"}
@@ -58,6 +61,7 @@ class TestGitLabServer:
capture_output=True,
text=True,
check=False,
+ cwd=working_dir,
)
@patch("subprocess.run")
@@ -71,7 +75,8 @@ class TestGitLabServer:
mock_run.return_value = mock_process
server = GitLabServer()
- success, result = server.execute_glab_command(["api", "/projects"])
+ working_dir = "/test/directory"
+ success, result = server.execute_glab_command(["api", "/projects"], working_dir)
assert success is False
assert "error" in result
@@ -81,6 +86,7 @@ class TestGitLabServer:
capture_output=True,
text=True,
check=False,
+ cwd=working_dir,
)
@patch("subprocess.run")
@@ -90,7 +96,8 @@ class TestGitLabServer:
mock_run.side_effect = FileNotFoundError("No such file or directory: 'glab'")
server = GitLabServer()
- success, result = server.execute_glab_command(["--version"])
+ working_dir = "/test/directory"
+ success, result = server.execute_glab_command(["--version"], working_dir)
assert success is False
assert "error" in result
@@ -107,7 +114,8 @@ class TestGitLabServer:
mock_run.return_value = mock_process
server = GitLabServer()
- success, result = server.execute_glab_command(["api", "/projects"])
+ working_dir = "/test/directory"
+ success, result = server.execute_glab_command(["api", "/projects"], working_dir)
assert success is True
assert isinstance(result, list)
@@ -119,6 +127,7 @@ class TestGitLabServer:
capture_output=True,
text=True,
check=False,
+ cwd=working_dir,
)
@patch("subprocess.run")
@@ -132,7 +141,8 @@ class TestGitLabServer:
mock_run.return_value = mock_process
server = GitLabServer()
- success, result = server.execute_glab_command(["api", "/projects"])
+ working_dir = "/test/directory"
+ success, result = server.execute_glab_command(["api", "/projects"], working_dir)
assert success is False
assert "error" in result
@@ -145,11 +155,12 @@ class TestGitLabServer:
mock_execute.return_value = (True, "glab version 1.0.0")
server = GitLabServer()
- result = server.check_availability()
+ 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"])
+ 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:
@@ -158,15 +169,16 @@ class TestGitLabServer:
mock_execute.return_value = (False, {"error": "glab command not found"})
server = GitLabServer()
- result = server.check_availability()
+ 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"])
+ 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."""
+ """Test successful find_project with a single project."""
# Mock successful API response with a project
mock_execute.return_value = (
True,
@@ -182,15 +194,76 @@ class TestGitLabServer:
)
server = GitLabServer()
- result = server.find_project("test-project")
+ working_dir = "/test/directory"
+ result = server.find_project("test-project", working_dir)
- assert "id" in result
- assert result["id"] == 1
- assert result["name"] == "test-project"
- assert result["path_with_namespace"] == "group/test-project"
- assert result["web_url"] == "https://gitlab.com/group/test-project"
- assert result["description"] == "A test project"
- mock_execute.assert_called_once_with(["api", "/projects?search=test-project"])
+ 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:
@@ -199,12 +272,13 @@ class TestGitLabServer:
mock_execute.return_value = (True, [])
server = GitLabServer()
- result = server.find_project("nonexistent-project")
+ 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"]
+ ["api", "/projects?search=nonexistent-project"], working_dir
)
@patch.object(GitLabServer, "execute_glab_command")
@@ -214,8 +288,43 @@ class TestGitLabServer:
mock_execute.return_value = (False, {"error": "API error"})
server = GitLabServer()
- result = server.find_project("test-project")
+ 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"])
+ 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)