Skip to content

Commit 5b7389f

Browse files
committed
test: 新增ci
1 parent 4003997 commit 5b7389f

File tree

5 files changed

+127
-2
lines changed

5 files changed

+127
-2
lines changed

.github/workflows/ci.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main, dev ]
6+
pull_request:
7+
branches: [ main, dev ]
8+
9+
jobs:
10+
test-cli:
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
matrix:
14+
os: [ubuntu-latest, windows-latest, macos-latest]
15+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Set up Python ${{ matrix.python-version }}
21+
uses: actions/setup-python@v5
22+
with:
23+
python-version: ${{ matrix.python-version }}
24+
25+
- name: Install dependencies
26+
run: |
27+
python -m pip install --upgrade pip
28+
pip install -e .
29+
pip install pytest
30+
31+
- name: Run CLI tests with pytest
32+
run: |
33+
python -m pytest tests/test_cli.py -v

magic_dash.egg-info/PKG-INFO

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ magic-dash list
6969

7070
### 2.2 生成指定项目模板
7171

72+
- ⭐ 推荐:交互式选择项目模板(不指定 `--name` 参数时会弹出选择菜单)
73+
74+
```bash
75+
magic-dash create
76+
```
77+
7278
- 默认生成到当前路径
7379

7480
```bash
@@ -81,6 +87,20 @@ magic-dash create --name magic-dash
8187
magic-dash create --name magic-dash --path 目标路径
8288
```
8389

90+
- 项目生成完成后,进入项目目录运行应用
91+
92+
```bash
93+
cd magic-dash
94+
pip install -r requirements.txt
95+
python app.py
96+
```
97+
98+
- 对于 `magic-dash-pro` 模板,需要额外初始化数据库
99+
100+
```bash
101+
python -m models.init_db
102+
```
103+
84104
### 2.3 查看当前`magic-dash`版本
85105

86106
```bash

magic_dash.egg-info/SOURCES.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,5 @@ magic_dash/templates/magic-dash/views/status_pages/_404.py
109109
magic_dash/templates/magic-dash/views/status_pages/_500.py
110110
magic_dash/templates/magic-dash/views/status_pages/__init__.py
111111
magic_dash/templates/simple-tool/app.py
112-
magic_dash/templates/simple-tool/requirements.txt
112+
magic_dash/templates/simple-tool/requirements.txt
113+
tests/test_cli.py

magic_dash/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def _create(name, path):
112112
for template in BUILTIN_TEMPLATES.keys():
113113
console.print(f" - [cyan]{template}[/cyan]")
114114
console.print()
115-
return
115+
raise click.ClickException("无效的模板名称")
116116

117117
# 显示模板信息
118118
template_info = BUILTIN_TEMPLATES[name]

tests/test_cli.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)