Skip to content

Commit d34c805

Browse files
committed
fix: harden binary release smoke test
1 parent 5045786 commit d34c805

4 files changed

Lines changed: 65 additions & 6 deletions

File tree

main.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# 导入 cleanup 模块以注册 atexit 钩子
1818
import src.utils.cleanup
1919
# 从新的配置模块导入 settings 实例
20-
from src.utils.config import get_settings
20+
from src.utils.config import get_settings, resolve_app_root
2121
# 导入日志管理器,以便调用其清理函数
2222
import src.utils.log_manager
2323
# 导入UI工具
@@ -137,6 +137,24 @@ def initialize_dependencies():
137137

138138
console.print("[dim]依赖项初始化完成。[/dim]")
139139

140+
def run_smoke_test() -> int:
141+
"""执行非交互式启动自检。"""
142+
settings = get_settings()
143+
console.print(f"PyRAG-Kit {VERSION} smoke test ok")
144+
console.print(f"app_root={resolve_app_root()}")
145+
console.print(f"log_path={settings.log_path}")
146+
return 0
147+
148+
149+
def run_cli(argv: list[str] | None = None) -> int:
150+
"""解析启动参数并执行对应入口。"""
151+
args = list(sys.argv[1:] if argv is None else argv)
152+
if "--smoke-test" in args:
153+
return run_smoke_test()
154+
main()
155+
return 0
156+
157+
140158
def main():
141159
"""程序主入口,提供交互式菜单并实现延迟加载。"""
142160
display_banner()
@@ -177,4 +195,4 @@ def main():
177195
console.print("[bold red]请检查错误信息并重试。[/bold red]")
178196

179197
if __name__ == "__main__":
180-
main()
198+
sys.exit(run_cli())

scripts/build_binary_release.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,16 @@ def executable_path(bundle_root: Path) -> Path:
149149

150150
def validate_bundle(bundle_root: Path) -> None:
151151
executable = executable_path(bundle_root)
152-
subprocess.run(
153-
[str(executable)],
152+
result = subprocess.run(
153+
[str(executable), "--smoke-test"],
154154
cwd=bundle_root,
155-
input="4\n",
156155
text=True,
157156
capture_output=True,
158157
check=True,
159158
timeout=30,
160159
)
160+
if "smoke test ok" not in result.stdout:
161+
raise RuntimeError("发布包自检输出不符合预期。")
161162

162163

163164
def main() -> None:

tests/test_main_entry.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
import main
3+
4+
5+
def test_run_cli_uses_smoke_test(monkeypatch):
6+
called = {"count": 0}
7+
8+
def fake_run_smoke_test():
9+
called["count"] += 1
10+
return 0
11+
12+
monkeypatch.setattr(main, "run_smoke_test", fake_run_smoke_test)
13+
14+
result = main.run_cli(["--smoke-test"])
15+
16+
assert result == 0
17+
assert called["count"] == 1

tests/test_release_scripts.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# -*- coding: utf-8 -*-
2+
import subprocess
23
from pathlib import Path
34

4-
from scripts.build_binary_release import prepare_runtime_layout
5+
from scripts.build_binary_release import prepare_runtime_layout, validate_bundle
56
from scripts.extract_release_notes import extract_section
67

78

@@ -20,3 +21,25 @@ def test_extract_section_returns_version_content():
2021

2122
assert section.startswith("## [1.3.0]")
2223
assert "运行与配置" in section
24+
25+
26+
def test_validate_bundle_uses_smoke_test(monkeypatch, tmp_path):
27+
bundle_root = tmp_path / "bundle"
28+
app_dir = bundle_root / "PyRAG-Kit"
29+
app_dir.mkdir(parents=True)
30+
executable = app_dir / "PyRAG-Kit"
31+
executable.write_text("", encoding="utf-8")
32+
33+
recorded = {}
34+
35+
def fake_run(command, **kwargs):
36+
recorded["command"] = command
37+
recorded["kwargs"] = kwargs
38+
return subprocess.CompletedProcess(command, 0, stdout="PyRAG-Kit 1.3.0 smoke test ok\n", stderr="")
39+
40+
monkeypatch.setattr("scripts.build_binary_release.subprocess.run", fake_run)
41+
42+
validate_bundle(bundle_root)
43+
44+
assert recorded["command"][-1] == "--smoke-test"
45+
assert recorded["kwargs"]["check"] is True

0 commit comments

Comments
 (0)