|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import shutil |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +def run_command(cmd, input_text=None): |
| 8 | + """执行命令并返回结果""" |
| 9 | + if input_text: |
| 10 | + process = subprocess.Popen( |
| 11 | + cmd, |
| 12 | + shell=True, |
| 13 | + stdin=subprocess.PIPE, |
| 14 | + stdout=subprocess.PIPE, |
| 15 | + stderr=subprocess.PIPE, |
| 16 | + text=True, |
| 17 | + ) |
| 18 | + stdout, stderr = process.communicate(input=input_text) |
| 19 | + returncode = process.returncode |
| 20 | + else: |
| 21 | + result = subprocess.run( |
| 22 | + cmd, |
| 23 | + shell=True, |
| 24 | + capture_output=True, |
| 25 | + text=True, |
| 26 | + ) |
| 27 | + returncode = result.returncode |
| 28 | + stdout = result.stdout |
| 29 | + stderr = result.stderr |
| 30 | + return returncode, stdout, stderr |
| 31 | + |
| 32 | + |
| 33 | +def test_version(): |
| 34 | + """测试版本号查看""" |
| 35 | + returncode, stdout, stderr = run_command("magic-dash --version") |
| 36 | + assert returncode == 0, f"命令执行失败: {stderr}" |
| 37 | + assert stdout.strip(), "无版本号输出" |
| 38 | + |
| 39 | + |
| 40 | +def test_list(): |
| 41 | + """测试 list 命令列出模板""" |
| 42 | + returncode, stdout, stderr = run_command("magic-dash list") |
| 43 | + assert returncode == 0, f"命令执行失败: {stderr}" |
| 44 | + assert "magic-dash" in stdout, "未找到模板 'magic-dash'" |
| 45 | + assert "simple-tool" in stdout, "未找到模板 'simple-tool'" |
| 46 | + |
| 47 | + |
| 48 | +def test_create_with_name(tmp_path): |
| 49 | + """测试 create 命令创建项目""" |
| 50 | + project_path = tmp_path / "simple-tool" |
| 51 | + |
| 52 | + input_text = "\n\n" |
| 53 | + returncode, stdout, stderr = run_command( |
| 54 | + f"magic-dash create --name simple-tool --path {tmp_path}", input_text=input_text |
| 55 | + ) |
| 56 | + |
| 57 | + assert returncode == 0, f"命令执行失败: {stderr}" |
| 58 | + assert os.path.exists(project_path), f"项目目录未创建: {project_path}" |
| 59 | + assert os.path.exists(os.path.join(project_path, "app.py")), "app.py 不存在" |
| 60 | + |
| 61 | + |
| 62 | +def test_create_invalid_name(): |
| 63 | + """测试错误处理 - 不存在的模板名称""" |
| 64 | + returncode, stdout, stderr = run_command( |
| 65 | + "magic-dash create --name nonexistent-template" |
| 66 | + ) |
| 67 | + assert returncode != 0, "应该返回错误但成功了" |
| 68 | + assert ( |
| 69 | + "不存在的Dash应用项目模板名称" in stdout |
| 70 | + or "不存在的Dash应用项目模板名称" in (stderr or "") |
| 71 | + ), "未显示期望的错误信息" |
0 commit comments