use std::fs;
use std::path::{Path, PathBuf};
use assert_fs::TempDir;
use assert_fs::prelude::*;
use lsp_types::Url;
pub struct Fixture {
t: TempDir,
files: Vec<PathBuf>,
}
impl Fixture {
pub fn new() -> Self {
let t = TempDir::new().unwrap();
Self { t, files: Vec::new() }
}
}
impl Fixture {
pub fn add_file(&mut self, path: impl AsRef<Path>, contents: impl AsRef<str>) {
self.files.push(path.as_ref().to_owned());
self.t.child(path).write_str(contents.as_ref().trim()).unwrap();
}
}
impl Fixture {
pub fn root_path(&self) -> &Path {
self.t.path()
}
pub fn root_url(&self) -> Url {
Url::from_directory_path(self.t.path()).unwrap()
}
pub fn file_absolute_path(&self, path: impl AsRef<Path>) -> PathBuf {
self.t.child(path).path().to_owned()
}
pub fn file_url(&self, path: impl AsRef<Path>) -> Url {
Url::from_file_path(self.file_absolute_path(path)).unwrap()
}
pub fn read_file(&self, path: impl AsRef<Path>) -> String {
fs::read_to_string(self.file_absolute_path(path)).unwrap()
}
}