Skip to content

Commit

Permalink
test(exec): 添加脚本执行测试用例并进行代码调整
Browse files Browse the repository at this point in the history
- 在 exec.rs 中添加测试用例模块,验证脚本执行功能- 更新 Cargo.toml,将包名改为 moon-agent
- 移除 anyhow 和 thiserror 依赖- 更新 README.md,添加打包步骤说明
  • Loading branch information
aide-cloud committed Dec 2, 2024
1 parent eb95624 commit 2426147
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
40 changes: 16 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "script-agent"
name = "moon-agent"
version = "0.1.0"
edition = "2021"

Expand All @@ -10,8 +10,6 @@ serde_json = "1.0"
tokio = { version = "1.0", features = ["full"] }
uuid = { version = "1.4", features = ["v4"] }
tempfile = "3.8"
anyhow = "1.0"
thiserror = "1.0"
tracing = "0.1"
tracing-subscriber = "0.3"
dotenv = "0.15"
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ cargo build
cargo run
```

4. 打包

```bash
cargo build --release
```

## 使用示例

* 执行 Python2 脚本
Expand Down
26 changes: 26 additions & 0 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ impl ScriptExecutor {
.write_all(script_req.content.as_bytes())
.map_err(|e| format!("Failed to write script: {}", e))?;

println!("file_path: {}", temp_file.path().display());

// 设置执行权限
#[cfg(unix)]
fs::set_permissions(temp_file.path(), fs::Permissions::from_mode(0o755))
Expand Down Expand Up @@ -57,3 +59,27 @@ impl ScriptExecutor {
pub async fn execute_script(script_req: &ScriptRequest) -> Result<ScriptResponse, String> {
ScriptExecutor::execute(&script_req)
}


#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_execute_script() {
let script_req = ScriptRequest {
script_type: ScriptType::Python2,
content: "print('Hello, World!')".to_string(),
};

let result = execute_script(&script_req).await;

assert!(result.is_ok());

let script_resp = result.unwrap();

assert_eq!(script_resp.stdout, "Hello, World!\n");
assert_eq!(script_resp.stderr, "");
assert_eq!(script_resp.exit_code, 0);
}
}

0 comments on commit 2426147

Please sign in to comment.