#![cfg(feature = "python")]
use std::path::{Path, PathBuf};
use fidius_core::python_descriptor::PythonInterfaceDescriptor;
use fidius_host::PluginHost;
use serde::Serialize;
#[derive(Serialize)]
struct PipeConfig {
display_name: String,
}
fn byte_pipe_descriptor() -> &'static PythonInterfaceDescriptor {
&test_plugin_smoke::__fidius_BytePipe::BytePipe_PYTHON_DESCRIPTOR
}
fn repo_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.parent()
.unwrap()
.to_path_buf()
}
fn copy_dir(src: &Path, dst: &Path) {
std::fs::create_dir_all(dst).unwrap();
for entry in std::fs::read_dir(src).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
let dest = dst.join(entry.file_name());
if path.is_dir() {
copy_dir(&path, &dest);
} else {
std::fs::copy(&path, &dest).unwrap();
}
}
}
fn stage(tmp: &tempfile::TempDir) -> PathBuf {
let plugins_root = tmp.path().to_path_buf();
let dest = plugins_root.join("py-configured-pipe");
copy_dir(&repo_root().join("tests/test-plugin-py-configured"), &dest);
copy_dir(
&repo_root().join("python/fidius"),
&dest.join("vendor/fidius"),
);
let py = dest.join("configured_pipe.py");
let src = std::fs::read_to_string(&py).unwrap();
let patched = src.replace(
"__HASH_PLACEHOLDER__",
&format!("0x{:016X}", byte_pipe_descriptor().interface_hash),
);
std::fs::write(&py, patched).unwrap();
plugins_root
}
#[test]
fn config_bound_once_and_used_in_methods() {
let tmp = tempfile::TempDir::new().unwrap();
let plugins = stage(&tmp);
let host = PluginHost::builder().search_path(&plugins).build().unwrap();
let handle = host
.load_python_configured(
"py-configured-pipe",
byte_pipe_descriptor(),
&PipeConfig {
display_name: "configured-pipe".into(),
},
)
.expect("load_python_configured");
let name: String = handle.call_method(1, &()).expect("name");
assert_eq!(name, "configured-pipe");
let out = handle.call_method_raw(0, b"abc").expect("reverse");
assert_eq!(out, b"cba");
}