use std::path::{Path, PathBuf};
use crate_plugin_kit::cache::InstallSource;
use crate_plugin_kit::{CratePluginKit, KitConfig};
struct Entry;
fn cfg() -> KitConfig {
let mut cfg = KitConfig::new("toyapp");
cfg.wrapper_body = concat!(
"pub fn entry_value() -> u32 { {crate_ident}::create() }\n",
"\n",
"#[unsafe(no_mangle)]\n",
"pub extern \"C\" fn toyapp_plugin_entry_v1() -> u32 { entry_value() }\n",
)
.to_string();
cfg
}
fn fixture() -> (tempfile::TempDir, PathBuf) {
let tmp = tempfile::tempdir().expect("should create a temp dir");
let src = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("fixtures");
let dst = tmp.path().join("fixtures");
copy_tree(&src.join("toy-rlib-plugin"), &dst.join("toy-rlib-plugin"));
copy_tree(&src.join("toy-contract"), &dst.join("toy-contract"));
(tmp, dst.join("toy-rlib-plugin"))
}
fn copy_tree(src: &Path, dst: &Path) {
std::fs::create_dir_all(dst).expect("should create the fixture directory");
for entry in std::fs::read_dir(src).expect("should read the fixture directory") {
let entry = entry.expect("should read a fixture entry");
let to = dst.join(entry.file_name());
if entry.file_type().expect("should stat").is_dir() {
copy_tree(&entry.path(), &to);
} else {
std::fs::copy(entry.path(), &to).expect("should copy a fixture file");
}
}
}
fn kit(tmp: &Path) -> (CratePluginKit<Entry>, PathBuf) {
let root = tmp.join("store");
let kit = CratePluginKit::<Entry>::new(cfg().with_data_dir(&root)).expect("should build");
(kit, root)
}
#[test]
fn a_checkout_installs_like_a_published_plugin() {
let (tmp, checkout) = fixture();
let (kit, _root) = kit(tmp.path());
let installed = kit
.install_from_path(&checkout)
.expect("installing the fixture should succeed");
assert_eq!(installed.crate_name, "toyapp-plugin-toy");
assert_eq!(installed.version, "0.1.0");
assert_eq!(
installed.source,
InstallSource::Local {
path: checkout.canonicalize().unwrap()
},
"the directory is remembered, because it is what `update` rebuilds from"
);
assert!(installed.library.is_file(), "{:?}", installed.library);
assert!(installed.dir.join("toyapp-plugin.toml").is_file());
let listed = kit.list().expect("the store should list");
assert_eq!(listed.len(), 1);
assert_eq!(listed[0].crate_name, "toyapp-plugin-toy");
assert_eq!(listed[0].abi, Some(1));
assert_eq!(listed[0].family.as_deref(), Some("test"));
assert_eq!(
listed[0].extra["detect"]["strong"][0].as_str(),
Some("toy.lock"),
"the host's own section survived the install"
);
let loaded = kit
.load("toyapp-plugin-toy")
.expect("the installed plugin should load");
assert_eq!(loaded.path(), installed.library);
}
#[test]
fn update_rebuilds_from_the_recorded_directory() {
let (tmp, checkout) = fixture();
let (kit, root) = kit(tmp.path());
kit.install_from_path(&checkout).expect("should install");
assert!(root.join("plugins").join("toyapp-plugin-toy").is_dir());
let updated = kit
.update("toy", Some("9.9.9"))
.expect("a local plugin updates by rebuilding");
assert_eq!(updated.version, "0.1.0", "the checkout's own version");
assert!(matches!(updated.source, InstallSource::Local { .. }));
assert!(updated.library.is_file());
}
#[test]
fn uninstall_removes_it() {
let (tmp, checkout) = fixture();
let (kit, _root) = kit(tmp.path());
let installed = kit.install_from_path(&checkout).expect("should install");
kit.uninstall("toy").expect("should uninstall");
assert!(!installed.dir.exists(), "the directory should be gone");
assert!(kit.list().expect("the store should list").is_empty());
}
#[test]
fn update_keeps_the_build_cache_inside_the_wrapper() {
let (tmp, checkout) = fixture();
let (kit, root) = kit(tmp.path());
kit.install_from_path(&checkout).expect("should install");
let cache = root.join("build").join("toyapp-plugin-toy").join("target");
assert!(
cache.is_dir(),
"the first install built through a wrapper, so cargo left a target directory: {}",
cache.display()
);
std::fs::write(cache.join("sentinel"), "cargo's cache lives here").expect("should write");
let source = checkout.join("src").join("lib.rs");
let mut text = std::fs::read_to_string(&source).expect("should read the fixture source");
text.push_str("\n// touched, to give the rebuild something to do\n");
std::fs::write(&source, text).expect("should write the fixture source");
let updated = kit.update("toy", None).expect("should rebuild");
assert!(updated.library.is_file(), "{:?}", updated.library);
assert!(
cache.join("sentinel").is_file(),
"the build cache must survive an update, or every update recompiles the whole graph"
);
}