use super::named_targets;
const MANIFEST: &str = "[package]\nname = \"scopefix\"\nversion = \"0.1.0\"\nedition = \"2021\"\n";
fn crate_with(dir: &str, files: &[&str], extra_manifest: &str) -> tempfile::TempDir {
let tmp = tempfile::tempdir().expect("tempdir");
std::fs::create_dir_all(tmp.path().join("src")).expect("mkdir src");
std::fs::write(tmp.path().join("src/lib.rs"), "pub fn used() {}\n").expect("write lib");
std::fs::write(
tmp.path().join("Cargo.toml"),
format!("{MANIFEST}{extra_manifest}"),
)
.expect("write manifest");
std::fs::create_dir_all(tmp.path().join(dir)).expect("mkdir");
for f in files {
let p = tmp.path().join(dir).join(f);
if let Some(parent) = p.parent() {
std::fs::create_dir_all(parent).expect("mkdir parent");
}
std::fs::write(&p, "fn main() {}\n").expect("write");
}
tmp
}
#[test]
fn examples_are_discovered_as_named_targets() {
let tmp = crate_with("examples", &["demo.rs", "other.rs"], "");
assert_eq!(
named_targets(tmp.path(), "example"),
vec!["demo".to_string(), "other".to_string()],
"examples/ must yield one cargo target per file"
);
}
#[test]
fn benches_are_named_without_the_libs_implicit_bench_target() {
let tmp = crate_with(
"benches",
&["b.rs"],
"\n[[bench]]\nname = \"b\"\nharness = false\n",
);
assert_eq!(named_targets(tmp.path(), "bench"), vec!["b".to_string()]);
}
#[test]
fn feature_gated_targets_are_skipped() {
let tmp = crate_with(
"examples",
&["plain.rs", "gated.rs"],
"\n[features]\ndemo = []\n\n[[example]]\nname = \"gated\"\nrequired-features = [\"demo\"]\n",
);
assert_eq!(
named_targets(tmp.path(), "example"),
vec!["plain".to_string()],
"a feature-gated example must not be named on the cargo command line"
);
}
#[test]
fn missing_directory_yields_no_targets() {
let tmp = crate_with("src", &[], "");
assert!(named_targets(tmp.path(), "example").is_empty());
assert!(named_targets(tmp.path(), "bench").is_empty());
}
#[test]
fn a_non_package_directory_yields_no_targets() {
let tmp = tempfile::tempdir().expect("tempdir");
assert!(named_targets(tmp.path(), "example").is_empty());
}