22import subprocess
33import shutil
44import pytest
5- import sys
65
76
87def run_command (cmd , input_text = None ):
@@ -39,29 +38,45 @@ def test_version():
3938 """测试版本号查看"""
4039 returncode , stdout , stderr = run_command ("magic-dash --version" )
4140 assert returncode == 0 , f"命令执行失败: { stderr } "
42- assert stdout .strip (), "无版本号输出"
41+ version = stdout .strip ()
42+ assert version , "无版本号输出"
43+ assert "." in version , f"版本号格式不正确: { version } "
44+
45+
46+ def test_help ():
47+ """测试帮助信息查看"""
48+ returncode , stdout , stderr = run_command ("magic-dash --help" )
49+ assert returncode == 0 , f"命令执行失败: { stderr } "
50+ assert "magic-dash" in stdout .lower (), "帮助信息中未找到程序名"
4351
4452
4553def test_list ():
46- """测试 list 命令列出模板 """
54+ """测试 list 命令列出所有内置模板 """
4755 returncode , stdout , stderr = run_command ("magic-dash list" )
4856 assert returncode == 0 , f"命令执行失败: { stderr } "
49- assert "magic-dash" in stdout , "未找到模板 'magic-dash'"
50- assert "simple-tool" in stdout , "未找到模板 'simple-tool'"
57+
58+ templates = ["magic-dash" , "magic-dash-pro" , "simple-tool" ]
59+ for template in templates :
60+ assert template in stdout , f"未找到模板 '{ template } '"
5161
5262
53- def test_create_with_name (tmp_path ):
63+ @pytest .mark .parametrize ("template_name" , ["simple-tool" , "magic-dash" ])
64+ def test_create_with_name (tmp_path , template_name ):
5465 """测试 create 命令创建项目"""
55- project_path = tmp_path / "simple-tool"
66+ project_path = tmp_path / template_name
5667
5768 input_text = "\n \n "
5869 returncode , stdout , stderr = run_command (
59- f"magic-dash create --name simple-tool --path { tmp_path } " , input_text = input_text
70+ f"magic-dash create --name { template_name } --path { tmp_path } " ,
71+ input_text = input_text ,
6072 )
6173
6274 assert returncode == 0 , f"命令执行失败: { stderr } "
6375 assert os .path .exists (project_path ), f"项目目录未创建: { project_path } "
64- assert os .path .exists (os .path .join (project_path , "app.py" )), "app.py 不存在"
76+
77+ essential_files = ["app.py" , "requirements.txt" ]
78+ for file in essential_files :
79+ assert os .path .exists (os .path .join (project_path , file )), f"{ file } 不存在"
6580
6681
6782def test_create_invalid_name ():
@@ -70,3 +85,15 @@ def test_create_invalid_name():
7085 "magic-dash create --name nonexistent-template"
7186 )
7287 assert returncode != 0 , "应该返回错误但成功了"
88+
89+
90+ def test_create_cancel (tmp_path ):
91+ """测试取消创建项目"""
92+ project_path = tmp_path / "simple-tool"
93+ input_text = "\n n\n "
94+ returncode , stdout , stderr = run_command (
95+ f"magic-dash create --name simple-tool --path { tmp_path } " , input_text = input_text
96+ )
97+
98+ assert returncode == 0 , "取消操作应该正常退出"
99+ assert not os .path .exists (project_path ), "项目不应被创建"
0 commit comments