use super::*;
const HAND_WRITTEN_LICENSE: &str = "MIT License\n\nCopyright (c) 2019 The Sample Authors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files.\n";
const PLACEHOLDER_LICENSE: &str = "MIT License\n\nCopyright (c) <year> <copyright holder>\n";
fn license_fixture(base: &Path) -> (PathBuf, Vec<GeneratedFile>) {
let full = seed(base, "LICENSE", HAND_WRITTEN_LICENSE);
let files = vec![GeneratedFile {
path: PathBuf::from("LICENSE"),
content: PLACEHOLDER_LICENSE.to_owned(),
generated_header: false,
}];
(full, files)
}
#[test]
fn adopting_an_unmarkable_create_once_seed_touches_none_of_its_bytes() {
let dir = tempfile::tempdir().expect("tempdir");
let base = dir.path();
let (full, files) = license_fixture(base);
let outputs = managed_outputs(&files, base);
assert!(
outputs[0].create_once,
"the fixture has to be on the gated rail, or this test proves nothing about the flag"
);
let report = run(&clobbering_seeds(base, "LICENSE"), &outputs).expect("adopt");
assert_eq!(
std::fs::read_to_string(&full).expect("read after"),
HAND_WRITTEN_LICENSE,
"adoption must not write the placeholder, add a marker, or reformat an unmarkable seed"
);
assert_eq!(report.recorded_unstampable, vec![PathBuf::from("LICENSE")]);
assert!(
crate::cli::cache::is_scaffold_owned_path(base, &full),
"the consent has to land in the committed record -- it is the only place it can land"
);
}
#[test]
fn the_recorded_adoption_is_what_lets_the_next_overwriting_write_replace_the_seed() {
let dir = tempfile::tempdir().expect("tempdir");
let base = dir.path();
let (full, files) = license_fixture(base);
let outputs = managed_outputs(&files, base);
run(&clobbering_seeds(base, "LICENSE"), &outputs).expect("adopt");
let write = crate::cli::pipeline::write_scaffold_files_report(&files, base, true).expect("scaffold write");
assert_eq!(
std::fs::read_to_string(&full).expect("read after"),
PLACEHOLDER_LICENSE,
"the adoption armed this write: the hand-maintained licence is gone and the placeholder \
is in its place"
);
assert!(
write.changed_paths.contains(&full),
"the write must report the replacement it performed, got: {:?}",
write.changed_paths
);
assert!(
write.refused_paths.is_empty(),
"nothing refused it -- that is the point: {:?}",
write.refused_paths
);
}
#[test]
fn without_the_adoption_the_identical_overwriting_write_refuses_and_the_seed_survives() {
let dir = tempfile::tempdir().expect("tempdir");
let base = dir.path();
let (full, files) = license_fixture(base);
let write = crate::cli::pipeline::write_scaffold_files_report(&files, base, true).expect("scaffold write");
assert_eq!(
std::fs::read_to_string(&full).expect("read after"),
HAND_WRITTEN_LICENSE,
"with no ownership record the guard is the only thing protecting this file, and it must hold"
);
assert!(
write.refused_paths.contains(&full),
"the refusal must be reported, got: {:?}",
write.refused_paths
);
assert!(write.changed_paths.is_empty());
}
#[test]
fn an_ordinary_non_overwriting_write_leaves_the_adopted_seed_alone() {
let dir = tempfile::tempdir().expect("tempdir");
let base = dir.path();
let (full, files) = license_fixture(base);
let outputs = managed_outputs(&files, base);
run(&clobbering_seeds(base, "LICENSE"), &outputs).expect("adopt");
let write = crate::cli::pipeline::write_scaffold_files_report(&files, base, false).expect("scaffold write");
assert_eq!(
std::fs::read_to_string(&full).expect("read after"),
HAND_WRITTEN_LICENSE,
"a non-overwriting write skips a create-once seed whether or not it was adopted"
);
assert!(write.changed_paths.is_empty());
assert!(
write.refused_paths.is_empty(),
"skipped, not refused: the guard is never even reached"
);
}
#[test]
fn an_unmarkable_seed_has_no_route_to_ownership_other_than_the_licence_the_write_guard_reads() {
let dir = tempfile::tempdir().expect("tempdir");
let base = dir.path();
let (full, _files) = license_fixture(base);
assert!(
crate::cli::pipeline::marker_comment_style(&full).is_none(),
"fixture must be genuinely unmarkable, or the marker rail protects it regardless"
);
assert!(
!crate::cli::pipeline::is_owned_by_ownership_record(base, &full),
"no ownership before adoption"
);
assert!(
crate::cli::pipeline::stamp_for_adoption(&full, HAND_WRITTEN_LICENSE).is_none(),
"nothing can be stamped into it, so the record is the whole of the adoption"
);
}