diff --git a/crates/erp-plugin-prototype/tests/test_plugin_integration.rs b/crates/erp-plugin-prototype/tests/test_plugin_integration.rs index 3fa9d00..1923516 100644 --- a/crates/erp-plugin-prototype/tests/test_plugin_integration.rs +++ b/crates/erp-plugin-prototype/tests/test_plugin_integration.rs @@ -11,32 +11,48 @@ use anyhow::Result; use erp_plugin_prototype::{create_engine, load_plugin}; -/// 获取测试插件 WASM Component 文件路径 -fn wasm_path() -> String { - let candidates = [ - // 预构建的 WASM Component(通过 wasm-tools component new 生成) - "../../target/erp_plugin_test_sample.component.wasm".into(), - // 备选:绝对路径 - format!( - "{}/../../target/erp_plugin_test_sample.component.wasm", - std::env::current_dir().unwrap().display() - ), - ]; - for path in &candidates { - if std::path::Path::new(path).exists() { - return path.clone(); +static WASM_PATH: std::sync::OnceLock = std::sync::OnceLock::new(); + +/// 获取测试插件 WASM Component 文件路径(线程安全,只转换一次) +fn wasm_path() -> &'static str { + WASM_PATH.get_or_init(|| { + let component_path = "../../target/erp_plugin_test_sample.component.wasm"; + let raw_wasm = + "../../target/wasm32-unknown-unknown/release/erp_plugin_test_sample.wasm"; + + if std::path::Path::new(component_path).exists() { + return component_path.to_owned(); } - } - candidates[0].clone() + + if std::path::Path::new(raw_wasm).exists() { + let out = std::process::Command::new("wasm-tools") + .args(["component", "new", raw_wasm, "-o", component_path]) + .output() + .expect("wasm-tools 未安装,请运行: cargo install wasm-tools"); + if out.status.success() && std::path::Path::new(component_path).exists() { + return component_path.to_owned(); + } + panic!( + "wasm-tools component new 失败: {}", + String::from_utf8_lossy(&out.stderr) + ); + } + + panic!( + "未找到 WASM 编译产物。请先运行:\n \ + cargo build -p erp-plugin-test-sample --target wasm32-unknown-unknown --release" + ); + }) } #[tokio::test] async fn test_v6_load_plugin_from_binary() -> Result<()> { let wasm_path = wasm_path(); - let wasm_bytes = std::fs::read(&wasm_path).map_err(|e| { + let wasm_bytes = std::fs::read(wasm_path).map_err(|e| { anyhow::anyhow!( "读取 WASM 失败: {}。请先编译: cargo build -p erp-plugin-test-sample --target wasm32-unknown-unknown --release\n路径: {}", - e, wasm_path + e, + wasm_path ) })?;