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
57 lines
1.8 KiB
Rust
57 lines
1.8 KiB
Rust
//! The receipt policy in force when the settings come from files:
|
|
//! config.json's when it sets one, else the settings file's threshold.
|
|
|
|
use ledger_config::{BooksSettings, effective_receipt_policy};
|
|
|
|
/// 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 Drop for TestDir {
|
|
fn drop(&mut self) {
|
|
let _ = std::fs::remove_dir_all(&self.0);
|
|
}
|
|
}
|
|
|
|
fn dir(tag: &str) -> TestDir {
|
|
let d = std::env::temp_dir().join(format!("ledger-config-policy-{}-{tag}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&d);
|
|
std::fs::create_dir_all(&d).unwrap();
|
|
TestDir(d)
|
|
}
|
|
|
|
#[test]
|
|
fn config_json_wins_else_the_settings_file() {
|
|
let d = dir("files");
|
|
let json = d.join("config.json");
|
|
let settings = BooksSettings {
|
|
receipt_threshold: Some("0.00".into()),
|
|
..BooksSettings::default()
|
|
};
|
|
// No config.json: the settings file's threshold.
|
|
let p = effective_receipt_policy(&json, &settings).unwrap();
|
|
assert_eq!(p.require_receipts_above, "0.00");
|
|
// config.json's policy, whole.
|
|
std::fs::write(
|
|
&json,
|
|
r#"{"receipt_policy": {"require_receipts_above": "10.00", "receipt_exempt_accounts": ["6999"]}}"#,
|
|
)
|
|
.unwrap();
|
|
let p = effective_receipt_policy(&json, &settings).unwrap();
|
|
assert_eq!(p.require_receipts_above, "10.00");
|
|
assert_eq!(p.receipt_exempt_accounts, ["6999"]);
|
|
// A broken one is refused, by name.
|
|
std::fs::write(&json, "{ not json").unwrap();
|
|
let err = effective_receipt_policy(&json, &settings).unwrap_err();
|
|
assert!(
|
|
err.starts_with(&format!("bad config {}", json.display())),
|
|
"{err}"
|
|
);
|
|
}
|