use super::*;
use tempfile::TempDir;
fn write(path: &Path, body: &str) {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, body).unwrap();
}
#[test]
fn reports_a_file_the_new_version_does_not_have() {
let tmp = TempDir::new().unwrap();
let current = tmp.path().join("current");
let replacement = tmp.path().join("new");
write(¤t.join("SKILL.md"), "v1");
write(¤t.join("templates/default.pptx"), "upstream");
write(¤t.join("templates/mine.pptx"), "user work");
write(&replacement.join("SKILL.md"), "v2");
write(&replacement.join("templates/default.pptx"), "upstream v2");
assert_eq!(
removed_paths(¤t, &replacement).unwrap(),
vec!["templates/mine.pptx"]
);
}
#[test]
fn says_nothing_when_every_path_survives() {
let tmp = TempDir::new().unwrap();
let current = tmp.path().join("current");
let replacement = tmp.path().join("new");
write(¤t.join("SKILL.md"), "v1");
write(¤t.join("scripts/run.py"), "old");
write(&replacement.join("SKILL.md"), "v2 — rewritten");
write(&replacement.join("scripts/run.py"), "new");
write(&replacement.join("scripts/extra.py"), "added upstream");
assert!(removed_paths(¤t, &replacement).unwrap().is_empty());
}
#[test]
fn rolls_up_a_directory_the_new_version_drops_entirely() {
let tmp = TempDir::new().unwrap();
let current = tmp.path().join("current");
let replacement = tmp.path().join("new");
write(¤t.join("SKILL.md"), "v1");
write(¤t.join(".git/objects/aa/bb"), "x");
write(¤t.join(".git/HEAD"), "ref");
write(¤t.join("templates/a.pptx"), "x");
write(¤t.join("templates/b.pptx"), "x");
write(&replacement.join("SKILL.md"), "v2");
assert_eq!(
removed_paths(¤t, &replacement).unwrap(),
vec![".git/", "templates/"]
);
}
#[test]
fn skips_regenerable_artifacts_but_not_dotfiles_in_general() {
let tmp = TempDir::new().unwrap();
let current = tmp.path().join("current");
let replacement = tmp.path().join("new");
write(¤t.join("SKILL.md"), "v1");
write(¤t.join("scripts/__pycache__/m.cpython-311.pyc"), "x");
write(¤t.join("stray.pyc"), "x");
write(¤t.join(".DS_Store"), "x");
write(¤t.join(".gitignore"), "mine");
write(&replacement.join("SKILL.md"), "v2");
assert_eq!(
removed_paths(¤t, &replacement).unwrap(),
vec![".gitignore", "scripts/"]
);
}
#[test]
fn a_path_that_changes_shape_counts_as_removed() {
let tmp = TempDir::new().unwrap();
let current = tmp.path().join("current");
let replacement = tmp.path().join("new");
write(¤t.join("thing"), "a file the user edited");
write(¤t.join("other/inner.txt"), "x");
write(&replacement.join("thing/inner.txt"), "now a directory");
write(&replacement.join("other"), "now a file");
assert_eq!(
removed_paths(¤t, &replacement).unwrap(),
vec!["other/", "thing"]
);
}
#[test]
fn an_absent_current_directory_reports_nothing() {
let tmp = TempDir::new().unwrap();
assert!(removed_paths(&tmp.path().join("gone"), tmp.path())
.unwrap()
.is_empty());
}
#[test]
fn never_answers_nothing_will_be_lost_when_a_path_cannot_be_classified() {
let tmp = TempDir::new().unwrap();
let current = tmp.path().join("current");
write(¤t.join("SKILL.md"), "v1");
let replacement = tmp.path().join("not-a-directory");
write(&replacement, "this is a file");
match removed_paths(¤t, &replacement) {
Err(err) => assert!(
format!("{err:#}").contains("survives the update"),
"refused, but not for the reason expected: {err:#}"
),
Ok(reported) => assert!(
reported.iter().any(|p| p == "SKILL.md"),
"an unclassifiable path was reported as safe: {reported:?}"
),
}
}
#[cfg(unix)]
#[test]
fn a_symlink_the_new_version_lacks_is_reported() {
let tmp = TempDir::new().unwrap();
let current = tmp.path().join("current");
let replacement = tmp.path().join("new");
std::fs::create_dir_all(¤t).unwrap();
std::fs::create_dir_all(&replacement).unwrap();
std::os::unix::fs::symlink("/somewhere", current.join("link")).unwrap();
assert_eq!(removed_paths(¤t, &replacement).unwrap(), vec!["link"]);
}