use rustledger_loader::Loader;
fn fixture(documents: &str, subdirs: &[&str]) -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
for sub in subdirs {
std::fs::create_dir_all(dir.path().join(sub)).unwrap();
}
std::fs::write(
dir.path().join("ledger.bean"),
format!("option \"documents\" \"{documents}\"\n\n2020-01-01 open Assets:Cash USD\n"),
)
.unwrap();
dir
}
fn e7006_warnings(dir: &tempfile::TempDir) -> Vec<String> {
let result = Loader::new()
.load(&dir.path().join("ledger.bean"))
.expect("ledger loads");
result
.options
.warnings
.iter()
.filter(|w| w.code == "E7006")
.map(|w| w.message.clone())
.collect()
}
#[test]
fn relative_root_next_to_ledger_is_found() {
let dir = fixture("docs", &["docs"]);
assert!(
!std::path::Path::new("docs").exists(),
"precondition: the test CWD must not contain a `docs` dir, or this \
test would pass even with the CWD-relative bug reinstated"
);
assert_eq!(
e7006_warnings(&dir),
Vec::<String>::new(),
"a `documents` root beside the ledger must resolve, regardless of CWD"
);
}
#[test]
fn missing_root_still_warns() {
let dir = fixture("nosuchdir", &[]);
let warnings = e7006_warnings(&dir);
assert_eq!(warnings.len(), 1, "expected one E7006, got {warnings:?}");
assert!(
warnings[0].contains("nosuchdir")
&& warnings[0].contains(&dir.path().display().to_string()),
"the warning should name the root and where it resolved to: {}",
warnings[0]
);
}
#[test]
fn root_present_only_in_cwd_still_warns() {
assert!(
std::path::Path::new("src").exists(),
"precondition: expected to run from the crate root, where `src` exists"
);
let dir = fixture("src", &[]);
let warnings = e7006_warnings(&dir);
assert_eq!(
warnings.len(),
1,
"a root that only exists in the CWD is not a valid document root: {warnings:?}"
);
}
#[test]
fn absolute_root_is_checked_as_given() {
let present = tempfile::tempdir().unwrap();
let dir = fixture(&present.path().display().to_string(), &[]);
assert_eq!(e7006_warnings(&dir), Vec::<String>::new());
let dir = fixture(&present.path().join("gone").display().to_string(), &[]);
assert_eq!(e7006_warnings(&dir).len(), 1);
}
#[test]
fn file_where_the_root_should_be_warns() {
let dir = fixture("docs", &[]);
std::fs::write(dir.path().join("docs"), "not a directory").unwrap();
assert_eq!(
e7006_warnings(&dir).len(),
1,
"a plain file named `docs` must not satisfy a documents root"
);
}
#[test]
fn virtual_filesystem_load_does_not_warn() {
let mut vfs = rustledger_loader::VirtualFileSystem::new();
vfs.add_file(
"/mem/ledger.bean",
"option \"documents\" \"docs\"\n\n2020-01-01 open Assets:Cash USD\n",
);
let result = Loader::new()
.with_filesystem(Box::new(vfs))
.load(std::path::Path::new("/mem/ledger.bean"))
.expect("in-memory ledger loads");
let warnings: Vec<_> = result
.options
.warnings
.iter()
.filter(|w| w.code == "E7006")
.collect();
assert!(
warnings.is_empty(),
"in-memory load must not warn about document roots it cannot see: {warnings:?}"
);
}