fix(plugin): WASM 集成测试自动构建 Component — OnceLock 线程安全
Some checks failed
CI / rust-check (push) Has been cancelled
CI / rust-test (push) Has been cancelled
CI / frontend-build (push) Has been cancelled
CI / security-audit (push) Has been cancelled

测试无法找到 .component.wasm 文件导致 6 个测试全部失败。
改进 wasm_path() 使用 OnceLock 保证只转换一次,
自动从编译产物通过 wasm-tools component new 生成。
This commit is contained in:
iven
2026-04-27 12:34:52 +08:00
parent 7c0f0ce906
commit a1bc62cd5e

View File

@@ -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<String> = 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
)
})?;