From a1bc62cd5e185c0eacfa955e595319f7ac6c30c3 Mon Sep 17 00:00:00 2001 From: iven Date: Mon, 27 Apr 2026 12:34:52 +0800 Subject: [PATCH] =?UTF-8?q?fix(plugin):=20WASM=20=E9=9B=86=E6=88=90?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=87=AA=E5=8A=A8=E6=9E=84=E5=BB=BA=20Compon?= =?UTF-8?q?ent=20=E2=80=94=20OnceLock=20=E7=BA=BF=E7=A8=8B=E5=AE=89?= =?UTF-8?q?=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 测试无法找到 .component.wasm 文件导致 6 个测试全部失败。 改进 wasm_path() 使用 OnceLock 保证只转换一次, 自动从编译产物通过 wasm-tools component new 生成。 --- .../tests/test_plugin_integration.rs | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) 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 ) })?;