feat: 支持 uv 从 pyproject/requirements 创建虚拟环境
- 工作目录探测新增 pyproject.toml / requirements*.txt 扫描、uv 可用性与 can_create - 新增 POST /api/v1/python/venv/create:已有虚拟环境优先复用;pyproject 走 uv sync,requirements 走 uv venv + uv pip install,并使用默认 PyPI 镜像 - 编辑器在缺少虚拟环境且具备依赖文件时显示「用 uv 创建环境」;没有 uv 或没有依赖文件时明确报错 - 解释器字段改为可选,留空由服务端默认 python.exe 并使用虚拟环境;Python 任务缺少虚拟环境/依赖文件时运行直接失败,不再回退系统解释器 - 补后端探测/创建测试、前端入口测试与 README
This commit is contained in:
@@ -47,9 +47,22 @@ async fn fetch_token(app: &Router) -> String {
|
||||
}
|
||||
|
||||
async fn post_probe(app: &Router, token: Option<&str>, payload: Value) -> (StatusCode, Value) {
|
||||
post_json(app, "/api/v1/python/venv", token, payload).await
|
||||
}
|
||||
|
||||
async fn post_create(app: &Router, token: Option<&str>, payload: Value) -> (StatusCode, Value) {
|
||||
post_json(app, "/api/v1/python/venv/create", token, payload).await
|
||||
}
|
||||
|
||||
async fn post_json(
|
||||
app: &Router,
|
||||
uri: &str,
|
||||
token: Option<&str>,
|
||||
payload: Value,
|
||||
) -> (StatusCode, Value) {
|
||||
let mut builder = Request::builder()
|
||||
.method("POST")
|
||||
.uri("/api/v1/python/venv")
|
||||
.uri(uri)
|
||||
.header(header::CONTENT_TYPE, "application/json");
|
||||
if let Some(token) = token {
|
||||
builder = builder.header("x-taskpilot-token", token);
|
||||
@@ -68,7 +81,7 @@ async fn post_probe(app: &Router, token: Option<&str>, payload: Value) -> (Statu
|
||||
.into_body()
|
||||
.collect()
|
||||
.await
|
||||
.expect("probe body should be readable")
|
||||
.expect("response body should be readable")
|
||||
.to_bytes();
|
||||
|
||||
(status, serde_json::from_slice(&body).unwrap_or(Value::Null))
|
||||
@@ -179,3 +192,77 @@ async fn venv_probe_reports_missing_working_directory() {
|
||||
.contains("工作目录")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn venv_probe_reports_project_dependency_files() {
|
||||
let (app, temp_dir) = test_app().await;
|
||||
let token = fetch_token(&app).await;
|
||||
let project = temp_dir.path().join("deps-project");
|
||||
fs::create_dir_all(&project).expect("project directory should be created");
|
||||
fs::write(
|
||||
project.join("pyproject.toml"),
|
||||
"[project]\nname = \"demo\"\n",
|
||||
)
|
||||
.expect("pyproject.toml should be written");
|
||||
fs::write(project.join("requirements.txt"), "").expect("requirements.txt should be written");
|
||||
|
||||
let (status, body) = post_probe(
|
||||
&app,
|
||||
Some(&token),
|
||||
json!({
|
||||
"working_dir": project.to_str().expect("project path should be UTF-8"),
|
||||
"interpreter": "python.exe"
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
assert_eq!(body["status"], "missing");
|
||||
assert_eq!(body["has_pyproject"], true);
|
||||
assert_eq!(body["has_requirements"], true);
|
||||
assert_eq!(body["requirements_files"], json!(["requirements.txt"]));
|
||||
assert_eq!(body["can_create"], body["uv_available"]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn venv_create_rejects_project_without_dependency_files() {
|
||||
let (app, temp_dir) = test_app().await;
|
||||
let token = fetch_token(&app).await;
|
||||
let project = temp_dir.path().join("empty-project");
|
||||
fs::create_dir_all(&project).expect("project directory should be created");
|
||||
|
||||
let (status, body) = post_create(
|
||||
&app,
|
||||
Some(&token),
|
||||
json!({
|
||||
"working_dir": project.to_str().expect("project path should be UTF-8")
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(status, StatusCode::BAD_REQUEST);
|
||||
assert_eq!(body["code"], "VENV_CREATE_FAILED");
|
||||
assert!(
|
||||
body["message"]
|
||||
.as_str()
|
||||
.expect("message should be a string")
|
||||
.contains("pyproject.toml")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn venv_create_requires_management_token() {
|
||||
let (app, temp_dir) = test_app().await;
|
||||
|
||||
let (status, body) = post_create(
|
||||
&app,
|
||||
None,
|
||||
json!({
|
||||
"working_dir": temp_dir.path().to_str().expect("temp path should be UTF-8")
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(status, StatusCode::FORBIDDEN);
|
||||
assert_eq!(body["code"], "FORBIDDEN");
|
||||
}
|
||||
|
||||
@@ -113,6 +113,19 @@ fn script_task(id: &str, name: &str) -> Value {
|
||||
})
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn empty_interpreter_defaults_to_python() {
|
||||
let (app, _temp_dir) = test_app().await;
|
||||
let mut payload = script_task("default_interpreter", "Default interpreter");
|
||||
payload["action"]["interpreter"] = json!("");
|
||||
|
||||
let response = send_json(&app, Method::POST, "/api/v1/tasks", Some(payload)).await;
|
||||
|
||||
assert_eq!(response.status(), StatusCode::CREATED);
|
||||
let created = response_json(response).await;
|
||||
assert_eq!(created["action"]["interpreter"], "python.exe");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn task_crud_flow_works_end_to_end() {
|
||||
let (app, _temp_dir) = test_app().await;
|
||||
|
||||
Reference in New Issue
Block a user