every_shipped_document_loads walks the index, not a .gitignore reading (improvise-c0aj)

This commit is contained in:
Edward Langley
2026-09-23 08:56:26 -07:00
parent 4e52d06838
commit 6dceaad1c7
2 changed files with 97 additions and 78 deletions
+50 -52
View File
@@ -1,71 +1,52 @@
//! Every `.improv` document this repository ships must load (improvise-odbc).
//! Why a DIRECTORY WALK and not a list: `context/test-evidence/tui.md`.
//! "Shipped" is "tracked", read off git's index: `context/test-evidence/tui.md`.
use std::path::{Path, PathBuf};
/// The directories `.gitignore` un-ignores `*.improv` in: the definition of
/// "shipped", read from the one place that states it (improvise-qlrh).
fn shipped_directories(root: &Path) -> Vec<PathBuf> {
let gitignore = std::fs::read_to_string(root.join(".gitignore")).expect("a .gitignore");
gitignore
.lines()
.filter_map(|line| line.trim().strip_prefix('!'))
.filter_map(|pattern| pattern.strip_suffix("/*.improv"))
.map(|dir| root.join(dir))
.collect()
}
fn documents_in(dir: &Path) -> Vec<PathBuf> {
let mut found: Vec<PathBuf> = std::fs::read_dir(dir)
.expect("a shipped-document directory")
.map(|entry| entry.expect("a readable directory entry").path())
// `extension()` of `x.improv.gz` is `gz`, so matching on the extension
// alone would skip it and put back the hole this test closes.
.filter(|p| {
p.to_str()
.is_some_and(|s| s.ends_with(".improv") || s.ends_with(".improv.gz"))
})
/// Every tracked `.improv` and `.improv.gz` under `root`. Outside a checkout this
/// panics rather than answering empty (improvise-c0aj).
fn shipped_documents(root: &Path) -> Vec<PathBuf> {
let out = std::process::Command::new("git")
.arg("-C")
.arg(root)
.args(["ls-files", "-z", "--", "*.improv", "*.improv.gz"])
.output()
.expect("git runs: this test reads the index, so it needs a checkout");
assert!(
out.status.success(),
"git ls-files failed in {}: {}",
root.display(),
String::from_utf8_lossy(&out.stderr)
);
let mut found: Vec<PathBuf> = String::from_utf8(out.stdout)
.expect("UTF-8 paths")
.split('\0')
.filter(|p| !p.is_empty())
.map(|p| root.join(p))
.collect();
found.sort();
found
}
/// Every shipped document under `root`: the walk the test below loads.
fn shipped_documents(root: &Path) -> Vec<PathBuf> {
shipped_directories(root)
.iter()
.flat_map(|dir| documents_in(dir))
.collect()
}
#[test]
fn every_shipped_document_loads() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let dirs = shipped_directories(root);
let documents = shipped_documents(root);
assert!(
!dirs.is_empty(),
"precondition: .gitignore must un-ignore at least one *.improv directory"
!documents.is_empty(),
"precondition: the index must list at least one shipped document"
);
for dir in dirs {
let documents = documents_in(&dir);
// Per directory, so a walk that silently loses one cannot pass (qlrh).
for path in documents {
let workbook = improvise::persistence::load(&path)
.unwrap_or_else(|e| panic!("{} does not load: {e}", path.display()));
assert!(
!documents.is_empty(),
"precondition: {} is un-ignored for *.improv but the walk found none there",
dir.display()
workbook.views.contains_key(&workbook.active_view),
"{} loads but its initial view {:?} is not one of {:?}",
path.display(),
workbook.active_view,
workbook.views.keys().collect::<Vec<_>>()
);
for path in documents {
let workbook = improvise::persistence::load(&path)
.unwrap_or_else(|e| panic!("{} does not load: {e}", path.display()));
assert!(
workbook.views.contains_key(&workbook.active_view),
"{} loads but its initial view {:?} is not one of {:?}",
path.display(),
workbook.active_view,
workbook.views.keys().collect::<Vec<_>>()
);
}
}
}
@@ -125,10 +106,27 @@ fn a_document_un_ignored_by_a_nested_gitignore_is_walked() {
write(root, ".gitignore", "*.improv\n");
write(root, "harness/.gitignore", "!keep.improv\n");
write(root, "harness/keep.improv", "x");
run_git(root, &["add", ".gitignore", "harness/.gitignore", "harness/keep.improv"]);
run_git(
root,
&[
"add",
".gitignore",
"harness/.gitignore",
"harness/keep.improv",
],
);
assert!(walked(root, "harness/keep.improv"));
}
/// An empty walk would pass every per-document assertion vacuously, so a
/// directory git cannot read must stop the test instead.
#[test]
#[should_panic(expected = "git ls-files failed")]
fn a_directory_that_is_not_a_checkout_fails_loudly() {
let not_a_repo = tempfile::tempdir().expect("a temp dir");
shipped_documents(not_a_repo.path());
}
#[test]
fn a_force_added_document_is_walked() {
let repo = scratch_repo();