(b74dc5a) Remove a test's fixture directory with a guard, not a trailing line
Every unit test here built its board under the system temp directory, and the ones that remembered a `remove_dir_all` at the end only cleaned up when their assertions passed. Most had no such line at all. /tmp reached 18363 of these and filled the disk, which failed a link step and then a `git add` with "No space left on device": an error naming neither the tests nor what they had left behind. `util::Fixture` is a uuid-named directory whose `Drop` removes it, which is the only shape that also runs when an assertion fails, since unwinding runs destructors. It derefs to `Path` and implements `AsRef<Path>`, because the two reach different call sites: a parameter typed `&Path` is a coercion site and one bound by `impl AsRef<Path>` is not. So nothing changed at the ~160 call sites but the type, and the 85 trailing cleanup lines are gone. Five leaking prefixes, not the two the ticket names. `worklane-project-*` from `project_tree` and `worklane-bare-*` never cleaned up at all, and the four `worklane-count/blocked/stray/stamps-*` sites in `util.rs` cleaned up only on the passing path. `Fixture::child` is what covers `project_tree`, which needs the fixture to point at the project and remove the directory above it. `assert_fs::TempDir` is a dev-dependency already and was still not the answer: it names its directories `.tmpXXXXXX`, which says nothing about whose they are for the one case that matters, the one left on disk on purpose. Wrapping it to add a prefix and a printed path is more code than the guard. `WORKLANE_KEEP_FIXTURES=1` keeps them and prints each path, opt-in rather than kept-automatically-on-failure: the assertions here carry the state they are about, so a path printed on every failure would be noise on almost all of them. Read once when a fixture is made rather than at drop time, so the answer cannot change mid-test, and `Fixture::swept()` turns it off for the three tests that are about the guard itself and must not be failed by a reader's environment. Verified against a clean temp dir rather than against /tmp, which still holds the historical leak: `TMPDIR=/tmp/scratch cargo test` leaves zero directories behind, with the variable set and unset, 383 green either way. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
dd2b457bff
commit
07c783410c
+177
-16
@@ -3077,10 +3077,183 @@ pub fn by_status_order(status: Status, a: &TicketItem, b: &TicketItem) -> std::c
|
||||
by_stamp.then_with(|| by_priority_then_title(a, b))
|
||||
}
|
||||
|
||||
/// The environment variable that keeps a test's fixture directory on disk.
|
||||
///
|
||||
/// Opt-in rather than on-by-default-when-a-test-fails, because the assertions in this crate
|
||||
/// carry the state they are about: a failing board test prints the rendered rows, a failing
|
||||
/// listing test prints the listing. The path is what you want when you would rather poke at
|
||||
/// the files than read the message, which is the less common of the two.
|
||||
#[cfg(test)]
|
||||
pub const KEEP_FIXTURES: &str = "WORKLANE_KEEP_FIXTURES";
|
||||
|
||||
/// A fixture directory under the system temp directory that removes itself.
|
||||
///
|
||||
/// Every unit test in this crate builds its board or its status directory by hand under
|
||||
/// `std::env::temp_dir()`. The ones that remembered a `remove_dir_all` at the end of the
|
||||
/// test only cleaned up when their assertions passed, and most had no such line at all, so
|
||||
/// a full run left one directory per test behind. `/tmp` reached 18363 of them and filled
|
||||
/// the disk, which failed a link step and then a `git add` with "No space left on device" —
|
||||
/// an error that names neither the tests nor what they left behind. That is b74dc5a.
|
||||
///
|
||||
/// A `Drop` rather than a line at the end of each test, because that is the only shape that
|
||||
/// runs when an assertion fails, which is exactly when a fixture is most likely to be
|
||||
/// stranded: unwinding runs destructors, so the guard holds on the failing path without the
|
||||
/// test having to say anything.
|
||||
///
|
||||
/// `assert_fs::TempDir` is a dev-dependency already and is what the integration tests use,
|
||||
/// and it was still not the answer here. It names its directories `.tmpXXXXXX`, which says
|
||||
/// nothing about whose they are for the one case that matters — the one left on disk on
|
||||
/// purpose — and it has no opinion about naming the path when it keeps one. Wrapping it to
|
||||
/// add both is more code than this.
|
||||
///
|
||||
/// `Deref` to `Path`, so a fixture reads as the path it stands for at the call sites that
|
||||
/// already had one and nothing had to change but the type.
|
||||
#[cfg(test)]
|
||||
pub struct Fixture {
|
||||
/// What is removed: the directory this fixture created at the top.
|
||||
root: std::path::PathBuf,
|
||||
/// What the test works with, at or below the root. The two differ only for a fixture
|
||||
/// that needs a named directory inside a parent it can also throw away.
|
||||
path: std::path::PathBuf,
|
||||
/// Whether to leave the directory behind, read once here rather than at drop time.
|
||||
///
|
||||
/// At construction because the answer must not be able to change between a test
|
||||
/// starting and ending, and because it is what [`Fixture::swept`] can then turn off for
|
||||
/// the tests that are about the guard rather than about this flag.
|
||||
keep: bool,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl Fixture {
|
||||
/// A fresh directory, named after the family of tests that asked for it.
|
||||
///
|
||||
/// The prefix earns its place on the kept path: `worklane-tui-` says which suite left it
|
||||
/// where a random name would not, and the uuid is what makes two fixtures with one
|
||||
/// prefix two directories, so no test can reach another's.
|
||||
pub fn new(prefix: &str) -> Self {
|
||||
let root = std::env::temp_dir().join(format!("{prefix}-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&root).unwrap();
|
||||
|
||||
Fixture {
|
||||
path: root.clone(),
|
||||
root,
|
||||
keep: std::env::var_os(KEEP_FIXTURES).is_some(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The same fixture, swept up whatever the environment says.
|
||||
///
|
||||
/// For the tests below, which assert that the directory goes: they are about the guard
|
||||
/// and not about the flag, so `WORKLANE_KEEP_FIXTURES` in a reader's environment must
|
||||
/// not be able to fail them.
|
||||
pub fn swept(mut self) -> Self {
|
||||
self.keep = false;
|
||||
self
|
||||
}
|
||||
|
||||
/// The same fixture, pointing at a directory inside it.
|
||||
///
|
||||
/// For the tree a project test needs: `<root>/<project>/.worklane`, where the path the
|
||||
/// test hands to `projects` is the project and what has to be removed is the root above
|
||||
/// it. Consuming, so one directory never has two guards and therefore never two attempts
|
||||
/// to remove it.
|
||||
pub fn child(mut self, name: &str) -> Self {
|
||||
self.path = self.root.join(name);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl AsRef<std::path::Path> for Fixture {
|
||||
fn as_ref(&self) -> &std::path::Path {
|
||||
&self.path
|
||||
}
|
||||
}
|
||||
|
||||
/// Beside `AsRef`, because the two reach different call sites: a parameter bound by
|
||||
/// `impl AsRef<Path>` is not a coercion site, and a parameter typed `&Path` is.
|
||||
#[cfg(test)]
|
||||
impl std::ops::Deref for Fixture {
|
||||
type Target = std::path::Path;
|
||||
|
||||
fn deref(&self) -> &std::path::Path {
|
||||
&self.path
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl Drop for Fixture {
|
||||
fn drop(&mut self) {
|
||||
if self.keep {
|
||||
// Named, since a fixture kept and not findable is a fixture kept for nothing.
|
||||
eprintln!("kept fixture {}", self.root.display());
|
||||
return;
|
||||
}
|
||||
|
||||
// Best effort. A directory that will not go must not turn a passing test into a
|
||||
// failing one, and must not panic a second time out of a drop that is already
|
||||
// unwinding, which would lose the failure the test was reporting.
|
||||
std::fs::remove_dir_all(&self.root).ok();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The guard removes the directory when it goes out of scope, which is what makes the
|
||||
/// cleanup independent of the assertions passing: unwinding runs destructors, so a
|
||||
/// failing test drops its fixture on the way out exactly as a passing one does.
|
||||
#[test]
|
||||
fn a_fixture_removes_itself_when_its_guard_goes() {
|
||||
let path = {
|
||||
let dir = Fixture::new("worklane-guard").swept();
|
||||
fs::write(dir.join("a.md"), "body").unwrap();
|
||||
assert!(dir.exists(), "the fixture was not created");
|
||||
dir.to_path_buf()
|
||||
};
|
||||
|
||||
assert!(!path.exists(), "the fixture outlived its guard: {}", path.display());
|
||||
}
|
||||
|
||||
/// And on the failing path, which is the whole point: a line at the end of a test does
|
||||
/// not run when an assertion above it does not pass.
|
||||
#[test]
|
||||
fn a_fixture_removes_itself_when_the_test_panics() {
|
||||
// The default hook would print this deliberate panic and make a green run read as a
|
||||
// red one. Swapped for the length of the call and put straight back, which is as
|
||||
// narrow as a process-wide hook can be made.
|
||||
let hook = std::panic::take_hook();
|
||||
std::panic::set_hook(Box::new(|_| {}));
|
||||
let caught = std::panic::catch_unwind(|| {
|
||||
let dir = Fixture::new("worklane-guard").swept();
|
||||
// Handed out through the payload, so the assertion below has a path to look at
|
||||
// whether or not the guard did its job.
|
||||
std::panic::panic_any(dir.to_path_buf());
|
||||
});
|
||||
std::panic::set_hook(hook);
|
||||
|
||||
let path = *caught
|
||||
.expect_err("the closure did not panic")
|
||||
.downcast::<std::path::PathBuf>()
|
||||
.expect("the panic did not carry the fixture path");
|
||||
|
||||
assert!(!path.exists(), "the fixture survived a failing test: {}", path.display());
|
||||
}
|
||||
|
||||
/// Two fixtures with one prefix are two directories, so no test can rely on one another
|
||||
/// test built — which the shared temp directory is what made possible.
|
||||
#[test]
|
||||
fn two_fixtures_with_one_prefix_are_two_directories() {
|
||||
let first = Fixture::new("worklane-guard");
|
||||
let second = Fixture::new("worklane-guard");
|
||||
assert_ne!(first.to_path_buf(), second.to_path_buf(), "one directory, two guards");
|
||||
|
||||
let kept = second.to_path_buf();
|
||||
drop(first);
|
||||
assert!(kept.exists(), "one fixture going took the other with it");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_color_disables_colour_on_a_terminal() {
|
||||
assert!(!colors_wanted(true, Some("1"), Some("xterm-256color")));
|
||||
@@ -3454,8 +3627,7 @@ mod tests {
|
||||
/// in `show`, so the two cannot disagree about what a criterion is.
|
||||
#[test]
|
||||
fn collect_ticket_items_counts_the_criteria_show_would_number() {
|
||||
let dir = std::env::temp_dir().join(format!("worklane-count-{}", uuid::Uuid::new_v4()));
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
let dir = Fixture::new("worklane-count");
|
||||
|
||||
let write = |name: &str, body: &str| {
|
||||
fs::write(
|
||||
@@ -3480,8 +3652,6 @@ mod tests {
|
||||
counted.contains(&None),
|
||||
"a ticket with no criteria got a count: {counted:?}"
|
||||
);
|
||||
|
||||
fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
/// The count is a directory listing rather than a read: nothing here parses a comment,
|
||||
@@ -3537,8 +3707,7 @@ mod tests {
|
||||
/// listing that stops on one malformed ticket is the worse failure.
|
||||
#[test]
|
||||
fn collect_ticket_items_reads_the_uuid_and_the_blockers_it_records() {
|
||||
let dir = std::env::temp_dir().join(format!("worklane-blocked-{}", uuid::Uuid::new_v4()));
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
let dir = Fixture::new("worklane-blocked");
|
||||
|
||||
let uuid = |tail: &str| format!("11111111-0000-4000-8000-00000000000{tail}");
|
||||
let write = |name: &str, relations: &str| {
|
||||
@@ -3563,8 +3732,6 @@ mod tests {
|
||||
assert_eq!(of("T1").blockers, vec![uuid("2")]);
|
||||
assert!(of("T2").blockers.is_empty(), "a regression was read as a blocker");
|
||||
assert!(of("T3").blockers.is_empty(), "an unreadable section was not survived");
|
||||
|
||||
fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
/// Whole values, so a filter or a card cannot confuse two tags that share a prefix.
|
||||
@@ -3591,8 +3758,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn collect_ticket_items_lists_only_md_files() {
|
||||
let dir = std::env::temp_dir().join(format!("worklane-stray-{}", uuid::Uuid::new_v4()));
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
let dir = Fixture::new("worklane-stray");
|
||||
|
||||
fs::write(
|
||||
dir.join("11111111-0000-4000-8000-000000000001.md"),
|
||||
@@ -3608,8 +3774,6 @@ mod tests {
|
||||
let titles: Vec<&str> = items.iter().map(|item| item.title.as_str()).collect();
|
||||
|
||||
assert_eq!(titles, vec!["Real"], "{titles:?}");
|
||||
|
||||
fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
/// The template ticket, and the shape every insertion test measures against.
|
||||
@@ -3927,8 +4091,7 @@ mod tests {
|
||||
/// comparison time, which is what makes an ordered column cost one read per file.
|
||||
#[test]
|
||||
fn collect_ticket_items_reads_the_stamps_the_order_sorts_on() {
|
||||
let dir = std::env::temp_dir().join(format!("worklane-stamps-{}", uuid::Uuid::new_v4()));
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
let dir = Fixture::new("worklane-stamps");
|
||||
|
||||
fs::write(
|
||||
dir.join("aaaa_walked.md"),
|
||||
@@ -3959,8 +4122,6 @@ mod tests {
|
||||
assert_eq!(fresh.committed_on, None);
|
||||
assert_eq!(fresh.submitted_on, None);
|
||||
assert_eq!(fresh.completed_on, None);
|
||||
|
||||
fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
/// Removal is scoped to the section for the same reason the write is: a description
|
||||
|
||||
Reference in New Issue
Block a user