llmcodegen/tests/test_cli.py

173 lines
5.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pytest
from typer.testing import CliRunner
from unittest.mock import Mock, patch
import sys
from pathlib import Path
from loguru import logger
# 测试 CLI 命令
runner = CliRunner()
def test_cli_init_success():
"""测试 init 命令成功执行"""
from src.llm_codegen.cli import app
with patch('src.llm_codegen.cli.CodeGenerator') as mock_generator:
mock_instance = Mock()
mock_instance.run = Mock()
mock_generator.return_value = mock_instance
test_readme = Path("test_readme.md")
test_readme.write_text("# Test Project\n\nA test project for CLI.")
result = runner.invoke(app, ["init", str(test_readme), "--output", "./test_output"])
test_readme.unlink()
assert result.exit_code == 0
assert "初始化失败" not in result.stdout
mock_generator.assert_called_once()
mock_instance.run.assert_called_once_with(test_readme)
def test_cli_init_failure_no_readme():
"""测试 init 命令当 README 不存在时失败"""
from src.llm_codegen.cli import app
result = runner.invoke(app, ["init", "nonexistent.md"])
assert result.exit_code != 0
def test_cli_enhance_success():
"""测试 enhance 命令成功执行(基于新实现,使用 process_issue"""
from src.llm_codegen.cli import app
# 模拟 CodeGenerator 和 Path.exists仅使 design.json 存在判断通过)
with patch('src.llm_codegen.cli.CodeGenerator') as mock_generator, \
patch('pathlib.Path.exists') as mock_exists:
# 使 design.json 存在检查返回 True
mock_exists.return_value = True
mock_instance = Mock()
mock_instance.process_issue = Mock(return_value=True)
mock_generator.return_value = mock_instance
# 创建临时工单文件
test_issue = Path("test_feature.issue")
issue_content = "name: Add feature\ndescription: Test feature"
test_issue.write_text(issue_content)
result = runner.invoke(app, ["enhance", str(test_issue), "--output", "./test_output"])
test_issue.unlink()
assert result.exit_code == 0
assert "增强失败" not in result.stdout
mock_generator.assert_called_once()
mock_instance.process_issue.assert_called_once_with(issue_content, issue_type="enhance")
def test_cli_fix_success():
"""测试 fix 命令成功执行(基于新实现,使用 process_issue"""
from src.llm_codegen.cli import app
with patch('src.llm_codegen.cli.CodeGenerator') as mock_generator, \
patch('pathlib.Path.exists') as mock_exists:
mock_exists.return_value = True
mock_instance = Mock()
mock_instance.process_issue = Mock(return_value=True)
mock_generator.return_value = mock_instance
test_issue = Path("test_bug.issue")
issue_content = "name: Fix bug\ndescription: Test bug"
test_issue.write_text(issue_content)
result = runner.invoke(app, ["fix", str(test_issue), "--output", "./test_output"])
test_issue.unlink()
assert result.exit_code == 0
assert "修复失败" not in result.stdout
mock_generator.assert_called_once()
mock_instance.process_issue.assert_called_once_with(issue_content, issue_type="fix")
def test_cli_help():
"""测试 CLI 帮助命令"""
from src.llm_codegen.cli import app
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "基于LLM的自动化代码生成与维护工具" in result.stdout
result = runner.invoke(app, ["init", "--help"])
assert result.exit_code == 0
assert "README.md 文件路径" in result.stdout
def test_cli_enhance_no_design():
"""测试 enhance 命令当 design.json 不存在时失败"""
from src.llm_codegen.cli import app
with patch('pathlib.Path.exists') as mock_exists:
mock_exists.return_value = False # design.json 不存在
test_issue = Path("test_feature.issue")
test_issue.write_text("name: Test")
result = runner.invoke(app, ["enhance", str(test_issue)])
test_issue.unlink()
assert result.exit_code != 0
def test_cli_fix_no_design():
"""测试 fix 命令当 design.json 不存在时失败"""
from src.llm_codegen.cli import app
with patch('pathlib.Path.exists') as mock_exists:
mock_exists.return_value = False
test_issue = Path("test_bug.issue")
test_issue.write_text("name: Test")
result = runner.invoke(app, ["fix", str(test_issue)])
test_issue.unlink()
assert result.exit_code != 0
def test_cli_check_success():
"""测试 check 命令成功执行"""
from src.llm_codegen.cli import app
with patch('src.llm_codegen.cli.CodeGenerator') as mock_generator, \
patch('src.llm_codegen.cli.Checker') as mock_checker:
mock_gen_instance = Mock()
mock_generator.return_value = mock_gen_instance
mock_check_instance = Mock()
mock_check_instance.run_full_check_and_fix = Mock(return_value=True)
mock_checker.return_value = mock_check_instance
result = runner.invoke(app, ["check", "--output", "./test_output"])
assert result.exit_code == 0
assert "检查与修复完成" in result.stdout
mock_checker.assert_called_once()
mock_check_instance.run_full_check_and_fix.assert_called_once_with(max_retries=3)
if __name__ == "__main__":
pytest.main([__file__])