use std::fs;
use std::path::{Path, PathBuf};
use pedant_core::resolution::rust::{ResolutionLimits, RustProject, RustProjectError};
pub type FixtureFile = (&'static str, &'static str);
pub fn write_file(base: &Path, relative: &str, contents: &[u8]) {
let path = base.join(relative);
let parent = path.parent().expect("a fixture path has a parent");
fs::create_dir_all(parent).unwrap();
fs::write(&path, contents).unwrap();
assert!(path.is_file(), "fixture file {relative} was not created");
}
pub fn build_repository(files: &[FixtureFile], reversed: bool) -> tempfile::TempDir {
let tmp = tempfile::tempdir().unwrap();
let mut ordered: Vec<&FixtureFile> = files.iter().collect();
if reversed {
ordered.reverse();
}
for (relative, contents) in ordered {
write_file(tmp.path(), relative, contents.as_bytes());
}
tmp
}
pub fn repository_root(tmp: &tempfile::TempDir) -> PathBuf {
tmp.path().join("repo")
}
pub fn load_project(
tmp: &tempfile::TempDir,
limits: ResolutionLimits,
) -> Result<RustProject, RustProjectError> {
RustProject::load(&repository_root(tmp), limits)
}
pub fn load_default(tmp: &tempfile::TempDir) -> RustProject {
load_project(tmp, ResolutionLimits::default()).unwrap()
}