#![allow(dead_code)]
use std::path::PathBuf;
use assert_cmd::Command;
pub fn corpora_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("..")
.join("..")
.join("tests")
.join("corpora")
}
pub fn corpus_a() -> PathBuf {
corpora_dir().join("corpus-a-canonical")
}
pub fn corpus_b() -> PathBuf {
corpora_dir().join("corpus-b-edges")
}
pub fn corpus_a_expected(rel: &str) -> PathBuf {
corpus_a().join("EXPECTED").join(rel)
}
pub fn corpus_b_expected(rel: &str) -> PathBuf {
corpus_b().join("EXPECTED").join(rel)
}
pub fn split_frontmatter_body(text: &str) -> Option<(&str, &str)> {
let stripped = text.strip_prefix('\u{feff}').unwrap_or(text);
let rest = stripped
.strip_prefix("---\n")
.or_else(|| stripped.strip_prefix("---\r\n"))?;
let mut idx = 0usize;
for line in rest.split_inclusive('\n') {
if line.trim_end_matches(['\r', '\n']) == "---" {
let fm = &rest[..idx];
let body = &rest[idx + line.len()..];
return Some((fm, body));
}
idx += line.len();
}
None
}
pub fn dbmd() -> Command {
Command::cargo_bin("dbmd").expect("the `dbmd` binary builds for integration tests")
}
pub fn copy_store_to_temp(src: &std::path::Path) -> (tempfile::TempDir, PathBuf) {
let tmp = tempfile::TempDir::new().expect("create tempdir");
let dst = tmp.path().join("store");
copy_dir_all(src, &dst);
(tmp, dst)
}
fn copy_dir_all(src: &std::path::Path, dst: &std::path::Path) {
std::fs::create_dir_all(dst).expect("create dest dir");
for entry in std::fs::read_dir(src).expect("read source dir") {
let entry = entry.expect("dir entry");
let file_type = entry.file_type().expect("file type");
let target = dst.join(entry.file_name());
if file_type.is_dir() {
copy_dir_all(&entry.path(), &target);
} else if file_type.is_file() {
std::fs::copy(entry.path(), &target).expect("copy file");
}
}
}
pub fn write_db_md(dir: &std::path::Path) {
std::fs::write(
dir.join("DB.md"),
"---\ntype: db-md\nscope: company\nowner: Test\n---\n\n# Test store\n",
)
.expect("write DB.md");
}
pub fn write_file(dir: &std::path::Path, rel: &str, contents: &str) -> PathBuf {
let abs = dir.join(rel);
std::fs::create_dir_all(abs.parent().expect("path has a parent")).expect("create parents");
std::fs::write(&abs, contents).expect("write file");
abs
}