feat: 解决python虚拟环境问题
This commit is contained in:
@@ -275,6 +275,77 @@ async fn manual_script_run_captures_exit_code_and_logs() {
|
||||
app.state.shutdown();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn auto_detected_venv_injects_activation_environment() {
|
||||
let app = test_app().await;
|
||||
let project = app._temp_dir.path().join("project");
|
||||
let working_dir = project.join("src");
|
||||
let venv = project.join(".venv");
|
||||
std::fs::create_dir_all(&working_dir).unwrap();
|
||||
std::fs::create_dir_all(venv.join("Scripts")).unwrap();
|
||||
std::fs::write(venv.join("pyvenv.cfg"), "home = C:\\Python").unwrap();
|
||||
let command_shell = std::env::var_os("COMSPEC")
|
||||
.map(std::path::PathBuf::from)
|
||||
.unwrap_or_else(|| std::path::PathBuf::from("C:\\Windows\\System32\\cmd.exe"));
|
||||
std::fs::copy(command_shell, venv.join("Scripts").join("python.exe")).unwrap();
|
||||
|
||||
let task = json!({
|
||||
"id": "venv_task",
|
||||
"name": "Venv task",
|
||||
"cron": "0 0 2 * * *",
|
||||
"timeout_secs": 5,
|
||||
"action": {
|
||||
"type": "script",
|
||||
"working_dir": working_dir,
|
||||
"interpreter": "python.exe",
|
||||
"script": "/C",
|
||||
"args": [
|
||||
"echo",
|
||||
"VIRTUAL=%VIRTUAL_ENV%;UNBUFFERED=%PYTHONUNBUFFERED%;HOME=%PYTHONHOME%;CUSTOM=%CUSTOM%"
|
||||
],
|
||||
"env": {
|
||||
"PYTHONHOME": "C:\\wrong-python-home",
|
||||
"PYTHONUNBUFFERED": "0",
|
||||
"CUSTOM": "yes"
|
||||
}
|
||||
}
|
||||
});
|
||||
let create_response = send_json(&app.router, Method::POST, "/api/v1/tasks", Some(task)).await;
|
||||
assert_eq!(create_response.status(), StatusCode::CREATED);
|
||||
let run = send_json(
|
||||
&app.router,
|
||||
Method::POST,
|
||||
"/api/v1/tasks/venv_task/run",
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
let run_id = response_json(run).await["id"].as_i64().unwrap();
|
||||
assert_eq!(
|
||||
wait_for_run(&app.router, run_id).await["status"],
|
||||
"succeeded"
|
||||
);
|
||||
let log = response_json(
|
||||
send_json(
|
||||
&app.router,
|
||||
Method::GET,
|
||||
&format!("/api/v1/runs/{run_id}/logs"),
|
||||
None,
|
||||
)
|
||||
.await,
|
||||
)
|
||||
.await;
|
||||
let content = log["content"].as_str().unwrap();
|
||||
assert!(
|
||||
content.contains(&format!(
|
||||
"VIRTUAL={};UNBUFFERED=1;HOME=%PYTHONHOME%;CUSTOM=yes",
|
||||
venv.display()
|
||||
)),
|
||||
"unexpected venv log output: {content}"
|
||||
);
|
||||
assert!(!content.contains("wrong-python-home"));
|
||||
app.state.shutdown();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn http_and_process_executors_run_through_the_api() {
|
||||
let app = test_app().await;
|
||||
|
||||
Reference in New Issue
Block a user