use super::*;
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::time::Duration;
pub(super) fn cfg() -> KitConfig {
let mut c = KitConfig::new("myapp");
c.contract_version = "0.1".to_string();
c.wrapper_body = "myapp_plugin::export!({crate_ident}::create);\n".to_string();
c.lock_timeout = Duration::from_millis(500);
c
}
pub(super) fn write_into(
c: &KitConfig,
plugin: PluginSource<'_>,
contract: Option<&ContractDep>,
) -> tempfile::TempDir {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().join("wrapper");
std::fs::create_dir_all(dir.join("src")).unwrap();
let stem = c.lib_stem("myapp-plugin-foo");
write(c, &dir, "myapp-plugin-foo", &stem, plugin, contract).unwrap();
tmp
}
pub(super) fn toml_of(tmp: &tempfile::TempDir) -> String {
std::fs::read_to_string(tmp.path().join("wrapper").join("Cargo.toml")).unwrap()
}
fn generate(c: &KitConfig) -> (tempfile::TempDir, PathBuf) {
generate_with_contract(c, None)
}
fn generate_with_contract(
c: &KitConfig,
contract: Option<&ContractDep>,
) -> (tempfile::TempDir, PathBuf) {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().join("build").join("myapp-plugin-foo");
std::fs::create_dir_all(dir.join("src")).unwrap();
let stem = c.lib_stem("myapp-plugin-foo");
write(
c,
&dir,
"myapp-plugin-foo",
&stem,
PluginSource::Registry { version: "0.1.0" },
contract,
)
.unwrap();
(tmp, dir)
}
#[test]
fn a_local_plugin_becomes_a_path_dependency_and_still_exports() {
let c = cfg();
let tmp = write_into(
&c,
PluginSource::Path {
dir: Path::new("/local/plugin-foo"),
},
None,
);
let toml = toml_of(&tmp);
assert!(
toml.contains(r#"myapp-plugin-foo = { path = "/local/plugin-foo" }"#),
"{toml}"
);
let body =
std::fs::read_to_string(tmp.path().join("wrapper").join("src").join("lib.rs")).unwrap();
assert_eq!(body, "myapp_plugin::export!(myapp_plugin_foo::create);\n");
}
#[test]
fn wrapper_manifest_names_the_right_library() {
let c = cfg();
let (_tmp, dir) = generate(&c);
let toml = std::fs::read_to_string(dir.join("Cargo.toml")).unwrap();
assert!(
toml.contains(r#"name = "myapp_plugin_foo""#),
"{toml}"
);
assert!(toml.contains(r#"crate-type = ["cdylib"]"#), "{toml}");
}
#[test]
fn wrapper_is_not_publishable() {
let c = cfg();
let (_tmp, dir) = generate(&c);
let toml = std::fs::read_to_string(dir.join("Cargo.toml")).unwrap();
assert!(toml.contains("publish = false"), "{toml}");
}
#[test]
fn wrapper_is_its_own_workspace() {
let c = cfg();
let (_tmp, dir) = generate(&c);
let toml = std::fs::read_to_string(dir.join("Cargo.toml")).unwrap();
assert!(toml.contains("[workspace]"), "{toml}");
}
#[test]
fn the_plugin_version_is_pinned_exactly() {
let c = cfg();
let (_tmp, dir) = generate(&c);
let toml = std::fs::read_to_string(dir.join("Cargo.toml")).unwrap();
assert!(toml.contains(r#"myapp-plugin-foo = "=0.1.0""#), "{toml}");
}
#[test]
fn contract_crate_is_a_dependency() {
let c = cfg();
let (_tmp, dir) = generate(&c);
let toml = std::fs::read_to_string(dir.join("Cargo.toml")).unwrap();
assert!(toml.contains("myapp-plugin"), "{toml}");
assert!(toml.contains(r#""0.1""#), "{toml}");
}
#[test]
fn the_plugins_own_requirement_wins() {
let c = cfg();
let contract = ContractDep {
req: Some("=0.0.3".to_string()),
path: None,
};
let (_tmp, dir) = generate_with_contract(&c, Some(&contract));
let toml = std::fs::read_to_string(dir.join("Cargo.toml")).unwrap();
assert!(toml.contains(r#"myapp-plugin = "=0.0.3""#), "{toml}");
assert!(
!toml.contains(r#""0.1""#),
"the fallback must not leak in: {toml}"
);
}
#[test]
fn body_substitutes_the_crate_ident() {
let c = cfg();
let (_tmp, dir) = generate(&c);
let body = std::fs::read_to_string(dir.join("src").join("lib.rs")).unwrap();
assert_eq!(body, "myapp_plugin::export!(myapp_plugin_foo::create);\n");
assert!(
!body.contains("{crate_ident}"),
"placeholder not substituted: {body}"
);
}
#[test]
fn body_substitution_handles_multi_hyphen_names() {
let c = cfg();
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().join("w");
std::fs::create_dir_all(dir.join("src")).unwrap();
let stem = c.lib_stem("myapp-plugin-a-b");
write(
&c,
&dir,
"myapp-plugin-a-b",
&stem,
PluginSource::Registry { version: "0.1.0" },
None,
)
.unwrap();
let body = std::fs::read_to_string(dir.join("src").join("lib.rs")).unwrap();
assert_eq!(body, "myapp_plugin::export!(myapp_plugin_a_b::create);\n");
}
#[test]
fn writes_a_body_but_no_toolchain_pin() {
let c = cfg();
let (_tmp, dir) = generate(&c);
assert!(dir.join("src").join("lib.rs").is_file());
assert!(
!dir.join("rust-toolchain.toml").exists(),
"the wrapper must not pin a toolchain"
);
}
#[test]
fn no_patch_section_without_local_overrides() {
let c = cfg();
let (_tmp, dir) = generate(&c);
let toml = std::fs::read_to_string(dir.join("Cargo.toml")).unwrap();
assert!(!toml.contains("[patch.crates-io]"), "{toml}");
}
#[test]
fn local_overrides_become_a_patch_section() {
let mut c = cfg();
c.local_overrides = BTreeMap::from([
(
"myapp-plugin-foo".to_string(),
PathBuf::from("/local/plugin-foo"),
),
("myapp-plugin".to_string(), PathBuf::from("/local/contract")),
]);
let (_tmp, dir) = generate(&c);
let toml = std::fs::read_to_string(dir.join("Cargo.toml")).unwrap();
assert!(toml.contains("[patch.crates-io]"), "{toml}");
assert!(
toml.contains(r#"myapp-plugin-foo = { path = "/local/plugin-foo" }"#),
"{toml}"
);
assert!(
toml.contains(r#"myapp-plugin = { path = "/local/contract" }"#),
"{toml}"
);
}
#[test]
fn local_override_paths_use_forward_slashes() {
let mut c = cfg();
c.local_overrides = BTreeMap::from([(
"myapp-plugin-foo".to_string(),
PathBuf::from(r"D:\local\plugin-foo"),
)]);
let (_tmp, dir) = generate(&c);
let toml = std::fs::read_to_string(dir.join("Cargo.toml")).unwrap();
assert!(toml.contains(r#"path = "D:/local/plugin-foo""#), "{toml}");
let patch_line = toml
.lines()
.find(|l| l.starts_with("myapp-plugin-foo"))
.unwrap();
assert!(!patch_line.contains('\\'), "{patch_line}");
}