From f43dc99f7a919bd12152e93703c8561e0123fe89 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Fri, 14 Dec 2018 14:33:45 -0800 Subject: [PATCH] Imfs test --- server/src/commands/build.rs | 3 +- server/src/imfs.rs | 24 +++++----- server/src/session.rs | 7 ++- server/tests/imfs.rs | 93 ++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 14 deletions(-) create mode 100644 server/tests/imfs.rs diff --git a/server/src/commands/build.rs b/server/src/commands/build.rs index 6d2ce204..83ba2346 100644 --- a/server/src/commands/build.rs +++ b/server/src/commands/build.rs @@ -77,7 +77,8 @@ pub fn build(options: &BuildOptions) -> Result<(), BuildError> { info!("Found project at {}", project.file_location.display()); info!("Using project {:#?}", project); - let imfs = Imfs::new(&project)?; + let mut imfs = Imfs::new(); + imfs.add_roots_from_project(&project)?; let tree = construct_oneoff_tree(&project, &imfs); let mut file = File::create(&options.output_file)?; diff --git a/server/src/imfs.rs b/server/src/imfs.rs index be9d544c..5891ff4a 100644 --- a/server/src/imfs.rs +++ b/server/src/imfs.rs @@ -32,25 +32,25 @@ pub struct Imfs { } impl Imfs { - pub fn new(project: &Project) -> io::Result { - let mut imfs = Imfs::empty(); - - add_sync_points(&mut imfs, &project.tree)?; - - Ok(imfs) - } - - pub fn empty() -> Imfs { + pub fn new() -> Imfs { Imfs { items: HashMap::new(), roots: HashSet::new(), } } + pub fn add_roots_from_project(&mut self, project: &Project) -> io::Result<()> { + add_sync_points(self, &project.tree) + } + pub fn get_roots(&self) -> &HashSet { &self.roots } + pub fn get_items(&self) -> &HashMap { + &self.items + } + pub fn get(&self, path: &Path) -> Option<&ImfsItem> { debug_assert!(path.is_absolute()); debug_assert!(self.is_within_roots(path)); @@ -141,19 +141,19 @@ impl Imfs { } } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct ImfsFile { pub path: PathBuf, pub contents: Vec, } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct ImfsDirectory { pub path: PathBuf, pub children: HashSet, } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum ImfsItem { File(ImfsFile), Directory(ImfsDirectory), diff --git a/server/src/session.rs b/server/src/session.rs index 5fb589f6..380fec2c 100644 --- a/server/src/session.rs +++ b/server/src/session.rs @@ -22,7 +22,12 @@ pub struct Session { impl Session { pub fn new(project: Project) -> io::Result { - let imfs = Arc::new(Mutex::new(Imfs::new(&project)?)); + let imfs = { + let mut imfs = Imfs::new(); + imfs.add_roots_from_project(&project)?; + + Arc::new(Mutex::new(imfs)) + }; let project = Arc::new(project); let message_queue = Arc::new(MessageQueue::new()); diff --git a/server/tests/imfs.rs b/server/tests/imfs.rs new file mode 100644 index 00000000..a6fd40ff --- /dev/null +++ b/server/tests/imfs.rs @@ -0,0 +1,93 @@ +use std::{ + collections::{HashMap, HashSet}, + io, + fs, + path::PathBuf, +}; + +use tempfile::tempdir; + +use librojo::{ + imfs::{Imfs, ImfsItem, ImfsFile, ImfsDirectory}, +}; + +struct ExpectedImfs { + roots: HashSet, + items: HashMap, +} + +fn check_expected(real: &Imfs, expected: &ExpectedImfs) { + assert_eq!(real.get_roots(), &expected.roots); + assert_eq!(real.get_items(), &expected.items); +} + +fn base_tree() -> io::Result<(Imfs, ExpectedImfs)> { + let root = tempdir()?; + + let foo_path = root.path().join("foo"); + let bar_path = root.path().join("bar.txt"); + let baz_path = foo_path.join("baz.txt"); + + fs::create_dir(&foo_path)?; + fs::write(&bar_path, b"bar")?; + fs::write(&baz_path, b"baz")?; + + let mut imfs = Imfs::new(); + imfs.add_root(root.path())?; + + let mut expected_roots = HashSet::new(); + expected_roots.insert(root.path().to_path_buf()); + + let root_item = { + let mut children = HashSet::new(); + children.insert(foo_path.clone()); + children.insert(bar_path.clone()); + + ImfsItem::Directory(ImfsDirectory { + path: root.path().to_path_buf(), + children, + }) + }; + + let foo_item = { + let mut children = HashSet::new(); + children.insert(baz_path.clone()); + + ImfsItem::Directory(ImfsDirectory { + path: foo_path.clone(), + children, + }) + }; + + let bar_item = ImfsItem::File(ImfsFile { + path: bar_path.clone(), + contents: b"bar".to_vec(), + }); + + let baz_item = ImfsItem::File(ImfsFile { + path: baz_path.clone(), + contents: b"baz".to_vec(), + }); + + let mut expected_items = HashMap::new(); + expected_items.insert(root.path().to_path_buf(), root_item); + expected_items.insert(foo_path.clone(), foo_item); + expected_items.insert(bar_path.clone(), bar_item); + expected_items.insert(baz_path.clone(), baz_item); + + let expected_imfs = ExpectedImfs { + roots: expected_roots, + items: expected_items, + }; + + Ok((imfs, expected_imfs)) +} + +#[test] +fn initial_read() -> io::Result<()> { + let (imfs, expected_imfs) = base_tree()?; + + check_expected(&imfs, &expected_imfs); + + Ok(()) +} \ No newline at end of file