fix: resolve CI/CD issues - all tests pass locally

This commit is contained in:
Developer
2026-02-05 13:32:25 +00:00
parent 155bc36ded
commit 1da735b646
30 changed files with 3982 additions and 605 deletions

View File

@@ -1,203 +1,143 @@
"""Test configuration and fixtures."""
"""Test configuration and fixtures for MCP Server CLI."""
import os
import sys
import tempfile
from pathlib import Path
from typing import Generator
import pytest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
@pytest.fixture
def create_python_project(tmp_path: Path) -> Path:
"""Create a Python project structure for testing."""
src_dir = tmp_path / "src"
src_dir.mkdir()
(src_dir / "__init__.py").write_text('"""Package init."""')
(src_dir / "main.py").write_text('''"""Main module."""
def hello():
"""Say hello."""
print("Hello, World!")
class Calculator:
"""A simple calculator."""
def add(self, a: int, b: int) -> int:
"""Add two numbers."""
return a + b
def multiply(self, a: int, b: int) -> int:
"""Multiply two numbers."""
return a * b
''')
(tmp_path / "requirements.txt").write_text('''requests>=2.31.0
click>=8.0.0
pytest>=7.0.0
''')
(tmp_path / "pyproject.toml").write_text('''[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[project]
name = "test-project"
version = "0.1.0"
description = "A test project"
requires-python = ">=3.9"
dependencies = [
"requests>=2.31.0",
"click>=8.0.0",
]
''')
return tmp_path
@pytest.fixture
def create_javascript_project(tmp_path: Path) -> Path:
"""Create a JavaScript project structure for testing."""
(tmp_path / "package.json").write_text('''{
"name": "test-js-project",
"version": "1.0.0",
"description": "A test JavaScript project",
"main": "index.js",
"dependencies": {
"express": "^4.18.0",
"lodash": "^4.17.0"
},
"devDependencies": {
"jest": "^29.0.0"
}
}
''')
(tmp_path / "index.js").write_text('''const express = require('express');
const _ = require('lodash');
function hello() {
return 'Hello, World!';
}
class Calculator {
add(a, b) {
return a + b;
}
}
module.exports = { hello, Calculator };
''')
return tmp_path
@pytest.fixture
def create_go_project(tmp_path: Path) -> Path:
"""Create a Go project structure for testing."""
(tmp_path / "go.mod").write_text('''module test-go-project
go 1.21
require (
github.com/gin-gonic/gin v1.9.0
github.com/stretchr/testify v1.8.0
from mcp_server_cli.config import AppConfig, ConfigManager
from mcp_server_cli.models import (
LocalLLMConfig,
SecurityConfig,
ServerConfig,
)
from mcp_server_cli.server import MCPServer
from mcp_server_cli.tools import (
FileTools,
GitTools,
ShellTools,
ToolRegistry,
)
''')
(tmp_path / "main.go").write_text('''package main
import "fmt"
func hello() string {
return "Hello, World!"
}
type Calculator struct{}
func (c *Calculator) Add(a, b int) int {
return a + b
}
func main() {
fmt.Println(hello())
}
''')
return tmp_path
@pytest.fixture
def create_rust_project(tmp_path: Path) -> Path:
"""Create a Rust project structure for testing."""
src_dir = tmp_path / "src"
src_dir.mkdir()
def temp_dir() -> Generator[Path, None, None]:
"""Create a temporary directory for tests."""
with tempfile.TemporaryDirectory() as tmpdir:
yield Path(tmpdir)
(tmp_path / "Cargo.toml").write_text('''[package]
name = "test-rust-project"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.0", features = "full" }
@pytest.fixture
def temp_file(temp_dir: Path) -> Generator[Path, None, None]:
"""Create a temporary file for tests."""
file_path = temp_dir / "test_file.txt"
file_path.write_text("Hello, World!")
yield file_path
[dev-dependencies]
assertions = "0.1"
''')
(src_dir / "main.rs").write_text('''fn hello() -> String {
"Hello, World!".to_string()
}
@pytest.fixture
def temp_yaml_file(temp_dir: Path) -> Generator[Path, None, None]:
"""Create a temporary YAML file for tests."""
file_path = temp_dir / "test_config.yaml"
content = """
server:
host: "127.0.0.1"
port: 8080
log_level: "DEBUG"
pub struct Calculator;
llm:
enabled: false
base_url: "http://localhost:11434"
model: "llama2"
impl Calculator {
pub fn add(a: i32, b: i32) -> i32 {
a + b
security:
allowed_commands:
- ls
- cat
- echo
blocked_paths:
- /etc
- /root
"""
file_path.write_text(content)
yield file_path
@pytest.fixture
def sample_tool_definition() -> dict:
"""Sample tool definition for testing."""
return {
"name": "test_tool",
"description": "A test tool",
"input_schema": {
"type": "object",
"properties": {
"param1": {
"type": "string",
"description": "First parameter",
"required": True
},
"param2": {
"type": "integer",
"description": "Second parameter",
"default": 10
}
},
"required": ["param1"]
}
}
}
fn main() {
println!("{}", hello());
}
''')
return tmp_path
@pytest.fixture
def create_mixed_project(tmp_path: Path) -> Path:
"""Create a project with multiple languages for testing."""
python_part = tmp_path / "python_part"
python_part.mkdir()
js_part = tmp_path / "js_part"
js_part.mkdir()
def default_config() -> AppConfig:
"""Create a default server configuration."""
return AppConfig(
server=ServerConfig(host="127.0.0.1", port=3000, log_level="INFO"),
llm=LocalLLMConfig(enabled=False, base_url="http://localhost:11434"),
security=SecurityConfig(
allowed_commands=["ls", "cat", "echo"],
blocked_paths=["/etc", "/root"],
),
)
src_dir = python_part / "src"
src_dir.mkdir()
(src_dir / "__init__.py").write_text('"""Package init."""')
(src_dir / "main.py").write_text('''"""Main module."""
def hello():
print("Hello")
''')
(python_part / "pyproject.toml").write_text('''[project]
name = "test-project"
version = "0.1.0"
description = "A test project"
requires-python = ">=3.9"
dependencies = ["requests>=2.31.0"]
''')
(js_part / "package.json").write_text('''{
"name": "test-js-project",
"version": "1.0.0",
"description": "A test JavaScript project"
}
''')
(js_part / "index.js").write_text('''function hello() {
return 'Hello';
}
module.exports = { hello };
''')
@pytest.fixture
def mcp_server(default_config: AppConfig) -> MCPServer:
"""Create an MCP server instance with registered tools."""
server = MCPServer(config=default_config)
server.register_tool(FileTools())
server.register_tool(GitTools())
server.register_tool(ShellTools())
return server
return tmp_path
@pytest.fixture
def tool_registry() -> ToolRegistry:
"""Create a tool registry instance."""
return ToolRegistry()
@pytest.fixture
def config_manager() -> ConfigManager:
"""Create a configuration manager instance."""
return ConfigManager()
@pytest.fixture
def mock_env():
"""Mock environment variables."""
env = {
"MCP_PORT": "4000",
"MCP_HOST": "0.0.0.0",
"MCP_LOG_LEVEL": "DEBUG",
}
original_env = os.environ.copy()
os.environ.update(env)
yield
os.environ.clear()
os.environ.update(original_env)