about summary refs log tree commit diff
path: root/src/filesystem.rs
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2021-10-09 19:46:54 +0200
committerMel <einebeere@gmail.com>2021-10-09 19:46:54 +0200
commit37636d7a180551e2040a20395d767ce89267090c (patch)
tree447716ed1dab71aa21c57b5a322aa7f1e46982f9 /src/filesystem.rs
parentc522a3b7431159f2743df0c0833814318edb539b (diff)
downloadka-37636d7a180551e2040a20395d767ce89267090c.tar.zst
ka-37636d7a180551e2040a20395d767ce89267090c.zip
Add tests for FsMock and fix Path::parent weirdness.
Diffstat (limited to 'src/filesystem.rs')
-rw-r--r--src/filesystem.rs57
1 files 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<FileMock> {
             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.
+    }
 }