tests: every test dir is removed when its test ends, pass or panic

Many test helpers removed their temp dir only before the test, or only
on pass, so runs left them behind: 117,000 filled /tmp (RAM here) and
stopped a mutation run. Each now holds a guard that removes the dir on
drop, unwind included; servers started on a test dir hold its guard
until they stop. A full cargo test --workspace leaves 0 dirs (859
before). just sweep-test-tmp stays for runs killed mid-test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T3gXMVt7XJPi6ens1or9Dj
This commit is contained in:
swaitsandClaude Opus 5.5 committed 2026-10-17 20:47:44 -06:00
1 parent b292a505b1
commit a833229d87
44 files changed
+952 -163

No files matched your search

+7
View File
@@ -76,6 +76,13 @@ struct Places {
sandbox: PathBuf,
}
/// The places go when the test ends, pass or panic.
impl Drop for Places {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.base);
}
}
fn places(tag: &str) -> Places {
let base =
std::env::temp_dir().join(format!("ledger-mcp-hostile-{}-{tag}", std::process::id()));
@@ -28,6 +28,15 @@ impl Drop for Server {
}
}
/// A test's dir, removed when the test ends, pass or panic.
struct TestDir(std::path::PathBuf);
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn free_port() -> u16 {
std::net::TcpListener::bind("127.0.0.1:0")
.unwrap()
@@ -123,6 +132,8 @@ async fn agents_posting_at_once_leave_whole_books() {
let dir = std::env::temp_dir().join(format!("ledger-mcp-http-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
// Declared before the server, so dropped after it: pass or panic.
let _cleanup = TestDir(dir.clone());
let toml = dir.join("wanderbooks.toml");
std::fs::write(&toml, "[books]\nreceipt_threshold = \"0.00\"\n").unwrap();
let data = dir.join("books-data");
+30 -2
View File
@@ -126,11 +126,39 @@ impl Mcp {
}
}
fn test_dir(name: &str) -> std::path::PathBuf {
/// A test's dir and what sits beside it (`.inbox`, `.elsewhere.txt`),
/// removed when the test ends, pass or panic.
struct TestDir(std::path::PathBuf);
impl std::ops::Deref for TestDir {
type Target = std::path::Path;
fn deref(&self) -> &std::path::Path {
&self.0
}
}
impl AsRef<std::path::Path> for TestDir {
fn as_ref(&self) -> &std::path::Path {
&self.0
}
}
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
for beside in [".inbox", ".elsewhere.txt"] {
let p = std::path::PathBuf::from(format!("{}{beside}", self.0.display()));
let _ = std::fs::remove_dir_all(&p);
let _ = std::fs::remove_file(&p);
}
}
}
fn test_dir(name: &str) -> TestDir {
let dir = std::env::temp_dir().join(format!("ledger-mcp-test-{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
TestDir(dir)
}
#[test]
+29 -14
View File
@@ -56,7 +56,31 @@ impl Drop for Mcp {
static DIR_SEQ: AtomicU64 = AtomicU64::new(0);
fn test_dir(name: &str) -> PathBuf {
/// A test's dir and the folder beside it (`.inbox`),
/// removed when the test ends, pass or panic.
struct TestDir(PathBuf);
impl std::ops::Deref for TestDir {
type Target = std::path::Path;
fn deref(&self) -> &std::path::Path {
&self.0
}
}
impl AsRef<std::path::Path> for TestDir {
fn as_ref(&self) -> &std::path::Path {
&self.0
}
}
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
let _ = std::fs::remove_dir_all(format!("{}.inbox", self.0.display()));
}
}
fn test_dir(name: &str) -> TestDir {
let n = DIR_SEQ.fetch_add(1, Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!(
"ledger-mcp-oracle-{}-{name}-{n}",
@@ -64,7 +88,7 @@ fn test_dir(name: &str) -> PathBuf {
));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
TestDir(dir)
}
/// `wanderbooks.toml` for two 50/50 members on 3000/3001, cash 1000,
@@ -2249,19 +2273,10 @@ fn receipts_and_documents_round_trip() {
#[test]
fn uploads_up_to_25_mib_are_accepted_and_no_more() {
const LIMIT: usize = 25 * 1024 * 1024;
// Over 50 MB of books: removed however the test ends, with its inbox
// (the system temp dir is often RAM, and a mutation run starts this
// test hundreds of times).
let dir = test_dir("limits");
// Over 50 MB of books: removed however the test ends (the system temp
// dir is often RAM, and a mutation run starts this test hundreds of
// times).
struct Cleanup(Vec<PathBuf>);
impl Drop for Cleanup {
fn drop(&mut self) {
for d in &self.0 {
let _ = std::fs::remove_dir_all(d);
}
}
}
let _cleanup = Cleanup(vec![dir.clone(), inbox(&dir)]);
let s = new_store(&dir);
drop(s);
let mut m = Mcp::start(&dir, "1000000.00");
+29 -2
View File
@@ -49,6 +49,9 @@ struct Mcp {
next_id: u64,
data: PathBuf,
validators: HashMap<String, jsonschema::Validator>,
/// The books dir when the server owns it: removed after the server
/// stops (fields drop after `Drop::drop`).
books_dir: Option<TestDir>,
}
impl Drop for Mcp {
@@ -60,7 +63,29 @@ impl Drop for Mcp {
static DIR_SEQ: AtomicU64 = AtomicU64::new(0);
fn test_dir(name: &str) -> PathBuf {
/// A test's dir, removed when the test ends, pass or panic.
struct TestDir(PathBuf);
impl std::ops::Deref for TestDir {
type Target = std::path::Path;
fn deref(&self) -> &std::path::Path {
&self.0
}
}
impl AsRef<std::path::Path> for TestDir {
fn as_ref(&self) -> &std::path::Path {
&self.0
}
}
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn test_dir(name: &str) -> TestDir {
let n = DIR_SEQ.fetch_add(1, Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!(
"ledger-mcp-spec-assets-{}-{name}-{n}",
@@ -68,7 +93,7 @@ fn test_dir(name: &str) -> PathBuf {
));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
TestDir(dir)
}
/// `wanderbooks.toml` with a company name and extra `[books]` TOML. The
@@ -113,6 +138,7 @@ impl Mcp {
}
});
let mut mcp = Self {
books_dir: None,
stdin,
rx,
child,
@@ -356,6 +382,7 @@ fn books(name: &str) -> Mcp {
let toml = books_toml(&dir, "Spec Co", "asset_tag_prefix = \"WL\"\n");
let mut m = Mcp::start(&dir, &toml);
m.accounts(CHART);
m.books_dir = Some(dir);
m
}
+31 -2
View File
@@ -52,6 +52,9 @@ struct Mcp {
validators: HashMap<String, jsonschema::Validator>,
/// Every response line the server wrote, with the tool it answered.
transcript: Vec<(String, String)>,
/// The books dir when the server owns it: removed after the server
/// stops (fields drop after `Drop::drop`).
books_dir: Option<TestDir>,
}
impl Drop for Mcp {
@@ -63,7 +66,31 @@ impl Drop for Mcp {
static DIR_SEQ: AtomicU64 = AtomicU64::new(0);
fn test_dir(name: &str) -> PathBuf {
/// A test's dir and the folder beside it (`-outside`),
/// removed when the test ends, pass or panic.
struct TestDir(PathBuf);
impl std::ops::Deref for TestDir {
type Target = std::path::Path;
fn deref(&self) -> &std::path::Path {
&self.0
}
}
impl AsRef<std::path::Path> for TestDir {
fn as_ref(&self) -> &std::path::Path {
&self.0
}
}
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
let _ = std::fs::remove_dir_all(format!("{}-outside", self.0.display()));
}
}
fn test_dir(name: &str) -> TestDir {
let n = DIR_SEQ.fetch_add(1, Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!(
"ledger-mcp-spec-attachments-{}-{name}-{n}",
@@ -71,7 +98,7 @@ fn test_dir(name: &str) -> PathBuf {
));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
TestDir(dir)
}
/// A directory beside the books dir `dir`, outside it: the vault key,
@@ -131,6 +158,7 @@ impl Mcp {
}
});
let mut mcp = Self {
books_dir: None,
stdin,
rx,
child,
@@ -363,6 +391,7 @@ fn books_at(name: &str, threshold: &str, extra: &str) -> Mcp {
let toml = books_toml(&dir, threshold, extra);
let mut m = Mcp::start(&dir, &toml);
m.accounts(CHART);
m.books_dir = Some(dir);
m
}
+24 -2
View File
@@ -112,11 +112,33 @@ impl Mcp {
}
}
fn test_dir(name: &str) -> std::path::PathBuf {
/// A test's dir, removed when the test ends, pass or panic.
struct TestDir(std::path::PathBuf);
impl std::ops::Deref for TestDir {
type Target = std::path::Path;
fn deref(&self) -> &std::path::Path {
&self.0
}
}
impl AsRef<std::path::Path> for TestDir {
fn as_ref(&self) -> &std::path::Path {
&self.0
}
}
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn test_dir(name: &str) -> TestDir {
let dir = std::env::temp_dir().join(format!("ledger-mcp-test-{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
TestDir(dir)
}
fn init(m: &mut Mcp) -> String {
+36 -3
View File
@@ -159,6 +159,9 @@ struct Mcp {
read_only_checked: BTreeSet<String>,
/// Invariant 3 (amounts out) violations: `tool: path = value`.
money_violations: Vec<String>,
/// The books dir when the server owns it: removed after the server
/// stops (fields drop after `Drop::drop`).
books_dir: Option<TestDir>,
}
impl Drop for Mcp {
@@ -170,14 +173,42 @@ impl Drop for Mcp {
static DIR_SEQ: AtomicU64 = AtomicU64::new(0);
/// A test's dir and the folders beside it (`.inbox`, `-outside`),
/// removed when the test ends, pass or panic.
struct TestDir(PathBuf);
impl std::ops::Deref for TestDir {
type Target = std::path::Path;
fn deref(&self) -> &std::path::Path {
&self.0
}
}
impl AsRef<std::path::Path> for TestDir {
fn as_ref(&self) -> &std::path::Path {
&self.0
}
}
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
for beside in [".inbox", "-outside"] {
let p = PathBuf::from(format!("{}{beside}", self.0.display()));
let _ = std::fs::remove_dir_all(&p);
let _ = std::fs::remove_file(&p);
}
}
}
/// A fresh, empty per-test directory under the system temp dir.
fn test_dir(name: &str) -> PathBuf {
fn test_dir(name: &str) -> TestDir {
let n = DIR_SEQ.fetch_add(1, Ordering::Relaxed);
let dir =
std::env::temp_dir().join(format!("ledger-mcp-spec-{}-{name}-{n}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
TestDir(dir)
}
/// A directory beside the books dir `dir`, outside it (vault keys, files
@@ -262,6 +293,7 @@ impl Mcp {
}
});
let mut mcp = Self {
books_dir: None,
stdin,
rx,
child,
@@ -1247,6 +1279,7 @@ fn happy_path(name: &str) -> Mcp {
"save_to": outside(&dir).join("cpa.zip").to_str().unwrap()}),
);
m.books_dir = Some(dir);
m
}
@@ -1692,7 +1725,7 @@ const REFUSED_AMOUNTS: &[&str] = &[
/// the core is expected to accept on a line.
const ACCEPTED_AMOUNTS: &[&str] = &["5", "5.00", "123.45", "5.5", "007.50", "1000000.00"];
fn amounts_server(name: &str) -> (PathBuf, Mcp, u64) {
fn amounts_server(name: &str) -> (TestDir, Mcp, u64) {
let dir = test_dir(name);
let toml = books_toml(&dir, "25.00", "");
let mut m = Mcp::start(&dir, &toml);
+31 -2
View File
@@ -50,6 +50,9 @@ struct Mcp {
next_id: u64,
data: PathBuf,
validators: HashMap<String, jsonschema::Validator>,
/// The books dir when the server owns it: removed after the server
/// stops (fields drop after `Drop::drop`).
books_dir: Option<TestDir>,
}
impl Drop for Mcp {
@@ -61,7 +64,29 @@ impl Drop for Mcp {
static DIR_SEQ: AtomicU64 = AtomicU64::new(0);
fn test_dir(name: &str) -> PathBuf {
/// A test's dir, removed when the test ends, pass or panic.
struct TestDir(PathBuf);
impl std::ops::Deref for TestDir {
type Target = std::path::Path;
fn deref(&self) -> &std::path::Path {
&self.0
}
}
impl AsRef<std::path::Path> for TestDir {
fn as_ref(&self) -> &std::path::Path {
&self.0
}
}
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn test_dir(name: &str) -> TestDir {
let n = DIR_SEQ.fetch_add(1, Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!(
"ledger-mcp-spec-parties-{}-{name}-{n}",
@@ -69,7 +94,7 @@ fn test_dir(name: &str) -> PathBuf {
));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
TestDir(dir)
}
/// `wanderbooks.toml` with a company name and extra `[books]` TOML. The
@@ -114,6 +139,7 @@ impl Mcp {
}
});
let mut mcp = Self {
books_dir: None,
stdin,
rx,
child,
@@ -339,6 +365,7 @@ fn books(name: &str) -> Mcp {
let toml = books_toml(&dir, "Spec Co", "");
let mut m = Mcp::start(&dir, &toml);
m.accounts(CHART);
m.books_dir = Some(dir);
m
}
@@ -1161,6 +1188,7 @@ fn books_with(name: &str, extra: &str) -> Mcp {
let toml = books_toml(&dir, "Spec Co", extra);
let mut m = Mcp::start(&dir, &toml);
m.accounts(CHART);
m.books_dir = Some(dir);
m
}
@@ -1343,6 +1371,7 @@ fn member_books(name: &str, extra: &str) -> Mcp {
m.accounts(&[("3001", "Capital K", "equity")]);
m.add_party("Stephen Waits", &["member"]);
m.add_party("Katherine Pendergast", &["member"]);
m.books_dir = Some(dir);
m
}
+35 -2
View File
@@ -58,6 +58,9 @@ struct Mcp {
validators: HashMap<String, jsonschema::Validator>,
/// Every response line the server wrote, with the tool it answered.
transcript: Vec<(String, String)>,
/// The books dir when the server owns it: removed after the server
/// stops (fields drop after `Drop::drop`).
books_dir: Option<TestDir>,
}
impl Drop for Mcp {
@@ -69,7 +72,35 @@ impl Drop for Mcp {
static DIR_SEQ: AtomicU64 = AtomicU64::new(0);
fn test_dir(name: &str) -> PathBuf {
/// A test's dir and the folders beside it (`-inbox`, `-outside`),
/// removed when the test ends, pass or panic.
struct TestDir(PathBuf);
impl std::ops::Deref for TestDir {
type Target = std::path::Path;
fn deref(&self) -> &std::path::Path {
&self.0
}
}
impl AsRef<std::path::Path> for TestDir {
fn as_ref(&self) -> &std::path::Path {
&self.0
}
}
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
for beside in ["-inbox", "-outside"] {
let p = PathBuf::from(format!("{}{beside}", self.0.display()));
let _ = std::fs::remove_dir_all(&p);
let _ = std::fs::remove_file(&p);
}
}
}
fn test_dir(name: &str) -> TestDir {
let n = DIR_SEQ.fetch_add(1, Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!(
"ledger-mcp-spec-vault-{}-{name}-{n}",
@@ -77,7 +108,7 @@ fn test_dir(name: &str) -> PathBuf {
));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
TestDir(dir)
}
/// A directory beside the books dir `dir`, outside it: the vault key,
@@ -164,6 +195,7 @@ impl Mcp {
}
});
let mut mcp = Self {
books_dir: None,
stdin,
rx,
child,
@@ -402,6 +434,7 @@ fn books_with(name: &str, extra: &str) -> Mcp {
json!({"vendor": "Acme Design", "form_1099": "nec"}),
);
m.add_party("Rent Co", &["landlord"]);
m.books_dir = Some(dir);
m
}
+36 -5
View File
@@ -7,11 +7,39 @@ use std::time::{Duration, Instant};
const BIN: &str = env!("CARGO_BIN_EXE_ledger-mcp");
fn temp(tag: &str) -> std::path::PathBuf {
/// A test's dir, removed when the test ends, pass or panic.
struct TestDir(std::path::PathBuf);
impl std::ops::Deref for TestDir {
type Target = std::path::Path;
fn deref(&self) -> &std::path::Path {
&self.0
}
}
impl AsRef<std::path::Path> for TestDir {
fn as_ref(&self) -> &std::path::Path {
&self.0
}
}
impl AsRef<std::ffi::OsStr> for TestDir {
fn as_ref(&self) -> &std::ffi::OsStr {
self.0.as_os_str()
}
}
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn temp(tag: &str) -> TestDir {
let d = std::env::temp_dir().join(format!("ledger-mcp-start-{}-{tag}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
d
TestDir(d)
}
/// Start the server, initialize, call one tool, and return its text
@@ -236,7 +264,8 @@ fn refuses_to_start(cmd: &mut Command) -> Option<String> {
/// used.
#[test]
fn an_import_root_holding_the_books_or_the_home_folder_is_refused() {
let dir = temp("bad-roots").canonicalize().unwrap();
let tmp = temp("bad-roots");
let dir = tmp.canonicalize().unwrap();
let home = dir.join("home");
let books = home.join("work/books");
let data = books.join("books-data");
@@ -316,7 +345,8 @@ fn an_import_root_holding_the_books_or_the_home_folder_is_refused() {
/// is the root, and the rest of the home folder stays out of reach.
#[test]
fn a_download_dir_of_home_never_opens_the_home_folder() {
let dir = temp("xdg-home").canonicalize().unwrap();
let tmp = temp("xdg-home");
let dir = tmp.canonicalize().unwrap();
let home = dir.join("home");
std::fs::create_dir_all(home.join(".ssh")).unwrap();
std::fs::write(home.join(".ssh/id_ed25519"), "PRIVATE KEY").unwrap();
@@ -358,7 +388,8 @@ fn a_download_dir_of_home_never_opens_the_home_folder() {
/// folder, and an import says so.
#[test]
fn a_default_download_dir_holding_the_books_is_dropped_not_fatal() {
let dir = temp("xdg-books").canonicalize().unwrap();
let tmp = temp("xdg-books");
let dir = tmp.canonicalize().unwrap();
let home = dir.join("home");
let downloads = dir.join("downloads");
std::fs::create_dir_all(&home).unwrap();
+10
View File
@@ -68,6 +68,16 @@ struct Books {
toml: PathBuf,
}
/// The books dir and the folders beside it go when the test ends, pass or
/// panic.
impl Drop for Books {
fn drop(&mut self) {
if let Some(base) = self.dir.parent() {
let _ = std::fs::remove_dir_all(base);
}
}
}
fn books(tag: &str) -> Books {
let base =
std::env::temp_dir().join(format!("ledger-mcp-tin-files-{}-{tag}", std::process::id()));