From 37636d7a180551e2040a20395d767ce89267090c Mon Sep 17 00:00:00 2001 From: Mel Date: Sat, 9 Oct 2021 19:46:54 +0200 Subject: Add tests for FsMock and fix Path::parent weirdness. --- src/filesystem.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/src/filesystem.rs b/src/filesystem.rs index 3d9cd99..f8d498c 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -430,7 +430,10 @@ pub mod mock { fn get_or_create_file(&mut self, path: &Path) -> Option { if let Some(parent) = path.parent() { - if !self.is_directory(parent) && !self.create_directory(path) { + if !parent.as_os_str().is_empty() + && !self.is_directory(parent) + && !self.create_directory(parent) + { return None; } } @@ -497,7 +500,7 @@ pub mod mock { fn create_directory(&mut self, path: &Path) -> bool { if let Some(parent) = path.parent() { - if !self.is_directory(parent) && !self.create_directory(path) { + if !parent.as_os_str().is_empty() && !self.is_directory(parent) && !self.create_directory(parent) { return false; } } @@ -548,6 +551,11 @@ pub mod mock { } fn is_directory(&self, path: &Path) -> bool { + // We assume these exist. + if path.as_os_str() == "." || path.as_os_str() == "/" { + return true; + } + self.entries .get(path) .map_or(false, |e| matches!(e, EntryMock::Dir { .. })) @@ -599,4 +607,49 @@ pub mod mock { Ok(matches!(self, EntryMock::Dir { .. })) } } + + mod tests { + use std::path::Path; + + use crate::filesystem::{mock::EntryMock, Fs}; + + use super::{FsMock, FsState}; + + #[test] + fn empty() { + let mock = FsMock::new(); + mock.assert_match(FsState::new(Vec::new())) + } + + #[test] + fn basic() { + let mock = FsMock::new(); + + let mut file = mock.create_file(Path::new("./folder/file")).unwrap(); + mock.write_to_file(&mut file, "content".as_bytes().into()) + .unwrap(); + + mock.assert_match(FsState::new(vec![ + EntryMock::dir("./folder"), + EntryMock::file("./folder/file", "content".as_bytes()), + ])) + } + + #[test] + fn deletion() { + let mock = FsMock::new(); + + mock.create_file(Path::new("./folder/file_to_delete")).unwrap(); + mock.create_directory(Path::new("./dir_to_delete")).unwrap(); + mock.delete_file(Path::new("./folder/file_to_delete")).unwrap(); + mock.delete_directory(Path::new("./dir_to_delete")).unwrap(); + + mock.assert_match(FsState::new(vec![ + EntryMock::dir("./folder"), + ])) + } + + // TODO: Add more test coverage for FsMock, as it has to be as robust as possible + // to ensure that tests depending on it are sane. + } } -- cgit 1.4.1