format: test the build-script lists and newer-file warnings, report and restore

This commit is contained in:
FredApps committed 2026-11-16 00:42:00 +01:00
1 parent dfe9d6fe97
commit 9e4431f9fa
3 files changed
+120

No files matched your search

+4
View File
@@ -98,3 +98,7 @@ fn parts(dir: &Path, prefix: &str, out: &mut String, mods: &mut String) -> Resul
}
Ok(())
}
#[cfg(test)]
#[path = "build_tests.rs"]
mod tests;
+75
View File
@@ -0,0 +1,75 @@
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
/// A fresh, empty folder for one test.
fn scratch(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("craft-format-build-{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
fn write(path: &Path, text: &str) {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, text).unwrap();
}
/// The error of a run that must fail.
fn refused(dir: &Path) -> String {
match lists(dir, &[DRAWING]) {
Ok(_) => panic!("expected an error"),
Err(e) => e,
}
}
const DRAWING: Family = Family { folder: "drawing", base: 3, list_prefix: "DRAWING_" };
#[test]
fn lists_folded_and_pending_changes_with_their_modules() {
let dir = scratch("ok");
write(&dir.join("drawing/v4/add-layers.rs"), "//! Layers.\npub fn step(v: &mut Value) -> Result<(), E> { Ok(()) }\n");
write(&dir.join("drawing/v4/notes.txt"), "not a change");
write(&dir.join("drawing/v5/rename.rs"), "\n//! A rename with no step.\n");
write(&dir.join("drawing/pending/next-1.rs"), "//! Pending.\nfn step(v: &mut Value) {}\n");
std::fs::create_dir_all(dir.join("drawing/v6x")).unwrap();
write(&dir.join("drawing/v7"), "a file, not a version folder");
let lists = lists(&dir, &[DRAWING]).unwrap();
assert_eq!(lists.watch, vec![dir.join("drawing")]);
let code = &lists.code;
assert!(code.contains("const DRAWING_FOLDED: &[(u32, &[Part])] = &["), "{code}");
assert!(code.contains(" (4, &[\n Part { name: \"add-layers\", step: Some(drawing_v4_add_layers::step) },\n ]),"), "{code}");
assert!(code.contains("Part { name: \"rename\", step: None }"), "{code}");
assert!(
code.contains("const DRAWING_PENDING: &[Part] = &[\n Part { name: \"next-1\", step: Some(drawing_pending_next_1::step) },"),
"{code}"
);
assert!(code.contains("mod drawing_v5_rename;"), "{code}");
assert!(!code.contains("notes"), "{code}");
assert!(code.find("const DRAWING_PENDING").unwrap() < code.find("#[path").unwrap(), "modules follow the lists");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn an_empty_family_has_empty_lists() {
let dir = scratch("empty");
std::fs::create_dir_all(dir.join("drawing")).unwrap();
let code = lists(&dir, &[DRAWING]).unwrap().code;
assert!(code.contains("const DRAWING_FOLDED: &[(u32, &[Part])] = &[\n];"), "{code}");
assert!(code.contains("const DRAWING_PENDING: &[Part] = &[\n];"), "{code}");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn refuses_gaps_bad_names_missing_notes_and_missing_folders() {
let dir = scratch("bad");
assert!(refused(&dir).contains("drawing"));
write(&dir.join("drawing/v5/a.rs"), "//! a");
assert!(refused(&dir).contains("expected v4/ next, found v5/"));
std::fs::rename(dir.join("drawing/v5"), dir.join("drawing/v4")).unwrap();
write(&dir.join("drawing/v4/Bad_Name.rs"), "//! b");
assert!(refused(&dir).contains("lowercase letters"));
std::fs::remove_file(dir.join("drawing/v4/Bad_Name.rs")).unwrap();
write(&dir.join("drawing/pending/no-note.rs"), "pub fn step() {}");
assert!(refused(&dir).contains("`//!` note"));
let _ = std::fs::remove_dir_all(&dir);
}
+41
View File
@@ -46,3 +46,44 @@ fn unreadable_sections_are_left_out_and_kept() {
assert!(said.restore_into(&mut saved).is_empty());
assert_eq!(saved, json!({"name": "A", "good": 1, "bad": 2}));
}
/// Notices, warnings and reports name what was left out, and kept values can be searched.
#[test]
fn warnings_report_and_mentions_cover_everything_kept() {
let mut said = NewerFile::new("App", &Newer { format: 8, reads_up_to: 8, unknown_changes: vec!["curves".into()] });
assert_eq!(said.notice(), "Saved by a newer App (format 8 with `curves`, which this version doesn't know). If something's missing, update.");
assert!(!said.needs_container());
said.keep_section("plugins", json!({"tool": "ruler"}), Some(json!({})), "unknown");
said.keep_element("shapes", json!({"name": "star", "points": ["tip"]}), "shape `star`".into());
let file = json!({"shapes": [{"name": "a", "tags": ["deep"], "more": {"x": "nested"}}], "n": 1});
said.compare(&file, &json!({"shapes": [{"name": "a"}], "n": 1}), "placeholder", &[]);
assert_eq!(said.warnings()[0], said.notice());
assert!(said.warnings().contains(&"Not read: section `plugins` (unknown). Saving keeps it.".to_string()));
let report = said.report();
assert_eq!(report["format"], 8);
assert_eq!(report["unknown_changes"], json!(["curves"]));
assert_eq!(report["notice"], json!(said.notice()));
for text in ["ruler", "tip", "deep", "nested"] {
assert!(said.mentions(text), "{text}");
}
assert!(!said.mentions("absent"));
said.parts.push(("blob".into(), Arc::from(&b"x"[..])));
assert!(said.needs_container());
}
/// A kept section or element that was replaced here is reported lost, not written over.
#[test]
fn replaced_sections_and_elements_are_reported_lost() {
let mut said = NewerFile::new("App", &newer());
said.keep_section("plugins", json!({"tool": "ruler"}), None, "unknown");
said.keep_element("shapes", json!({"name": "star"}), "shape `star`".into());
said.keep_element("shapes", json!({"points": 3}), "an unnamed shape".into());
let mut saved = json!({"plugins": {"edited": true}, "shapes": [{"name": "star"}]});
let lost = said.restore_into(&mut saved);
assert_eq!(lost, ["section `plugins`, replaced by what was edited here", "`shapes` element star, replaced by what was made here"]);
assert_eq!(saved["shapes"], json!([{"name": "star"}, {"points": 3}]));
let mut fresh = json!({});
said.sections.clear();
assert!(said.restore_into(&mut fresh).is_empty());
assert_eq!(fresh["shapes"], json!([{"name": "star"}, {"points": 3}]));
}