use super::super::fixtures::*;
use super::*;
#[test]
fn split_proposes_and_refuses_to_apply() {
let repo = routing_fixture("cli-split");
let root = repo.path();
let Ok(()) = std::fs::write(root.join("gamma.rs"), "pub fn gamma() {}\n")
else {
unreachable!("a module file is writable")
};
write_spec(root, "alpha", "widgets and sprockets");
let Ok(spec) = std::fs::read_to_string(root.join("SPEC.md")) else {
unreachable!("the fixture spec is readable")
};
let Ok(()) = std::fs::write(
root.join("SPEC.md"),
format!("{spec}\nV1: gamma holds the gamma rule\n"),
) else {
unreachable!("the fixture spec is writable")
};
assert_eq!(split_cmd(root, root, true), ExitCode::from(2), "--apply");
assert_eq!(split_cmd(root, root, false), ExitCode::SUCCESS);
assert!(!root.join("gamma").exists(), "split created a directory");
}
#[test]
fn a_private_or_crate_visible_module_is_still_a_node() {
let repo = routing_fixture("cli-split-grades");
let root = repo.path();
let Ok(()) = std::fs::create_dir_all(root.join("src")) else {
unreachable!("a src dir is creatable")
};
let Ok(()) = std::fs::write(
root.join("src").join("lib.rs"),
"pub mod api;\npub(crate) mod inner;\nmod hidden;\n",
) else {
unreachable!("a lib.rs is writable")
};
let found = split::structure(root);
let grade =
|n: &str| found.iter().find(|p| p.name == n).map(|p| p.evidence);
assert_eq!(grade("api"), Some(split::Evidence::Published));
assert_eq!(
grade("inner"),
Some(split::Evidence::Declared),
"pub(crate) is not published"
);
assert_eq!(grade("hidden"), Some(split::Evidence::Declared));
}
#[test]
fn split_proposes_nodes_the_spec_never_mentions() {
let repo = routing_fixture("cli-split-structure");
let root = repo.path();
let Ok(()) = std::fs::create_dir_all(root.join("src")) else {
unreachable!("a src dir is creatable")
};
let Ok(()) = std::fs::write(
root.join("src").join("lib.rs"),
"pub mod widget;\nmod helper;\n#[cfg(test)]\nmod testonly;\n",
) else {
unreachable!("a lib.rs is writable")
};
let found = split::structure(root);
let names: Vec<&str> = found.iter().map(|p| p.name.as_str()).collect();
assert_eq!(
names,
vec!["widget", "helper"],
"published first, then declared: {found:?}"
);
assert_eq!(split_cmd(root, root, false), ExitCode::SUCCESS);
}
#[test]
fn split_on_a_directory_with_no_spec_is_usage() {
let repo = routing_fixture("cli-split-nospec");
let bare = repo.path().join("bare");
let Ok(()) = std::fs::create_dir_all(&bare) else {
unreachable!("a dir is creatable")
};
assert_eq!(split_cmd(repo.path(), &bare, false), ExitCode::from(2));
}
fn seam_fixture() -> crate::testrepo::TestRepo {
let repo = routing_fixture("cli-seam");
let Ok(()) = std::fs::write(
repo.path().join("alpha").join("lib.rs"),
"pub struct Shared {}\nstruct Hidden;\n",
) else {
unreachable!("a module file is writable")
};
repo
}
#[test]
fn seam_names_a_public_type_and_not_a_private_one() {
let repo = seam_fixture();
let nodes = fed::discover(repo.path());
let t = node_types(&repo.path().join("alpha"), &nodes);
let named: Vec<&str> = t.iter().map(|p| p.name.as_str()).collect();
assert_eq!(named, vec!["Shared"], "a private type is not vocabulary");
}
#[test]
fn a_node_declaring_no_types_reports_rather_than_erroring() {
let repo = seam_fixture();
let root = repo.path();
let nodes = fed::discover(root);
assert!(node_types(&root.join("beta"), &nodes).is_empty());
assert_eq!(seam_cmd(root, root), ExitCode::SUCCESS, "absence is legal");
}
#[test]
fn a_type_belongs_to_the_nearest_node_not_to_every_ancestor() {
let repo = seam_fixture();
let nodes = fed::discover(repo.path());
let at_root = node_types(repo.path(), &nodes);
assert!(at_root.is_empty(), "alpha owns Shared: {at_root:?}");
}
#[test]
fn seam_on_a_dir_matching_no_node_is_usage() {
assert_eq!(run_args(argv(&["seam", "no-such-dir"])), ExitCode::from(2));
}