use super::*;
fn gen_file(rel: &str, content: &str) -> crate::core::backend::GeneratedFile {
crate::core::backend::GeneratedFile {
path: std::path::PathBuf::from(rel),
content: content.to_string(),
generated_header: true,
}
}
fn gen_file_unheadered(rel: &str, content: &str) -> crate::core::backend::GeneratedFile {
crate::core::backend::GeneratedFile {
path: std::path::PathBuf::from(rel),
content: content.to_string(),
generated_header: false,
}
}
#[test]
fn drifted_marked_paths_reports_a_marked_file_whose_body_no_longer_matches() {
let dir = tempfile::tempdir().expect("tempdir");
let old_file = gen_file("lib.rs", "pub fn greet() -> &'static str { \"old\" }\n");
let old_rendered = crate::cli::commands::adopt::managed_outputs(std::slice::from_ref(&old_file), dir.path());
std::fs::write(dir.path().join("lib.rs"), &old_rendered[0].content).unwrap();
let files = vec![gen_file("lib.rs", "pub fn greet() -> &'static str { \"new\" }\n")];
let (drifted, _stats) = drifted_marked_paths(&files, dir.path());
assert_eq!(
drifted,
vec![dir.path().join("lib.rs").display().to_string()],
"a marked file whose source changed since it was last written must be reported drifted"
);
}
#[test]
fn drifted_marked_paths_is_silent_once_the_marked_file_matches_the_fresh_render() {
let dir = tempfile::tempdir().expect("tempdir");
let file = gen_file("lib.rs", "pub fn greet() -> &'static str { \"new\" }\n");
let rendered = crate::cli::commands::adopt::managed_outputs(std::slice::from_ref(&file), dir.path());
std::fs::write(dir.path().join("lib.rs"), &rendered[0].content).unwrap();
let files = vec![file];
let (drifted, _stats) = drifted_marked_paths(&files, dir.path());
assert!(drifted.is_empty());
}
#[test]
fn drifted_marked_paths_ignores_a_file_that_carries_no_marker_at_all() {
let dir = tempfile::tempdir().expect("tempdir");
std::fs::write(dir.path().join("lib.rs"), "pub fn greet() {}\n").unwrap();
let files = vec![gen_file("lib.rs", "pub fn greet() { /* changed */ }\n")];
let (drifted, _stats) = drifted_marked_paths(&files, dir.path());
assert!(drifted.is_empty());
}
#[test]
fn drifted_marked_paths_now_catches_a_toml_file_via_the_real_poly_fmt_pass() {
if !crate::cli::pipeline::is_tool_available("poly") {
return;
}
let dir = tempfile::tempdir().expect("tempdir");
let header = crate::core::hash::header(crate::core::hash::CommentStyle::Hash);
std::fs::write(
dir.path().join("pyproject.toml"),
format!("{header}\n[project]\nname = \"old\"\n"),
)
.unwrap();
let files = vec![gen_file("pyproject.toml", "[project]\nname=\"new\"\n")];
let (drifted, stats) = drifted_marked_paths(&files, dir.path());
assert_eq!(
drifted,
vec![dir.path().join("pyproject.toml").display().to_string()],
"a marked TOML file whose source changed must now be reported drifted via the real \
formatter pass, not silently predicted-and-missed"
);
assert_eq!(
stats.compared, 1,
"the real-formatter pass must have actually examined this file"
);
assert_eq!(stats.skipped_missing_formatter, 0);
}
#[test]
fn drifted_marked_paths_is_silent_once_a_toml_file_matches_the_real_formatters_output() {
if !crate::cli::pipeline::is_tool_available("poly") {
return;
}
let dir = tempfile::tempdir().expect("tempdir");
let header = crate::core::hash::header(crate::core::hash::CommentStyle::Hash);
let rendered_raw = "[project]\nname=\"same\"\n";
let scratch = dir.path().join("scratch.toml");
std::fs::write(&scratch, format!("{header}\n{rendered_raw}")).unwrap();
crate::cli::pipeline::poly_format_strict(std::slice::from_ref(&scratch), dir.path())
.expect("poly fmt must succeed");
let converged = std::fs::read_to_string(&scratch).unwrap();
std::fs::write(dir.path().join("pyproject.toml"), &converged).unwrap();
std::fs::remove_file(&scratch).ok();
let files = vec![gen_file("pyproject.toml", rendered_raw)];
let (drifted, stats) = drifted_marked_paths(&files, dir.path());
assert!(
drifted.is_empty(),
"an already up-to-date TOML file must not be reported drifted: {drifted:?}"
);
assert_eq!(stats.compared, 1);
}
#[test]
fn drifted_marked_paths_reports_a_self_marking_file_whose_body_no_longer_matches() {
let dir = tempfile::tempdir().expect("tempdir");
let header = crate::core::hash::header(crate::core::hash::CommentStyle::DoubleSlash);
std::fs::write(
dir.path().join("reference.md"),
format!("{header}Describes `greet`, which says hi.\n"),
)
.unwrap();
let files = vec![gen_file_unheadered(
"reference.md",
&format!("{header}Describes `greet`, which says hi warmly.\n"),
)];
let (drifted, _stats) = drifted_marked_paths(&files, dir.path());
assert_eq!(
drifted,
vec![dir.path().join("reference.md").display().to_string()],
"a self-marking file (marker baked into content, generated_header: false) whose body \
changed must still be reported drifted"
);
}
#[test]
fn drifted_marked_paths_is_silent_once_a_self_marking_file_matches() {
let dir = tempfile::tempdir().expect("tempdir");
let header = crate::core::hash::header(crate::core::hash::CommentStyle::DoubleSlash);
let content = format!("{header}Describes `greet`, which says hi warmly.\n");
std::fs::write(dir.path().join("reference.md"), &content).unwrap();
let files = vec![gen_file_unheadered("reference.md", &content)];
let (drifted, _stats) = drifted_marked_paths(&files, dir.path());
assert!(drifted.is_empty());
}