Skip to content

test: add a self-removing TestDir helper for the test suites - #789

Merged
ajianaz merged 2 commits into
developfrom
test/testdir-helper
Nov 14, 2026
Merged

ajianaz merged 2 commits into
developfrom
test/testdir-helper

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds TestDir, WorkDir and ChildGuard, a small std-only RAII helper for test scratch directories, to tole-core and tole-cli. No existing test is converted in this PR; the conversions follow in separate PRs, one crate or family of tests each.

Why

Refs #781: the suites leaked about 175 000 tole-*/cora-* directories (17 GB) in one day because every test builds std::env::temp_dir().join(..) by hand and never removes it. Neither crate has tempfile or any dev-dependency and the owner decision is no new dependency, so the guard is written by hand.

Changes

  • crates/tole-core/tests/support/test_dir.rs and an identical crates/tole-cli/tests/common/test_dir.rs:
    • TestDir::new(prefix): unique directory (prefix-pid-counter-clock), canonical path, under CARGO_TARGET_TMPDIR in integration tests (cargo owns it, cargo clean removes it) and the system temp dir in unit tests. Derefs to Path, so call sites keep working.
    • Removed on Drop, also during a panic; retries after making the tree writable (read-only files and directories, Unix permissions behind cfg(unix)) and after a short wait (a child or scanner still holding a file). Never panics.
    • TOLE_KEEP_TEST_DIRS=1 keeps every directory and prints its path; TestDir::keep() keeps one.
    • WorkDir::new(prefix): <root>/w inside a TestDir, for tests that put a CODECORA_HOME next to the workdir with work.with_extension("home"); the twin lands in the same removed root.
    • ChildGuard(Child): kills and reaps the child on drop, so a guard declared after the TestDir stops the child before the directory is removed.
  • Exposed to integration tests through support/mod.rs / common/mod.rs (or #[path] for tests that do not pull those in) and to unit tests through a #[cfg(test)] #[path] module in each lib.rs. No production code changes.
  • tests/test_dir_helper.rs in both crates: removal on drop, on panic, read-only tree (Unix), uniqueness, keep(), WorkDir twin, ChildGuard (Unix).

Testing

  • cargo fmt --all, cargo clippy --workspace --all-targets -- -D warnings: clean.
  • cargo test -p tole-core --test test_dir_helper and cargo test -p tole-cli --test test_dir_helper: 7 passed each.
  • Baseline for the whole series, measured before any conversion with a private TMPDIR: one cargo test --workspace run leaves 1385 entries (148 MB) in TMPDIR plus one in target/tmp. After the whole series: none (see the conversion PRs and the CI guard PR).

Merge order: this PR first; the conversion PRs and the CI guard branch from it.

Refs #781

Refs #781. Adds TestDir (unique scratch directory removed on drop, also on panic, robust to read-only trees, TOLE_KEEP_TEST_DIRS=1 and keep() escape hatches) and ChildGuard (kill and reap a child before its directory goes) to tole-core and tole-cli, for integration tests and unit tests. No test is converted yet.
@github-actions

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

Refs #781. WorkDir creates <root>/w inside a TestDir, so work.with_extension("home") lands in the same removed root.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants